# Using user defined commands within Tikzcd

\documentclass{article}
\usepackage{amsmath,amscd,amssymb,amsthm,graphicx,amsxtra}
\usepackage{verbatim}
\usepackage{tikz-cd,xparse}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing}
\newcommand {\dc}[3]{$#1_{#2,#3}$}
\begin{document}
\begin{tikzcd}
{} \dc B {-2} 2 \arrow[dotted]{l}  &\dc B {-1} 2 \arrow{l}  &dc B 0 2 \arrow{l}  &\dc B 1 2 \arrow{l}  & \dc B 2 2 \arrow{l} &{} \arrow[dotted]{l}\\
\end{tikzcd}
\end{document}


I wrote this and several variants of it (using braces, with no space, with 'ampersand replacement' and so on) to produce and sadly none of it worked. I know I can get around it by using (_) but would like to know why my code wasn't working. I need to produce several of such images and objects, this (for me) was the first step towards defining a newcommand but it failed miserably. The error which keep appearing is

Package pgfbasematrix Error: Single ampersand used with wrong catcode.

• Welcome to TeX.SX! If I use your code I get an error Missing } inserted, complaining after B {-2} 2... – user31729 Aug 9 '15 at 8:30

$\dc{B}{2}{3}$
Finally, you can use it in your commutative diagrams:

\begin{tikzcd}
\null
& \dc{B}{-2}{2} \arrow[dotted]{l}
& \dc{B}{-1}{2} \lar
& \dc{B}{0}{2} \lar
& \dc{B}{1}{2} \lar
& \dc{B}{2}{2} \lar
& \null \arrow[dotted]{l}
\end{tikzcd}
\end{document}