Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
So it can be used a cross align’s & or even different lines … though, I agree, a simple boxing macro should be added to the hf-tikz package, as well. Have you read the manual, say page 2 and following? –  Qrrbrbirlbel Feb 17 '13 at 3:34
    
yes, the manual says to add stuff like: (0.1,-0.4)(-0.1,0.5) to specify the box boundaries. Makes it fairly useless unless you want to do a lot of error prone and unmaintainable boundary adjustment (for example in a larger document with 38 boxed equations). –  Peeter Joot Feb 17 '13 at 3:57
    
IMHO the possibility to customize at user level the dimensions of a box just adding some shift offsets is one of the major potentiality of the package. At the beginning there was a debate about let things be automatic or not with two solutions, as per 1, but the automatic one cannot be used with align and similar environments. AFAIK it is quite simple measure width and height of stuff on the same line, but in case things are across multiple line it's difficult, see 2 and the related question. –  Claudio Fiandrino Feb 17 '13 at 8:58
    
You know, \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. –  Claudio Fiandrino Feb 17 '13 at 9:09
    
Notice that once determined the height offset needed to an integral you're done: each time you want to highlight a formula with an integral you can reuse it. Same thing for fractions or sums. Sorry for the long comments, but they actually don't answer you: just some words of explanation on the design choices used. :) –  Claudio Fiandrino Feb 17 '13 at 9:12
show 2 more comments

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
+1: It was my turn, but your answer is really nice. I won't answer any more, but I will comment the question to explain a bit the reasons behind the design choices of the package. –  Claudio Fiandrino Feb 17 '13 at 8:49
    
@ClaudioFiandrino Feel free to post an answer yourself. After all, it might be the correct answer to the first of OP’s questions. While delving into the implemention I wondered why #3 and #4 where given as fixed length (being based on the default TikT cm unit) and are not based on ex and/or em? But than again, it’s math where anything can happen, so either choice probably had to be corrected manually at some point sooner or later. –  Qrrbrbirlbel Feb 17 '13 at 9:02
    
Basically my thought was that dimension all on the base of the font size is of help if inside the document the font size of the equations is changed frequently (and in turn adapts automatically the offsets). But testing the code, I noticed that the offsets' default values worked pretty in the same way for the basic font sizes (10,11,12pt), so for simplicity I left fixed units; consider that I assumed the default behaviour be the inline highlighting without fractions, integrals or sums thus the adaptation is for that case. The other ones should be "adapted" via offsets specifications. –  Claudio Fiandrino Feb 17 '13 at 9:35
    
How are the various arguments passed to your \tikzmarkin functions. For example in your solution 3, the equation content is #5. If, for example, I wanted to pass 'rounded corners' as your parameter #1, how would that be done? Or does #1 already have a value (what and from where?) –  Peeter Joot Feb 17 '13 at 17:11
    
@PeeterJoot You can still use \tikzmarkin[rounded corners]{<id>}[<lower math content>][<upper math content>]. hf-tikz also provides the key disable rounded corners, setting that to false does the some thing as rounded corners. (Note that all the package options (like beamer, shade, nofill, …) are disabled as I overwrite the \tikzmarkin macro.) –  Qrrbrbirlbel Feb 17 '13 at 17:31
show 1 more comment

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

share|improve this answer
    
If I understand what you are doing properly, this provides a mechanism for making classes of offsets for various uses, which should make it easier if you want to manually pick your box sizes, but still isn't a mechanism for automatically sizing the boxes. Correct? –  Peeter Joot Mar 9 '13 at 20:38
    
Almost true :) As said in the comments, the horizontal box size is never a problem (there are left and right offsets, but the \tikzmark principle works even in their absence). The problem is the vertical adjustment which depends on a) math operators used b) font size. I said you that actually once determined the right offsets, they can be re-used for all equations: in some sense the box dimensions are pre-computed (not really true). With the new version you see this property: I set once the offsets for sums and integrals and they hold for the rest of the document. –  Claudio Fiandrino Mar 10 '13 at 9:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.