如何让 tikzmark 工作

如何让 tikzmark 工作

我很难\tikzmark发挥它的魔力。我尝试使用 tikz 库,如\tikzmark以下代码所示:

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

但我收到了如下投诉:

! 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);

我尝试了一种不同的方法,不再费心加载tikzmark库:

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

我得到了一个非常神秘的错误:

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

我以为我理解了覆盖和诸如此类的东西,但显然我并不理解。

我想在这里做的事情类似于以下的临时解决方案:

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

在此处输入图片描述

答案1

tikzmark库使用pic坐标系,因此您需要使用前缀调用标记pic cs:,如下所示(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}

在此处输入图片描述

对于您的第二段代码,问题在于第一次运行中仍未计算出标记的坐标。代码第二次运行成功,因为此时坐标已正确计算,可以绘制箭头。您可以使用以下代码避免第一次运行中的错误

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

答案2

tikz为了好玩,可以使用无需额外运行 LaTeX 的变体:

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

结果

评论:

  • mathtools加载amsmath并定义进一步的命令,如\mathclap,在这种情况下也\makebox[0pt]{...}可以使用。
  • \underset来自包amsmath
  • \shortstack在 LaTeX 内核中定义。

答案3

一种变体,它只是在\approx标志下方创建一个坐标,从两个标记中选取水平距离并稍微调整垂直距离。然后使用该坐标作为放置节点和箭头的基础。

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

标记和坐标

相关内容