9

I'm having trouble incrementing a variable in TikZ and then using the result as part of a node name:

\def\x{2}
\begin{figure}[H]
\begin{tikzpicture}
\pgfmathsetmacro{\xplusone}{add(\x,1)}
\node (X\xplusone1) at (0,0) {};
\path (1, 1) edge (X\xplusone1);
\end{tikzpicture}
\end{figure}

seems to be parsing the final line as "X3" instead of "X31".

13

\pgfmathsetmacro always saves its result with a decimal place, even if you're just working with integers. This will confuse TikZ, which assumes that a period separates a node name from an anchor. You should use \pgfmathtruncatemacro instead, which will discard the decimal digits:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\def\x{2}
\begin{tikzpicture}
\pgfmathtruncatemacro{\xplusone}{\x + 1}
\node (X\xplusone1) at (0,0) {};
\path (1, 1) edge (X\xplusone1);
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.