2

This question already has an answer here:

My LaTeX document looks like this:

\documentclass{article}    
\usepackage{tikz-cd}

\newcommand{\commSq}[8]
{
    \begin{tikzcd}
        #1 \ar[r, "#2"] \ar[d, "#6"] & #3 \ar[d,"#4"] \\
        #7 \ar[r, "#8"] & #5
    \end{tikzcd}
}

\begin{document}
%1
\begin{tikzcd}
    A \ar[r, "f"] \ar[d, "g"] & B \ar[d,"h"] \\
    C \ar[r, "i"] & D
\end{tikzcd}
%2
\commSq{A}{f}{B}{h}{D}{g}{C}{i}
\end{document}

I figured that the part the code below %1 and the code below %2 do the same thing. However, using the command \commSq instead yields the error "Package pgf Error: Single ampersand used with wrong catcode. [...]" and I get this output:

enter image description here

What am I doing wrong here?

marked as duplicate by moewe, Community Aug 14 '15 at 9:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SX! That is a very interesting question with a very clear and concise MWE. Thank you very much. – moewe Aug 14 '15 at 9:29
  • 2
    “Single ampersand used with wrong catcode” error using tikz matrix in beamer made me think that using \pgfmatrixnextcell instead of & might work, and so it does. Hence, your question pretty much seems to be a duplicate of Problem with defining shortcuts for TikZ matrices.. – moewe Aug 14 '15 at 9:30
  • @moewe Thank you! That works. I'm still confused though, about what user-defined commands have to do with tikz-matrices. – Stefan Perko Aug 14 '15 at 9:50
  • Well the OP in the other question also had problems with a \newcommand containing TikZ code. They did not use the tikz-cd package, but tikz-cd uses TikZ, so the issue is inherited, if you will. – moewe Aug 14 '15 at 9:58
  • @moewe Yes, I understand. – Stefan Perko Aug 14 '15 at 9:59