Have tikzcd inside align

alignampersandtikz-cd

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}\]

produces:
enter image description here

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?

Best Answer

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}

enter image description here

Related Question