Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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".

share|improve this question
2  
I assume that the line \node (X\xplusone0)... should be \node (X\xplusone1)...? Also, for questions like this, please post complete compilable minimal files. In this case, you don't need the figure environment, but it would save others some work if you included the preamble and the document environment. –  Jake Sep 26 '11 at 6:23
    
Closely related question: tex.stackexchange.com/q/4275/86 –  Loop Space Sep 26 '11 at 7:27
    
Also closely related: tex.stackexchange.com/q/27822/86 –  Loop Space Sep 26 '11 at 7:28
    
Oh, and: tex.stackexchange.com/q/28228/86 –  Loop Space Sep 26 '11 at 7:29

1 Answer 1

up vote 10 down vote accepted

\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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