将节点放置在路径上时的节点内容

将节点放置在路径上时的节点内容

在 TikZ 中,可以通过以下命令指定节点:

\path (0,0) node[node contents=A];

如果希望将命令应用于节点中的文本,这非常有用,例如,为了避免编写

\node at (0,0) {\contour{red}{A}};

并简单设置

\path (0,0) node[node contents={\contour{red}{A}}];

(重复使用时非常方便!)

但是,语法中有一些限制,因为命令的解析在node contents找到后停止。特别是,这似乎阻止人们通过decorations.markings库将这样的节点放置在路径上。下面是最小(非)工作示例:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}
\path (.5,0) node[node contents={A}];
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with \node {B};}] (0,-1) to (1,-1);
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with \node[node contents=C];}] (0,-2) to (1,-2);
\end{tikzpicture}

\end{document}

最后一个命令引发错误“ Paragraph ended before \tikz@fig@scan@options was complete. ...node[node contents=C];}] (0,-2) to (1,-2);

有解决方法吗?

答案1

虽然我同意node contents有缺陷,但在这种情况下你只是缺少括号。一般来说,你经常会用execute at begin node它向节点内容添加一些内容,但据我发现,这个键的缺陷较少。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
\path (.5,0) node[node contents={A}];
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with \node {B};}] (0,-1) to (1,-1);
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with 
{\node[node contents=C];}}] (0,-2) to (1,-2);
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with 
{\node[execute at begin node=D]{};}}] (0,-3) to (1,-3);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

只需将代码放在括号之间即可。

 with {\node {B};}

截屏

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}
\path (.5,0) node[node contents={A}];
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with {\node {B};}}] (0,-1) to (1,-1);
\draw[postaction=decorate,decoration={markings,mark=at position 0.5 with {\node[node contents=C];}}] (0,-2) to (1,-2);
\end{tikzpicture}

\end{document}

相关内容