1

Consider a commutative diagram such as

\documentclass{scrartcl}
\usepackage{mathtools, tikz-cd}
\begin{document}
    \begin{equation}
        \begin{tikzcd}
            0 \rar & A \dar \rar & B \dar \rar & C \dar \rar & 0 &[3em] \text{(\textit{i})}\\
            0 \rar & A' \rar & B' \rar & C' \rar & 0 & \text{(\textit{ii})}
        \end{tikzcd}
    \end{equation}
\end{document}

enter image description here

As you can see, I'd like to add labels to the rows of the diagram. Now, you'll agree that this solution is rather ugly. Is it possible to add labels to the rows that appear at the margin; i.e., instead of what you see, I'd like to have, e.g., enter image description here Which I produced using

\documentclass{scrartcl}
\usepackage{mathtools, amsmath, tikz-cd}
\tikzset{every picture/.append style={remember picture}}
\begin{document}
    \begin{gather}
        \begin{tikzcd}[ampersand replacement=\&]
            0 \rar \& |[alias=A]|  A \rar \& B \rar \& C \rar \& 0
        \end{tikzcd}\\
        \begin{tikzcd}[ampersand replacement=\&]
            0 \rar \& A' \ar[from=A] \rar \& B' \rar \& C' \rar \& 0 
        \end{tikzcd}
    \end{gather}
\end{document}

which is certainly not the way to go, since it is not particularly stable, and the proper alignment of the cells, which I was too lazy fore here, will be a mess.

3
  • Unrelated: your use of \text in the first example makes no sense at all. \text should never be used to make something non-italic, because that is not what \text does.
    – daleif
    Mar 9, 2022 at 10:53
  • @daleif I always thought \text is for putting text in math, and a label is certainly closer to text than to math. And who tells you that I don't want the labels match the surrounding text shape? :P But yes, that's not the point of my question.
    – Bubaya
    Mar 9, 2022 at 13:14
  • I only not it becase a lot of users seems to think that this is the right tool to write something update in math mode and as you mention it is not.
    – daleif
    Mar 9, 2022 at 13:38

3 Answers 3

2

You don't really need tikz-cd for this kind of diagrams. Here is a way to do it with alignatand \rotatebox.

Unrelated: you don't have to load amsmath when you load mathtools, since the latter does it for you.

\documentclass{scrartcl}
\usepackage{mathtools}

\begin{document}

    \begin{alignat}{4}
            0 \xrightarrow{\hskip 2em}{} & A & \xrightarrow{\hskip 2em} {}& B & \xrightarrow{\hskip 2em}{} & C \xrightarrow{\hskip 2em} 0 \\[-1ex]
                             & \,\rotatebox[origin=c]{-90}{${} \xrightarrow{\hskip 2em}{}$} & & \,\rotatebox[origin=c]{-90}{${} \xrightarrow{\hskip 2em}{}$} & & \,\rotatebox[origin=c]{-90}{${} \xrightarrow{\hskip 2em}{}$} \notag \\[-1ex]
            0\xrightarrow{\hskip 2em}{} & A' &\xrightarrow{\hskip 2em} {}& B' & \xrightarrow{\hskip 2em}{} & C' \xrightarrow{\hskip 2em} 0
    \end{alignat}

\end{document} 

enter image description here

4
  • Sure, but what about more complicated diagrams?
    – Bubaya
    Mar 9, 2022 at 17:09
  • @Bubaya: What do you mean exactly with ‘more complicated diagrams’? One I think of is the Snake lemma (a tool for Homological Algebra), which I make with pstricks preferably.
    – Bernard
    Mar 10, 2022 at 9:16
  • I'd say that the solution has several other problems; what do you do if the lengths of the entries largely differ? Then you'd have to adjust all the arrow lengths until it looks okay. How does pstricks come to help here?
    – Bubaya
    Mar 10, 2022 at 14:29
  • @Bubaya: for the example I gave, I mainly thought of the S-shaped arrow form the last non-zero term in the first row to the first non-zero term in the second row. This being said, in case of entries with different lengths, you can use the psmatrix environment. Each entry is a node and you can use node connections between these entries.
    – Bernard
    Mar 10, 2022 at 16:20
