Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I’m using tikz-cd for diagrams, and have a medium-sized diagram that I want to re-use several times. However, when I try using a definition like:

\newcommand{\myarrow}{$\begin{tikzcd} A \ar{r} & B \end{tikzcd}$

I get an error:

ERROR: Package pgfbasematrix Error: Single ampersand used with wrong catcode.

Is it possible to put a tikz-cd diagram into a defined command somehow?

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

It's a known problem with TikZ matrices:

\documentclass{article}
\usepackage{tikz-cd}
\newcommand{\myarrow}{%
  $\begin{tikzcd}[ampersand replacement=\&] A \ar{r} \& B \end{tikzcd}$}
\begin{document}
\myarrow
\end{document}

The problem is that tikzpicture wants to do some juggling with the meaning of &, but it's impossible if the & has been found as the replacement text of a macro, as in your original definition. Happily, it provides the

ampersand replacement=...

key that allows to define some command as substitute for &, avoiding the need to do the jugglings.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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