# Variables in TikZ and other basic programming concepts

I'm discovering TikZ. I'm using it to draw a fairly complex automaton (something around 20 states). I did it the all manual way (ie by specifying all the states and all the connections). Though it was very long and really boring, so now I try to do it the correct way (ie by doing some looping).

Though I encounter a kinda big problem: I'd like to program a bit but I don't know what are the common tools (ie variables and things like that) available to me. I do know the basis of TeX and even started reading the TeX book, but I'm not advanced in it yet and still lack the basis.

Here for example I'd like to loop through a list of node and link it to the next one. For that I'd like to memorize the last node I went through so that it can be the departure of my next link (I tried to provide a MWE but my actual automaton is way bigger).

My attempt:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows, automata}

\begin{document}
\begin{tikzpicture}

\node[initial,state]    (A)                    {A};
\node[state]            (B) [right of=A]       {B};
\node[state]            (C) [right of=B]       {C};
\node[state, accepting] (D) [right of=C]       {D};

\newcommand\from{A}
\foreach \to in {B, C, D}
{
\path (\from) edge [-] (\to);
\renewcommand\from{\to};
}
\end{tikzpicture}
\end{document}



So what I'd like is a pointer towards a good tutorial where I could grab all those basic principles, because the TikZ/pgf manual is large and I didn't find the info I need. The principles I'd like to learn are the basis of TikZ programming.

PS: I guess my question must have been asked before. However I didn't find an answer in the questions I got as a result for my research.

-

It looks innocent but actually your question is linked to TeX groups, expansion and global/local definitions. Hence it is indeed quite broad to tackle it here and goes well beyond TikZ to TeX programming.

• \foreach operates in a TeX group. Among other peculiarities, the definitions are only valid inside this group but vanish outside this group. So in a way, every loop of \foreach resets the variables to their original setting. In your example \from becomes A again.
• You can use the [remember=<foreach variable> as <another macro name] key to keep the previous value in the memory. Hence the definition becomes immune to group enter/exit problems.
• TikZ \foreach understands a,...,z,A,...,Z as a repeating array. So in your example there is an easy way out.
• minimal class is not meant for mortals like us. It's more of a testing environment for TeXnicians (well I'm really approximating here) instead you can use article or standalone.

First, the expansion,grouping etc. solution

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, automata}

\begin{document}
\begin{tikzpicture}

\node[initial,state]    (A)                    {A};
\node[state]            (B) [below right of=A]       {B};
\node[state]            (C) [right of=B]       {C};
\node[state, accepting] (D) [above right of=C]       {D};

\newcommand\from{A}
\foreach \too in {B, C, D}
{
\path (\from) edge [-] (\too);
\xdef\from{\too}
}
\end{tikzpicture}
\end{document}


The bottom line is due to my poor image cropping. And here is a remember solution.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, automata}

\begin{document}
\begin{tikzpicture}

\node[initial,state]    (A)                    {A};
\node[state]            (B) [below right of=A]       {B};
\node[state]            (C) [right of=B]       {C};
\node[state, accepting] (D) [above right of=C]       {D};

\foreach \x [remember=\x as \lastx (initially A)] in {B,...,D}{
% "initially" works only with ... I've recently found it but
% didn't have time to report it.
\path (\lastx) edge [-] (\x);
}
\end{tikzpicture}
\end{document}


You might be better off with variable names that are not existing (La)TeX macros. So I've changed to \x.

-
Since you're using xdef you don't need the expandafters, and you could use \global\let\from=\to –  Loop Space Nov 23 '12 at 0:05
@AndrewStacey Yes, I don't know what I was thinking. Fixing in a sec. It's all the weird QTikZ habits. –  percusse Nov 23 '12 at 0:06
very nice starting point, thanks. –  Mog Nov 23 '12 at 0:15
"initially" works only with ... I've recently found it. See here for more. –  Ahmed Musa Nov 23 '12 at 1:02
Yes, I mentioned it in the comments on that question too but didn't see that it's already reported so I don't need to do anything :) –  percusse Nov 23 '12 at 1:08