如何将表达式的各个部分定义为 TikZ 节点?

如何将表达式的各个部分定义为 TikZ 节点?

我想解释一下表达式中不同符号的含义,如下所示:在此处输入图片描述

目前我使用 TikZ 来做这件事(见底部的 MWE),为表达式中我想要指向箭头的所有内容创建节点。调整节点的位置非常耗时,因此结果看起来就像您以普通方式编写表达式时得到的结果一样。

有没有更优雅的方法来做到这一点?

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}

\begin{document}

\begin{tikzpicture}
  [ plass/.style={inner sep=0pt,minimum size=4mm} ]
  \node[plass] (minusfortegn)                                    {$-$};
  \node[plass] (tre)            [right=-1.8mm of minusfortegn  ] {$3$};
  \node[plass] (minusoperator)  [right=-1.0mm of tre           ] {$-$};
  \node[plass] (to)             [right=-1.0mm of minusoperator ] {$2$};
  \node[plass] (lik)            [right=-1.6mm of to            ] {\phantom{(}=\phantom{)}};
  \node[plass] (minusfortegnto) [right=-1.4mm of lik           ] {$-$};
  \node[plass] (fem)            [right=-1.8mm of minusfortegnto] {$5$};

  \node (minusfortegntekst)  [above=4mm of to] {Minustegn som fortegn};
  \node (minusoperatortekst) [below=4mm of to] {Minustegn for subtraksjon};

  \draw [->] (minusfortegntekst)  to [out=230,in= 90] (minusfortegn);
  \draw [->] (minusfortegntekst)  to [out=310,in= 90] (minusfortegnto);
  \draw [->] (minusoperatortekst) to [out=110,in=270] (minusoperator);
\end{tikzpicture}

\end{document}

编辑:我查看了 hf-tikz 包,但在这种情况下它似乎不太优雅。不过我真的很喜欢 tikzmark 库中的子节点方法。@manuel 在下面的评论中展示了一种很好的方法。

我对 @Ignasi 的解决方案做了一些调整,最终得到了下面的代码,对我来说效果很好。非常感谢 @Ignasi、@Harish 和 @manuel 提供的宝贵反馈!

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning, tikzmark}

\begin{document}

\begin{tikzpicture}[remember picture]
  \node { $\subnode{fortegn1}{$-$} 3 \mathbin{\subnode{minusoperator}{$-$}} \subnode{to}{$2$} = \subnode{fortegn2}{$-$} 5$ };
  \node[above=4mm of to] (minusfortegntekst)  {Minustegn som fortegn};
  \node[below=4mm of to] (minusoperatortekst)  {Minustegn for subtraksjon};
  \draw [->] (minusfortegntekst)  to [out=230,in= 90] (fortegn1);
  \draw [->] (minusfortegntekst)  to [out=310,in= 90] (fortegn2);
  \draw [->] (minusoperatortekst) to [out=110,in=270] (minusoperator);
\end{tikzpicture}

\end{document}

答案1

您可以仅将要指向的表达式部分声明为节点。尽管我必须调整元素周围的空间,使其或多或少看起来像原始表达式。

另一个选择是使用类似hf-tikz

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usetikzlibrary{positioning}

\begin{document}

\[\tikz[remember picture]{\node[inner sep=0pt](m1){$-\,$ };}3
\tikz[remember picture]{\node[inner sep=0pt](m2){$\,-\,$};}2=
\tikz[remember picture]{\node[inner sep=0pt](m3){$-\,$};}5\]

\begin{tikzpicture}[remember picture, overlay]
\node[above= 5mm of m2] (M1)  {Minustegn som fortegn};
\node[below= 5mm of m2] (M2)  {Minustegn for subtraksjon};
\draw [->] (M1)  to [out=230,in= 90] (m1);
\draw [->] (M1)  to [out=310,in= 90] (m3);
\draw [->] (M2) to [out=110,in=270] (m2);
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:正如 Harish 所建议的,可以使用以下方法简化此代码\tikzmark以原始形式

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

或使用subnode来自其后部发展的tikzmarklibrary就像下面的代码一样(已根据 Manuel 的建议进行了更正,使表达式marked看起来像常规的数学表达式)。

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning, tikzmark}

\begin{document}

\[
\begin{tikzpicture}[remember picture]
\node {$\subnode{m1}{$-$} 3 \mathbin{\subnode{m2}{$-$}} 2 = \subnode{m3}{$-$} 5$};
\end{tikzpicture}
\]

\begin{tikzpicture}[remember picture, overlay]
\node[above= 5mm of m2] (M1)  {Minustegn som fortegn};
\node[below= 5mm of m2] (M2)  {Minustegn for subtraksjon};
\draw [->] (M1)  to [out=230,in= 90] (m1);
\draw [->] (M1)  to [out=310,in= 90] (m3);
\draw [->] (M2) to [out=110,in=270] (m2);
\end{tikzpicture}

\end{document}

相关内容