2

In TikZ one can specify a node by a command like

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

This is very useful if one wishes to apply a command to the text in the node, say, to avoid writing

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

and simply set

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

(which comes very handy when used repeatedly!)

However, there are some restrictions in the syntax, because the parsing of the command stops after node contents is found. In particular, this seems to prevent one from placing such a node on a path by means of decorations.markings library. Below is the minimal (non-)working example:

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

The last command raises an error "Paragraph ended before \tikz@fig@scan@options was complete. ...node[node contents=C];}] (0,-2) to (1,-2);"

Is there a workaround?

2 Answers 2

3

While I agree that node contents has flaws, in this case you are just lacking braces. In general you can often use execute at begin node to add something to the node contents, this key has, according to what I find, less flaws.

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

enter image description here

3

Just put the code between braces.

 with {\node {B};}

screenshot

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

You must log in to answer this question.

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