3

I'm trying to use a foreach loop to draw a whole bunch of arrows in a tikzcd environment by using the foreach loop to specify coordinates but I can't get it to compile. Here's a minimal example of what I'm trying:

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}
  A & B & C \\
  \foreach \x in {2,3}{
    \arrow[from=1-1, to=1-\x]
  }
\end{tikzcd}

\end{document}

Which should appear as an arrow from A to B and another from A to C. Instead I'm recieving this error:

ERROR: Undefined control sequence.

--- TeX said ---

pgf@sh@ns@\tikzcdmatrixname -1-\x

l.12 \end{tikzcd}

I think I could get it to work in a plain tikz environment but I'd like to use tikzcd if possible. Thanks for any help.

  • 1
    on your way it is not possible in tikz-cd. compare your approach with standard tikz-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 option r. check the manual, if there is mentioned possibilities of what you like to have. – Zarko Apr 25 at 7:47
  • If I replace the code in the tikzcd environment with just 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. – aardvark Apr 25 at 7:56
  • do you try to direct arrow from the first cell directly to the last? frankly said, i don't see any benefits with writing arrows on the your way. good luck in searching for solution which you like to obtain :-) – Zarko Apr 25 at 8:02
  • Yes, I should have said an arrow from A to B and an arrow from A to C. What I'm trying to do in the end is draw a cube diagram with 4x4x4 nodes made of lots of smaller cubes. There would be 144 arrows so I'd quite like to use foreach so I don't have to write them all out. Thanks :) – aardvark Apr 25 at 8:11
2

Try with a \draw in execute at end picture:

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}[
    every matrix/.append style={name=mymatrix},
    execute at end picture={
        \foreach \x in {2,3}{
            \draw[->] (mymatrix-1-1) -- (mymatrix-1-\x);
        }
    }
]
  A & B & C \\
\end{tikzcd}

And Zarko's example:

\begin{tikzcd}[
    every matrix/.append style={name=mymatrix},
    execute at end picture={
         \foreach \x [count=\xx] in {2,3}{ \draw[->] (mymatrix-1-\xx) -- (mymatrix-1-\x); 
         }
    }
]
  A & B & C \\
\end{tikzcd}
\end{document}

enter image description here

  • i'm glad to see, that i was wrong in my comment! to your example i would add the following example: ` \foreach \x [count=\xx] in {2,3}{ \draw[->] (mymatrix-1-\xx) -- (mymatrix-1-\x); } :-) +1 for answer – Zarko Apr 25 at 8:39
  • thanks for your answer - this worked for me – aardvark Apr 25 at 8:43
  • i store your answer in my barn of tikz-cd solutions. i very liked it! – Zarko Apr 25 at 8:46
  • @Zarko What an honor! :) Thanks – CarLaTeX Apr 25 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.