# Tikz, graphdrawing: How to use \foreach variables to set edge node labels?

I wanna create and label edges using a \foreach loop to avoid having to write every single node/edge when creating large graphs. It works fine when I use it to create nodes, but when I try to label the edges using the variable it stops working.

I put here an example of what I wanted to draw and then how I was trying to write it. I included both my attempt using the quotes notation and the usual node[]{} notation. I get an undefined control sequence error when I put those \x where the edge label should be.

I'm using graphdrawing with layered layouts to draw the graph, and I'd prefer a solution where I can keep using it, or at least something equivalent, to automatically position the nodes and edges in a nice way.

% !TeX program = lualatex

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, calc, quotes}
\usegdlibrary{layered}

\begin{document}

What I want in the end:

\begin{tikzpicture}[layered layout, nodes={circle,draw},level pre sep=1cm,every edge quotes/.style={auto,font=\footnotesize, draw=none, inner sep=0.1em},  horizontal=a to b ]

\node  (a) {a};
\node  (b) {b};

\foreach \x in {1,2,3}
{
\node (a\x) {$a^\x$};

}

\draw (a) edge ["1" ,->] (a1);
\draw (a1) edge [->] node[every edge quotes]{$a^1$}  (b);
\draw (a) edge ["2" ,->] (a2);
\draw (a2) edge [->] node[every edge quotes]{$a^2$}  (b);
\draw (a) edge ["3" ,->] (a3);
\draw (a3) edge [->] node[every edge quotes]{$a^3$}  (b);

\end{tikzpicture}

How I tried to write it:

\begin{tikzpicture}[layered layout, nodes={circle,draw},level pre sep=1cm,every edge quotes/.style={auto,font=\footnotesize, draw=none, inner sep=0.1em},  horizontal=a to b ]

\node  (a) {a};
\node  (b) {b};

\foreach \x in {1,2,3}
{
\node (a\x) {$a^\x$};
\draw (a) edge ["\x" ,->] (a\x);
\draw (a\x) edge [->] node[every edge quotes]{$a^{\x}$}  (b);
}

\end{tikzpicture}
\end{document}


Welcome to the expanding universe! These are expansion issues. One can fix them a la pgfplots.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, calc, quotes}
\usegdlibrary{layered}

\begin{document}

What I want in the end:

\begin{tikzpicture}[layered layout, nodes={circle,draw},level pre sep=1cm,every edge quotes/.style={auto,font=\footnotesize, draw=none, inner sep=0.1em},  horizontal=a to b ]

\node  (a) {a};
\node  (b) {b};

\foreach \x in {1,2,3}
{
\node (a\x) {$a^\x$};

}

\draw (a) edge ["1" ,->] (a1);
\draw (a1) edge [->] node[every edge quotes]{$a^1$}  (b);
\draw (a) edge ["2" ,->] (a2);
\draw (a2) edge [->] node[every edge quotes]{$a^2$}  (b);
\draw (a) edge ["3" ,->] (a3);
\draw (a3) edge [->] node[every edge quotes]{$a^3$}  (b);

\end{tikzpicture}

How I tried to write it:

\begin{tikzpicture}[layered layout, nodes={circle,draw},level pre sep=1cm,every edge quotes/.style={auto,font=\footnotesize, draw=none, inner sep=0.1em},  horizontal=a to b ]

\node  (a) {a};
\node  (b) {b};

\foreach \X in {1,2,3}
{
\edef\temp{\noexpand\node (a\X) {$a^\X$};}
\temp
\edef\temp{\noexpand\draw (a) edge ["\X" ,->] (a\X);}
\temp
\edef\temp{\noexpand\draw (a\X)
edge [->] node[every edge quotes]{$a^{\X}$}  (b);}
\temp
}

\end{tikzpicture}
\end{document}


• Thanks! It works perfectly in the example! I'll test it on my code. What was going on there? I need to temporarily define a macro to force the variable \x to expand and then call that macro? Aug 8 '18 at 16:36
• @Filipe You're welcome! (You can of course write everything into a big \edef but please make sure not to forget the \noexpands in front of the \draws and \node.)
– user121799
Aug 8 '18 at 16:38