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}