# Numbering rows in array environment

I would like to (use the \label - \ref mechanism to) number as equations the first and third rows in the following diagram:

\documentclass[10pt]{article}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx}

\begin{document}
$$\begin{array}{cccccccccc} 1 & \longrightarrow &A & \longrightarrow & B \times C & \overset{\pi}\longrightarrow & D & \longrightarrow & 1 \phantom{\, ,} \\ &&\rotatebox[origin=c]{90}{\leqslant}&&\rotatebox[origin=c]{90}{\leqslant}&&\rotatebox[origin=c]{90}{\leqslant}\\ 1 & \longrightarrow &a \cap b & \longrightarrow & c & \longrightarrow & c \pi & \longrightarrow & 1 \, , \end{array}$$
\end{document}


Although I have done some research, none of the solutions found (using align, alignat, etc.) worked well with my diagram. Do you have any suggestions? Concretely, which is the most suitable environment to use, and how?

• @LaRiFaRi -- I guess he means the rows in fact. If you compile the example it is rather clear. – Ruben Apr 15 '16 at 10:09
• Do you need to refer to those numbers using the \label-\ref-mechanism? – Ruben Apr 15 '16 at 10:11
• I mean rows indeed, I'd like to refer the two short exact sequences (rows) independently. – suitangi Apr 15 '16 at 10:12
• Yes @Ruben, should I edit the question accordingly? – suitangi Apr 15 '16 at 10:15
• BTW: you might want to use something like tikz-cd for commutative disagrams instead of relying on array, much more featues. – daleif Apr 15 '16 at 10:18

As you requested here is a solution usind tikz-cd. It uses a custom labeling macro \cdlabel that throws out the current line of the diagram and generates a label named by the first argument. You can \ref it as usual.

\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{cd}

\newcounter{cdrow}
\newcommand\cdlabel[1]{%
\refstepcounter{cdrow}
\label{#1}
(\thecdrow)
}

\begin{document}
\begin{tikzcd}
1 \arrow[r] & A \arrow[r] & B \times C \arrow[r, "\pi"] & D \arrow[r] & 1 & \cdlabel{eq1}\\
& \rotatebox[origin=c]{90}{$\leqslant$} & \rotatebox[origin=c]{90}{$\leqslant$} & \rotatebox[origin=c]{90}{$\leqslant$} && \\
1 \arrow[r] & a \cap b  \arrow[r] & c \arrow[r] & c\pi \arrow[r] & 1 & \cdlabel{eq2}
\end{tikzcd}
%
%1st line: eq. \ref{eq1}, 2nd line: eq. \ref{eq2}.
\end{document}


• Great! Now my doubt is which answer accept ;). Maybe you want to join both answers in a single one. – suitangi Apr 15 '16 at 18:00
• @suitangi, if you find it helpful you can upvote it. The solutions are quite different. So, I'd say we'll keep them separate. Regarding your question which to accept: The tikz-cd approach is more "diagram-ish" and a little bit cleaner overall. The alignat approach involves more hacking but sticks closer to your original idea of using some kind of ams array environment. And consider the user interface of the labeling mechanism. There are downsides and pitfalls in both solutions. I hope that helps you to decide. – Ruben Apr 15 '16 at 18:11

You can use alignat (in which the rows get numbered by default) instead of array.

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{graphicx}

\begin{document}
\begin{alignat}{10}
1 & \longrightarrow & A & \longrightarrow & B \times C & \overset{\pi}\longrightarrow & D & \longrightarrow & 1 \phantom{\, ,} \label{eq1}\\
&& \rotatebox[origin=c]{90}{$\leqslant$} && \rotatebox[origin=c]{90}{$\leqslant$} && \rotatebox[origin=c]{90}{$\leqslant$}   \nonumber\\
1 & \longrightarrow &a \cap b  & \longrightarrow & c & \longrightarrow & c \pi & \longrightarrow & 1 \, ,                      \label{eq2}
\end{alignat}
\end{document}


However, the default settings in alignat for the positiong of the cell content were designed for equations (resp. parts of equations) and don't match the requirements for your diagram or for diagrams in general (see the above figure).

To fix this you can use a custom version of alignat where you modify the positioning and spacing. I called this environment labeledcd. Note that it takes one argument, which specifies the number of columns as in alignat. (The argument in fact gets directly passed to alignat.)

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{graphicx}

\makeatletter
\newenvironment{labeledcd}[1]{%
\def\align@preamble{%
&\hfil
\strut@
\setboxz@h{\@lign$\m@th\displaystyle{####}$}%
\ifmeasuring@\savefieldlength@\fi
\set@field
\hfil
\tabskip\tabcolsep
&\setboxz@h{\@lign$\m@th\displaystyle{{}####}$}%
\ifmeasuring@\savefieldlength@\fi
\set@field
\hfil
\tabskip\tabcolsep
}%
\alignat{#1}
}{%
\endalignat
}
\makeatother

\begin{document}
\begin{labeledcd}{10}
1 & \longrightarrow & A & \longrightarrow & B \times C & \overset{\pi}\longrightarrow & D & \longrightarrow & 1              \label{eq1}\\
&& \rotatebox[origin=c]{90}{$\leqslant$} && \rotatebox[origin=c]{90}{$\leqslant$} && \rotatebox[origin=c]{90}{$\leqslant$} \nonumber\\
1 & \longrightarrow &a \cap b  & \longrightarrow & c & \longrightarrow & c \pi & \longrightarrow & 1                         \label{eq2}
\end{labeledcd}
1st line: eq. \ref{eq1}, 2nd line: eq. \ref{eq2}.
\end{document}


• +1 thank you, this works fine. I'd like to know, though, whether this can be accomplished without such kind of customizing. Or should I understand that what I am asking for is not implemented in any standard Latex package? What about using tikz-cd as @daleif suggested? – suitangi Apr 15 '16 at 15:33
• @suitangi, as a matter of fact, what you're asking for is implemented in standard LaTeX -- as far as amsmath is considered so (I would do so). alignat does exactly what you are looking for. The longish overhead in the preamble is only added to accomplish the right positiong and spacing. (I'll add a inbetween step in my solution) But, the basic feature is alredy there. – Ruben Apr 15 '16 at 17:33
• @suitangi -- tikz-cd is the package of choice when it comes to commutative diagrams. But(!): There is no genuine support for the referencable line-numbering feauture you requested. You can build it in. I'll add another answer for this. Stay tuned... – Ruben Apr 15 '16 at 17:38