3
\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 enter image description here 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.

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

2 Answers 2

2

Cells in a tikzcd environment are expected to be in math mode, so your definition of \dc should omit the $ signs.

\documentclass{article}
\usepackage{amsmath,amssymb,amsthm,graphicx}
\usepackage{tikz-cd}
\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 prefer always using braces around arguments, although it's not strictly required by the syntax. In this case, however, I don't see any real advantage over typing

B_{-2,2}

enter image description here

1
  • I was experimenting with syntax and \dc was defined with another purpose in mind, but do plan(need) to use user-defined commands within tikzcd. Aug 9, 2015 at 15:44
0

If I understand your comment below egreg's answer correctly, you want to have the possibility to write your command outside of math environments as well. egreg already explained what is happening with your MWE. If you just add an \ensuremath to his solution, you will be able to use this command where ever you like.

% arara: pdflatex

\documentclass{article}
\usepackage{tikz-cd}
\newcommand{\dc}[3]{\ensuremath{#1_{#2,#3}}}

\begin{document}
In text it would look like \dc{B}{1}{2}. In inline math it looks the same with $\dc{B}{1}{2}$. Display math looks like this:
\[\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .