5

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}

This is what I get when I run the code above.

1 Answer 1

2

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}

enter image description here

2
  • 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?
    – Filipe
    Commented Aug 8, 2018 at 16:36
  • 1
    @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
    Commented Aug 8, 2018 at 16:38

You must log in to answer this question.

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