13

I am having difficulty getting \tikzmark to do its magic. I tried using the tikz library for \tikzmark as in the following code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
    \tikzmark{calculator}\approx
    156    
\]
\begin{tikzpicture}[overlay,remember picture]
    \draw[arrows=->] (calculator) ++(0,2ex) -- (calculator);
\end{tikzpicture}
\end{document}

But I get a complaint as follows:

! Package pgf Error: No shape named calculator is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.14     \draw[arrows=->] (calculator)
                                       ++(0,2ex) -- (calculator);

I tried a different approach where I didn't bother to load the tikzmark library:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}%,tikzmark}
\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
    \tikzmark{calculator}\approx
    156    
\]
\begin{tikzpicture}[overlay,remember picture]
    \draw[arrows=->] (calculator) ++(0,2ex) -- (calculator);
\end{tikzpicture}
\end{document}

and I get the totally cryptic error:

! Undefined control sequence.
l.2 \savepointas
                {calculator}{pgfid1}
? 

I thought I understood overlays and whatnot, but clearly I don't.

What I would like to do here is something like the following kludge:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}%,tikzmark}
\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
    \raisebox{-\height}{$\stackrel{\approx}{\stackrel{\uparrow}{\makebox[0pt]{use calculator}}}$}
    156    
\]
\end{document}

enter image description here

A.Ellett
  • 50,533

3 Answers3

15

The tikzmark library uses the pic coordinate system, so you need to call the marks using the pic cs: prefix, as in (pic cs:calculator):

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
    \tikzmark{calculator}\approx
    156    
\]
\begin{tikzpicture}[overlay,remember picture]
    \draw[arrows=->] 
    ( $ (pic cs:calculator) +(6pt,-2.5ex) $ ) -- 
    ( $ (pic cs:calculator) +(6pt,-0.5ex) $ );
    \node[anchor=north]
    at ( $ (pic cs:calculator) +(6pt,-2ex) $ )
    {Use calculator};
\end{tikzpicture}
\end{document}

enter image description here

With your second code, the problem is that in the first run, the coordinates for the mark mark are still not calculated. The second run of the code works since by then the coordinate has been appropriately calculated and the arrow can be drawn. You can avoid the error in the first run by using

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}%,tikzmark}
\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
    \tikzmark{calculator}\approx
    156    
\]
\expandafter \ifx\csname pgf@sys@pdf@mark@pos@pgfid\the \csname
pgf@picture@serial@count\endcsname\endcsname\relax
\else
\begin{tikzpicture}[overlay,remember picture]
\draw[arrows=->] 
  ( $ (calculator) +(6pt,-2.5ex) $ ) -- 
  ( $ (calculator) +(6pt,-0.5ex) $ );
\node[anchor=north]
  at ( $ (calculator) +(6pt,-2ex) $ )
  {Use calculator};\end{tikzpicture}
\fi
\end{document}
Gonzalo Medina
  • 505,128
  • How do I get the arrow under the \approx? – A.Ellett Oct 25 '14 at 21:20
  • Also, I wanted to do somehting more like \draw[arrows=->] (pic cs:calculator) ++ (0,-4ex) -- (pic cs:calculator.south);. But, .south gets placed completely incorrectly. – A.Ellett Oct 25 '14 at 21:22
  • @A.Ellett Please see my updated answer. – Gonzalo Medina Oct 25 '14 at 21:22
  • @A.Ellett I don't think you can use .south because pics do not have anchors in the same way that nodes do. (Unless the tikzmark library does the work to emulate them?) – cfr Oct 25 '14 at 21:29
  • Is there a way to get my second code to work without it generating an error on the first run (I understand that the node will not be place correctly until the second run)? – A.Ellett Oct 25 '14 at 21:34
  • @A.Ellett yes, there is. Give me some seconds and I will add this to my answer. Regarding the .south operation, take into account that using \tikzmark doesn't really produce a node, so those modifiers won't work. – Gonzalo Medina Oct 25 '14 at 21:35
  • @A.Ellett I added the code that will prevent the error in the first run using your second code. – Gonzalo Medina Oct 25 '14 at 21:44
  • You are correct about the new code not generating an error. I'm not sure what that code's actually doing. But the expansion text seems to fail for every run and the tikzpicture is never drawn. – A.Ellett Oct 25 '14 at 22:08
  • @A.Ellett Hmmmm. Let me think about this. – Gonzalo Medina Oct 26 '14 at 15:55
  • @A.Ellett Please see my updated answer (I had forgotten the required \else so the drawing is made after the first run and the closing \fi). – Gonzalo Medina Oct 26 '14 at 16:13
12

For fun, a variant without tikz and without the need for an additional LaTeX run:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
  \underset{\mathclap{\shortstack{$\uparrow$\\[-.2ex]use calculator}}
  }{\approx} 156
\]
\end{document}

Result

Remarks:

  • Package mathtools loads amsmath and defines further commands like \mathclap, in this case also \makebox[0pt]{...} can be used.
  • \underset comes from package amsmath.
  • \shortstack is defined in the LaTeX kernel.
Heiko Oberdiek
  • 271,626
  • So much better than my kludge. Thank you for this post and reminding me about the mathtools package. – A.Ellett Oct 25 '14 at 21:38
2

A variation which just creates a coordinate underneath the \approx sign, picking up the horizontal distance from two marks and adjusting the vertical somewhat. This coordinate is then used as the basis for placing the node and arrow.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\begin{document}
By taking logarithms of both sides
\[
  t = \frac{30\cdot\ln(3/22)}{\ln(15/22)}
  \tikzmark{calculator}\approx\tikzmark{otherside}
    156
\]
\begin{tikzpicture}[overlay,remember picture]
  \coordinate (target) at ($(pic cs:calculator)!1/2!(pic cs:otherside) - (0,.5ex)$);
  \draw[arrows=->] (target) ++(0,-2ex) node [anchor=north] {use calculator} -- (target);
\end{tikzpicture}
\end{document}

marks and coordinates

cfr
  • 198,882
  • Very nice approach to centering the arrow below the \approx. – A.Ellett Oct 25 '14 at 21:58
  • @A.Ellett Thanks. I'm not sure if it is efficient, but I often find this is the simplest way to centre something in TiKZ ;). (And since you were loading calc anyway, I figured calculating the extra coordinate couldn't hurt much.) – cfr Oct 25 '14 at 22:01