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:
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.
uses the ending \tikzmarkend
to find the math content. The same procedure follows always (height, depth, …). This will break heavily if you nest hf-tikz
s.
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

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

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

align
’s&
or even different lines … though, I agree, a simple boxing macro should be added to thehf-tikz
package, as well. Have you read the manual, say page 2 and following?\boxed
makes a box around the whole math stuff, but try to measure things only in a part of that: hf-tikz indeed allows you even to highlight part of the formula. For that reason, at my eyes at least, some manual effort is not so dangerous: notice that you think to have always to do a trial and error procedure to identify the offsets, but it's not like this. Indeed, the horizontal part is always ok thanks to the\tikzmark
approach: the problem is the height. But, if you don't change every time the math font size, you can reuse the offsets.