hf-tikz doesn't box around equation

Until it is Claudio Fiandrino’s (the author of hf-tikz) turn, let me propose four solutions.

Solution 0

Of course, there is the solution that brought you to this: Just use one node that gets drawn and filled:

\NewDocumentCommand{\tikzmarksolo}{O{} O{} m}{% needs hf-tikz (uses same style, no beamer)
    \tikz[remember picture]
    \node[line width=1pt,rectangle,fill=\fcol,#1,draw=\bcol, anchor=base]
    (pic cs:#2) {$\displaystyle #3$};%  #2 shouldn’t be optional,
                                     %  either drop (pic cs:#2) if #2 is empty
                                     %  or make it mandatory
}

Solution 1 and 2

Solution 1 and 2 are very similar, they only differ on how they catch the math content:

  1. uses an optional last argument that is delimited by { }.

    If there is a last argument (#5 in the code) it is used to determine its height and depth. Those are added to the rectangle.

  2. uses the ending \tikzmarkend to find the math content. The same procedure follows always (height, depth, …). This will break heavily if you nest hf-tikzs.

Which brings me to the advantages of the „uselessness“ of hf-tikz: It works across & alignments and line-breaks of the amsmath environments, and it could even be nested.

Solution 3

A compromise: Solution works like the original but instead of having to specify the amount of height and depth, it determines those from math content given as optional arguments.

Codes/Outputs

Code 1 (optional { } argument)

\documentclass{article}

\usepackage{hf-tikz}
\newsavebox\qrrTikzmarkBox
\RenewDocumentCommand{\tikzmarkin}{O{} m D(){0.1,-0.18} D(){-0.1,0.35} G{}}{%
\if\relax\detokenize{#5}\relax
    \dp\qrrTikzmarkBox=0pt\relax
    \ht\qrrTikzmarkBox=0pt\relax
\else
    \sbox\qrrTikzmarkBox{$\displaystyle#5$}
\fi
\tikz[remember picture,overlay]
\draw[line width=1pt,rectangle,fill=\fcol,#1,draw=\bcol]
(pic cs:#2) ++([yshift=-\the\dp\qrrTikzmarkBox]#3) rectangle ([yshift=\the\ht\qrrTikzmarkBox]#4) node [anchor=text] (#2) {}
;
#5
}
\begin{document}

\[\tikzmarkin{a}x + y = 400\tikzmarkend{a}\]

\[
\tikzmarkin{z2}{
\int_{
    E - \frac{\Delta}{2} \le H \le E + \frac{\Delta}{2} \le H
    }
     d^{3N} x d^{3N} p
    =
    \left( \frac{2 \pi \Delta}{\omega} \right)^{3N}}
\tikzmarkend{z2}
\]
\end{document}

Output 1

enter image description here

Code 2 (catches anything until \tikzmarkend)

\documentclass{article}

\usepackage{hf-tikz}
\newsavebox\qrrTikzmarkBox
\RenewDocumentCommand{\tikzmarkin}{O{} m D(){0.1,-0.15} D(){-0.1,0.18} u{\tikzmarkend}}{%
\ifx\\#5\\
    \dp\qrrTikzmarkBox=0pt\relax
    \ht\qrrTikzmarkBox=0pt\relax
\else
    \sbox\qrrTikzmarkBox{$\displaystyle#5$}
\fi
\tikz[remember picture,overlay]
\draw[line width=1pt,rectangle,fill=\fcol,#1,draw=\bcol]
(pic cs:#2) ++([yshift=-\the\dp\qrrTikzmarkBox]#3) rectangle ([yshift=\the\ht\qrrTikzmarkBox]#4) node [anchor=text] (#2) {}
;
#5
\tikzmarkend
}
\begin{document}

\[\tikzmarkin{a}x + y = 400\tikzmarkend{a}\]

\[
\tikzmarkin{z2}
\int_{
    E - \frac{\Delta}{2} \le H \le E + \frac{\Delta}{2} \le H
    }
     d^{3N} x d^{3N} p
    =
    \left( \frac{2 \pi \Delta}{\omega} \right)^{3N}
\tikzmarkend{z2}
\]
\end{document}

Output 2

enter image description here

Code 3 (optional argument with math content)

\documentclass{article}
\usepackage{hf-tikz}
\newsavebox\qrrTikzmarkBoxA
\newsavebox\qrrTikzmarkBoxB
\RenewDocumentCommand{\tikzmarkin}{O{} m O{} O{0}}{%
\if\relax\detokenize{#3}\relax
    \dp\qrrTikzmarkBoxA=0pt\relax
\else
    \sbox\qrrTikzmarkBoxA{$\displaystyle#3$}
\fi
\if\relax\detokenize{#4}\relax
    \ht\qrrTikzmarkBoxB=0pt\relax
\else
    \sbox\qrrTikzmarkBoxB{$\displaystyle#4$}
\fi
\tikz[remember picture,overlay]
\draw[line width=1pt,rectangle,fill=\fcol,#1,draw=\bcol]
(pic cs:#2) ++([yshift=-\the\dp\qrrTikzmarkBoxA] 0.1,-0.18) rectangle ([yshift=\the\ht\qrrTikzmarkBoxB] -0.1,0.15) node [anchor=text] (#2) {}
;
}
\begin{document}
\[\tikzmarkin{a}x + y = 400\tikzmarkend{a}\]

\[
\tikzmarkin{z2}[\int_{\frac{\Delta}{2}}][\left( \frac{2 \pi \Delta}{\omega} \right)^{3N}]
\int_{
    E - \frac{\Delta}{2} \le H \le E + \frac{\Delta}{2} \le H
    }
     d^{3N} x d^{3N} p
    =
    \left( \frac{2 \pi \Delta}{\omega} \right)^{3N}
\tikzmarkend{z2}
\]
\end{document}

Output 3

enter image description here


The version 0.3 of the package fixes the problem of shift offsets insertion every time an equation should be highlighted. Indeed, one of the novelties is the introduction of a key-interface able to customize them, done in compatibility with the previous mechanism of course.

What at the moment is not mentioned in the manual, is the possibility to introduce styles helpful to make an offset and color customization applicable in the whole document (not explained in the manual only because I think about this today :)).

The keys that specify shift offsets are above/below/left/right/ offset and above left/below right offset which sets the combination of the previous ones. They should be introduced as first argument of \tikzmarkin in normal documents and as second argument within presentations (due to the presence of the overlay specification).

When the shift offsets are provided both via keys and via coordinates, i.e.

\tikzmarkin[above left offset={...},below right offset={...}]{marker-id}(..,..)(..,..)

the coordinate-based definition overrides the key-based definition.

An example:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}

\tikzset{offset def/.style={
    above left offset={-0.1,0.8},
    below right offset={0.1,-0.65},
  },
  integral first/.style={
    offset def,
  },
  integral second/.style={
    offset def,
    set fill color=green!50!lime!60,
    set border color=green!40!black,
  },
  sums/.style={
    offset def,
    set fill color=blue!20!cyan!60,
    set border color=blue!60!cyan,
  }
}

\begin{document}


\[\tikzmarkin{x-a}x + y = 400\tikzmarkend{x-a}\]

\vspace*{3ex}

\[
\tikzmarkin[integral first]{z2}
\int_{
E - \frac{\Delta}{2} \le H \le E + \frac{\Delta}{2} \le H
}
 d^{3N} x d^{3N} p
=
\left( \frac{2 \pi \Delta}{\omega} \right)^{3N}
\tikzmarkend{z2}
\]

\vspace*{3ex}

\[
\tikzmarkin[integral second]{z3}
\int_{
E - \frac{\Delta}{2} \le H \le E + \frac{\Delta}{2} \le H
}
 d^{3N} x d^{3N} p
\tikzmarkend{z3}
=
\tikzmarkin[integral first,disable rounded corners=true]{z4}
\left( \frac{2 \pi \Delta}{\omega} \right)^{3N}
\tikzmarkend{z4}
\]

\vspace*{3ex}

\begin{equation}
\begin{split}
H_c&=\tikzmarkin[disable rounded corners=true,sums]{xb}\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\tikzmarkend{xb}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\tikzmarkin[sums]{xb1}(0.05,-0.6)(-0.05,0.75)
\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].
\tikzmarkend{xb1}
\end{split}
\end{equation}

\vspace*{3ex}

\[
\tikzmarkin[below offset=-0.4,
  above offset=0.55,
  set fill color=magenta!60!purple!30]{bla bla}
x + \dfrac{y}{z} = 400
\tikzmarkend{bla bla}
\]
\end{document}

The result:

enter image description here