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

3 Answers 3

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}
10
  • How do I get the arrow under the \approx?
    – A.Ellett
    Commented Oct 25, 2014 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
    Commented Oct 25, 2014 at 21:22
  • @A.Ellett Please see my updated answer. Commented Oct 25, 2014 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
    Commented Oct 25, 2014 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
    Commented Oct 25, 2014 at 21:34
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.
1
  • So much better than my kludge. Thank you for this post and reminding me about the mathtools package.
    – A.Ellett
    Commented Oct 25, 2014 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

2
  • Very nice approach to centering the arrow below the \approx.
    – A.Ellett
    Commented Oct 25, 2014 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
    Commented Oct 25, 2014 at 22:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .