# How to have tikzcd inside align?

With the gathered subsidiary environment, the code

\newcommand{\id}{\mathrm{id}}

$\begin{gathered} \text{if g is a left-inverse of f, then } \begin{tikzcd} A\ar[r, "f"]\ar[rr, "\id_A"', bend right = 30] & B\ar[r, "g"] & A \end{tikzcd} \text{ commutes; and,}\\ \text{if g is a right-inverse of f, then } \begin{tikzcd} B \ar[r, "g"]\ar[rr, "\id_B", bend right = 30] & A\ar[r, "f"] & B \end{tikzcd} \text{ commutes.} \end{gathered}$


Now, I wish to use align the sentences at the commutative diagrams. I try the following using the align* environment:

\begin{align*}
\text{if $g$ is a left-inverse of $f$, then }
& \begin{gathered}[c]
\begin{tikzcd}
A\ar[r, "f"]\ar[rr, "\id_A"', bend right = 30] & B\ar[r, "g"] & A
\end{tikzcd}
\end{gathered}
\text{ commutes; and,}\\
\text{if $g$ is a right-inverse of $f$, then } &
& \begin{tikzcd}
B \ar[r, "g"]\ar[rr, "\id_B", bend right = 30] & A\ar[r, "f"] & B
\end{tikzcd}
\text{ commutes.}
\end{align*}


But the ampersands of align* and tikzcd seem to get mixed up. I get the following error messages:

1. Package pgf Error: Single ampersand used with wrong catcode. \end{align*}
2. Package pgf Error: No shape named tikz@f@6-1-3' is known. I think the culprit is a tikzcd arrow in cell 1-1.
3. Package pgf Error: Single ampersand used with wrong catcode. \end{align*}
4. Package pgf Error: No shape named tikz@f@7-1-3' is known. I think the culprit is a tikzcd arrow in cell 1-1.
5. Package pgf Error: Single ampersand used with wrong catcode. \end{align*}
6. Package pgf Error: No shape named tikz@f@8-1-3' is known. I think the culprit is a tikzcd arrow in cell 1-1.
7. Package pgf Error: Single ampersand used with wrong catcode. \end{align*}
8. Package pgf Error: No shape named tikz@f@9-1-3' is known. I think the culprit is a tikzcd arrow in cell 1-1. Overfull \hbox (66.04643pt too wide) detected

How can I get around this?

• See section 3.4 in the tikz-cd manual. Inside align & has special meaning other than the one used in tikzcd so they clash. Apr 5 at 13:46
• I'm not sure what's the purpose of using $...$ and gathered here. Or, worse, align* Apr 5 at 13:55
• why are you doing this in a math alignment? simply use a tabular or in fact I wouldn't align at all, and just use paragraphs. Apr 5 at 13:59
• @egreg I am using align* so that I the lower diagram is exactly below the upper one. And I used gathered so that I could break the first line after "and;". I couldn't use $...$ here for this. And \beging{tikzcd} ... \end{tikzcd} will not be centered if I don't use a super-environment like $...$.
– Atom
Apr 5 at 14:22
• @DavidCarlisle Thanks! tabular gave me what I wanted!
– Atom
Apr 5 at 14:31

As mention @David Carlisle in his comment, use of simple table can be solution for your problem:

\documentclass{article}
\usepackage{amsmath}
\newtheorem{theorem}{Theorem}
\newcommand{\id}{\mathrm{id}}
\usepackage{tikz-cd}

\begin{document}

\begin{theorem}
Suppose something about $f$ and $g$:

\begin{tabular}{@{} lcl @{}}
if $g$ is a left-inverse of $f$, then
&   \begin{tikzcd}
A\ar[r, "f"]\ar[rr, "\id_A"', bend right = 30] & B\ar[r, "g"] & A
\end{tikzcd}    &   commutes;   \\
if $g$ is a right-inverse of $f$, then
&   \begin{tikzcd}
B \ar[r, "g"]\ar[rr, "\id_B"', bend right = 30] & A\ar[r, "f"] & B
\end{tikzcd}    & commutes.
\end{tabular}
\end{theorem}

\end{document} • @DavidCarlisle, thank you very much! Sorry, I should wait with mine little a bit ... Apr 5 at 15:14
• so long as egreg doesn't get the tick, all is well:-) Apr 5 at 15:18

You would need ampersand replacement=\& in the options to \begin{tikzcd} and \& instead of & for separating cells.

On the other hand, I can't see the need for a math display here: you want to present two facts (actually two implications), so use a list.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}

\newtheorem{theorem}{Theorem}

\newcommand{\id}{\mathrm{id}}

\begin{document}

\begin{theorem}
Suppose something about $f$ and $g$;
\begin{itemize}
\item if $g$ is a left-inverse of $f$, then
$\begin{gathered}\begin{tikzcd} A\ar[r, "f"]\ar[rr, "\id_A"', bend right = 30] & B\ar[r, "g"] & A \end{tikzcd}\end{gathered}$
commutes;

\item if $g$ is a right-inverse of $f$, then
$\begin{gathered}\begin{tikzcd} B \ar[r, "g"]\ar[rr, "\id_B"', bend right = 30] & A\ar[r, "f"] & B \end{tikzcd}\end{gathered}$
commutes.
\end{itemize}
\end{theorem}

\end{document} It wouldn't be difficult to align the two diagrams, by measuring the words “left” and “right”:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}

\newtheorem{theorem}{Theorem}

\newcommand{\id}{\mathrm{id}}

\begin{document}

\begin{theorem}
Suppose something about $f$ and $g$;
\begin{itemize}
\settowidth{\dimen0}{left}
\settowidth{\dimen2}{right}

\item if $g$ is a left-inverse of $f$, then\hspace{\dimen2}
$\begin{gathered}\begin{tikzcd} A\ar[r, "f"]\ar[rr, "\id_A"', bend right = 30] & B\ar[r, "g"] & A \end{tikzcd}\end{gathered}$
commutes;

\item if $g$ is a right-inverse of $f$, then
$\begin{gathered}\begin{tikzcd} B \ar[r, "g"]\ar[rr, "\id_B"', bend right = 30] & A\ar[r, "f"] & B \end{tikzcd}\end{gathered}$
commutes.
\end{itemize}
\end{theorem}

\end{document} However, I see no real reason for the alignment. To the contrary, having them in different positions underlines that they illustrate distinct properties.