# Define a commutative diagram and reuse it

In a paper I am writing, I need to to draw double complex, which is a fairly complicated commutative diagram. I wrote it using tikzcd, and the code is the following

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{tikz-cd}
\newcommand{\s}{\mathscr{#1}}
\begin{document}
\begin{tikzcd}
& 0 \arrow{r}
& O_X \arrow{r}{\partial}\arrow{d}{i}
& \Omega^1_X \arrow{r}{\partial}\arrow{d}{i}
& \dots\arrow{r}{\partial}\arrow{d}{i}
& \Omega^n_X\arrow{r}\arrow{d}{i}
& 0 \\
& 0 \arrow{r}
& \s{A}\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \s{A}^{1,0}\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \dots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \s{A}^{n,0}\arrow{r}\arrow{d}{\bar{\partial}}
& 0 \\
& 0 \arrow{r}
& \s{A}^{0,1} \arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \s{A}^{1,1}\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \dots \arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \s{A}^{n,1}\arrow{r}\arrow{d}{\bar{\partial}}
& 0 \\
&
& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
& \\
& 0 \arrow{r}
& \s{A}^{0,n}\arrow{r}{\partial}
& \s{A}^{1,n}\arrow{r}{\partial}
& \dots\arrow{r}{\partial}
& \s{A}^{n,n}\arrow{r}{\partial}
& 0\\
\end{tikzcd}
\end{document}


Since I need to draw the same diagram multiple times, I thought that I could define a control sequence

\def\DoubleComplex{
"All the code you saw above"
}


When I try to compile it using

$\DoubleComplex$


I keep getting the following error message:

! Missing $inserted. <inserted text>$
l.234     \DoubleComplex


I wonder what is going on? And what would be a good way to reuse a commutative diagram?

• Welcome to TeX.SE. Please post documents, not just fragments. I could imagine however, that you need \protect\DoubleComplex, but that's a guess only. – user31729 Mar 18 '17 at 20:01
• I get ! Undefined control sequence. <recently read> \s if I try to make a document from your fragment – David Carlisle Mar 18 '17 at 20:10
• @DavidCarlisle Good catch. I forgot that I have some user defined newcommand. I reedited the question. Thanks – helloWorld Mar 20 '17 at 2:16

Tikz makes & an active character, which may lead to problems in certain contexts. Add [ampersand replacement=\&] to the tikzcd environment and use \& in the diagram instead of &.

I found another problem with your code: In the fourth row the seventh cell is empty, which results in a tikcd error (No shape named tikz@f@1-4-7 is known) because the destination for the arrow from the sixth cell is missing. It seems to be sufficient to add {} as contents of the empty cell. \documentclass{article}
\usepackage{tikz-cd}
\newcommand\s{\mathsf{#1}}
\makeatletter
\DeclareRobustCommand{\tvdots}{%
\vbox{\baselineskip4\p@\lineskiplimit\z@\kern0\p@\hbox{.}\hbox{.}\hbox{.}}}
\makeatother
\newcommand\DoubleComplex
{\begin{tikzcd}[ampersand replacement=\&]
\& 0 \arrow{r}
\& O_X \arrow{r}{\partial}\arrow{d}{i}
\& \Omega^1_X \arrow{r}{\partial}\arrow{d}{i}
\& \dots\arrow{r}{\partial}\arrow{d}{i}
\& \Omega^n_X\arrow{r}\arrow{d}{i}
\& 0 \\
\& 0 \arrow{r}
\& \s{A}\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \s{A}^{1,0}\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \dots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \s{A}^{n,0}\arrow{r}\arrow{d}{\bar{\partial}}
\& 0 \\
\& 0 \arrow{r}
\& \s{A}^{0,1} \arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \s{A}^{1,1}\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \dots \arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \s{A}^{n,1}\arrow{r}\arrow{d}{\bar{\partial}}
\& 0 \\
\&
\& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& \tvdots\arrow{r}{\partial}\arrow{d}{\bar{\partial}}
\& {}\\
\& 0 \arrow{r}
\& \s{A}^{0,n}\arrow{r}{\partial}
\& \s{A}^{1,n}\arrow{r}{\partial}
\& \dots\arrow{r}{\partial}
\& \s{A}^{n,n}\arrow{r}{\partial}
\& 0\\
\end{tikzcd}%
}
\begin{document}
First occurrence:
$\DoubleComplex$
Second occurrence:
$\DoubleComplex$
\end{document}

• Thank you so much for the detailed explanation. Good catch about the last arrow in the fourth row, it actually shouldn't be there. – helloWorld Mar 18 '17 at 21:12