TikZ-PGF – Fixing hf-tikz Not Boxing Around Equation

boxesequationshf-tikztikz-pgf

In TikZ based \boxed construct is causing pdflatex to hang it was suggested to use hf-tikz instead of manually using tikz. The documentation indicates that this package doesn't work properly for tall formulas like \frac{}'s. Sure enough, it doesn't, as illustrated in the following:

\documentclass{article}

\usepackage{hf-tikz}

\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}

This looks like

hf-tikz example output

Why is the manual offsetting that is suggested in the hf-tikz required, when a simple \boxed is able to figure out the dimensions to use? How can the box sizing be automated?

Best Answer

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

Related Question