2

If the problem of your second example is only the alignment, you can fix it.

This way, there is no problem when you reference the lines:

\documentclass{scrartcl}
\usepackage{mathtools, tikz-cd}
\tikzset{every picture/.append style={remember picture}}
\begin{document}
If the problem of your second example is only the alignment, you can fix it. 

This way, there is no problem referencing the first
\eqref{eq:first} or the second line \eqref{eq:second}:

\begin{gather}
    \begin{tikzcd}[ampersand replacement=\&,
    every cell/.append style={text centered}]
        0 \rar \& |[alias=A, text width=width("$A'$")]|A \rar \& |[text width=width("$B'$")]|B \rar \& |[text width=width("$C'$")]|C \rar \& 0\label{eq:first}
    \end{tikzcd}\\[5ex]
    \begin{tikzcd}[overlay, ampersand replacement=\&]
        0 \rar \& A' \ar[from=A] \rar \& B' \rar \& C' \rar \& 0\label{eq:second} 
    \end{tikzcd}
\end{gather}
\end{document}

enter image description here

1

This is more of a proof-of-concept since I'm not very familiar with the internals of AMSMath.

Note:

  • This needs a node in column 1 for each row.
  • If you shift this node up or down, the equation number will move with it vertically.
  • The macros \tag and \nonumber won't work.
  • The equation number will not be shifted down when the row is too long.
  • Don't write anything before or after the TikZ-CD. The placement of the equation numbers are dependent of the diagram being in the horizontal middle of the line.
  • Hyperref? Cleveref? Maybe.

Code

\documentclass{scrartcl}
\usepackage{mathtools, tikz-cd}
\makeatletter
\tikzcdset{
  eqno per line/.style={
    /tikz/column 1/.append code=%
      \global\pgfutil@namelet{tikzcd@rowlabel@\the\pgfmatrixcurrentrow}
                             {pgfutil@empty},
    /tikz/execute at end picture={%
      \pgfmathloop % simpler than \foreach
        \node[overlay,path only,anchor=base east,inner sep=+0pt,outer sep=+0pt](@)
        at(.5\linewidth,0|-\tikzcdmatrixname-\pgfmathcounter-1.base){%
          \@eqnswtrue\make@display@tag
          \expandafter\pgfutil@ifxempty
            \csname tikzcd@rowlabel@\pgfmathcounter\endcsname{}{%
            \expandafter\expandafter\expandafter\ltx@label
            \expandafter\expandafter\expandafter{%
              \csname tikzcd@rowlabel@\pgfmathcounter\endcsname}}};%
      \ifnum\pgfmathcounter<\pgfmatrixcurrentrow
      \repeatpgfmathloop}},
  every matrix/.append code={%
    \def\label##1{%
      \expandafter\xdef
        \csname tikzcd@rowlabel@\the\pgfmatrixcurrentrow\endcsname{##1}}}}
\makeatother
\newenvironment*{tikzcdeq}[1][]
  {\begin{equation*}\tikzcd[eqno per line,#1]}
  {\endtikzcd\end{equation*}}
\begin{document}
Lorem \ref{label3}
\begin{equation}
c^2 = a^2 + b^2
\end{equation}
ipsum \ref{label1}
\begin{equation*}
\begin{tikzcd}[eqno per line]
  0 \rar & A \dar \rar & B \dar \rar & C \dar \rar & 0 \label{label1} \\
  0 \rar & A' \rar     & B' \rar     & C' \rar     & 0 \label{label2}
\end{tikzcd}
\end{equation*}
dolor \eqref{label2}
\begin{equation}
E = mc^2
\end{equation}
sit \eqref{label4}
\begin{tikzcdeq}
  0 \rar & A \dar \rar & B \dar \rar & C \dar \rar & 0 \label{label3} \\
  0 \rar & A' \rar     & B' \rar     & C' \rar     & 0 \label{label4}
\end{tikzcdeq}
amet.
\end{document}

Output

enter image description here

1
  • If this doesn't work and two diagrams are really needed, we can measure the max width of each column and adjust the column widths (not the nodes') so that the vertical nodes align again. Mar 2 at 2:45

You must log in to answer this question.

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