astoff / tikz-cd

Commutative diagrams with TikZ
44 stars 3 forks source link

feature request: support tikz-cd in amsmath environments #2

Closed FrankMittelbach closed 5 years ago

FrankMittelbach commented 5 years ago

While tikzcd environments can be used inside equation it is not possible to use them in align or gather or similar environments. For smaller diagrams that flexibility help if there is a need for equation numbers and texts or formulas accompanying the diagrams.

\documentclass{article}

\usepackage{amsmath,tikz-cd}
\begin{document}
\begin{align}
     T  
  & \text{ foo} \\
     T  
  & \text{ bar}
\end{align}

\begin{align}
  \begin{tikzcd}
     T \arrow[r,]  & X
  \end{tikzcd}
  & \text{ foo} \\
  \begin{tikzcd}
     T \arrow[r,]  & X
  \end{tikzcd}
  & \text{ bar} 
\end{align}
\end{document}
astoff commented 5 years ago

Do you have a suggestion for a solution? (Note also this issue affects all tikz matrices. if it can be solved at all, then this should be done directly there.)

This problem happens because, inside {tikzcd} (or any other tikz matrix), the character & is made active and points to the command \pgfmatrixnextcell. In some cases (the one you pointed out, beamer slides, etc.), the contents of {tikzcd} is read before the catcodes are appropriately set up.

One solution is to use the ampersand replacement option (see the pgf manual). If you really want to avoid this, then the best I can suggest for now (and this is explained in §3.4 of the package manual) is to add the following right before the beginning of the document:

\def\temp{&}
\catcode`&=\active
\let&=\temp

Of course, this may break other packages.

FrankMittelbach commented 5 years ago

Upfront I don't have a suggestion, no. I feared it might be due to changing the catcode which then rules out packages that read their env bodies before processing them.

Long term I think LaTeX should get rid of using the default catcode of & but make it active by default in the way you suggest, but as you say this is likely to break a number of packages at least initially. I guess we have to see if this feasible despite that.