This is a classical expansion problem.
The \arrow
macro just takes everything between [
and ]
and stores it as-is in a long list which gets processed much later. At that point \x
isn't 2
or 3
anymore.
I provide three solutions:
An \arrowx[<opt1>][<opt2>]
macro that expands <opt1>
before it processes it. The <opt2>
will get forwarded as-is and not expanded. That will only be really necessary when you have to use fragile node content.
The \arloop{<opt>}{<list>}
macro uses a \foreach
loop (i.e. the ...
syntax is allowed) over <list>
to construct \arrow[<opt>]
statements where #1
in <opt>
is replaced by the elements of your list.
The end arrows
key that executes its argument directly (after the nodes have been constructed, of course) when there's no expansion issue anymore.
(The PGFPlots package also provides various ungrouped and/or invoking loop constructs but these seem easy enough to implement just for TikZ-CD.)
Code
\documentclass{standalone}
\usepackage{tikz-cd}
\makeatletter
\tikzcdset{every diagram/.append code=\let\arrowx\tikzcd@arrowx\let\arloop\tikzcd@arloop}
\NewDocumentCommand{\tikzcd@arrowx}{O{}O{}}{%
\edef\tikzcd@temp{#1}%
\expandafter\arrow\expandafter[\expandafter{\tikzcd@temp,#2}]}
\NewDocumentCommand{\tikzcd@arloop}{mm}{%
\pgfkeys{/utils/temp/.code=\arrow[{#1}],/utils/temp/.list={#2}}}
\NewDocumentCommand{\tikzcd@arrowlate}{O{}}{%
\path[{/tikz/commutative diagrams/.cd,every arrow,#1}]
(\tikzcd@ar@start\tikzcd@startanchor)to(\tikzcd@ar@target\tikzcd@endanchor);}
\tikzcdset{
end arrows/.style={
/tikz/commutative diagrams/every matrix/.append style={
append after command=
\pgfextra{\pgfutil@g@addto@macro\tikzcd@savedpaths{\let\arrow\tikzcd@arrowlate#1}}}}}
\makeatother
\begin{document}
\begin{tikzcd}
A & B & C
\foreach \x in {2, 3}{
\arrowx[from=1-1, to=1-\x, bend right]
}
\end{tikzcd}
\begin{tikzcd}
A & B & C
\arloop{from=1-1, to=1-#1, bend left}{2,3}
\end{tikzcd}
\begin{tikzcd}[
end arrows={
\foreach \x in {2, 3}{
\arrow[from=1-1, to=1-\x, bend right]
}
}
]
A & B & C
\end{tikzcd}
\end{document}
Output
tikz-cd
. compare your approach with standardtikz-cd
syntax:A \ar[r]& B\ar[r] & C
and you will see, that arrows there is modified (starting point is defined with cell where its command is placed and end in cell defined by optionr
. check the manual, if there is mentioned possibilities of what you like to have.A & B \\
followed by\arrow[from=1-1, to=1-2]
then it compiles correctly. The problem appears to be in putting the variable\x
in as a coordinate.