# Multiple equation numbers for a tikz picture

I am attempting to draw two related exact sequences in the same tikz picture, for alignment purposes. I currently have wrapped the tikzpicture environment in an equation environment, so that both have the same number. Ideally, I would like to have separate equation numbers; is there a way to do this?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\newcommand\Z[2]{\ensuremath{\mathfrak  Z^{(#1)}_{#2}}}
\begin{document}
$$\begin{tikzpicture} [description/.style={fill=white,inner sep=2pt},baseline=(current bounding box.center)] \matrix (m) [matrix of math nodes, row sep=1em, column sep=1.5em, text height=1.5ex, text depth=0.25ex] { 0 & \Omega^k_X & \Z k k & \Z k {k-1} & \cdots & \Z k 1 & \Z k 0 & 0\phantom .\\ 0 & \Lambda^k \mathcal E^\vee & \Z k k & \Z k {k-1} & \cdots & \Z k 1 & \Z k 0 & 0.\\ }; \foreach \a in {2,3,...,8} { \pgfmathsubtract{\a}{1} \draw[->] (m-1-\pgfmathresult) -- (m-1-\a); \draw[->] (m-2-\pgfmathresult) -- (m-2-\a); }; \end{tikzpicture}$$
\end{document}

-
You shouldn't use \pgfmathresult in \draw commands. The TikZ commands use pgfmath internally and \pgfmathresult can be overwritten any time. Use e.g. \pgfmathsetmacro{\myresult}{\a-1}. –  Martin Scharrer Mar 30 '11 at 19:36
@Martin: Thanks, that's great, and brings up a related question you may have insight on: Referring to tikz matrix coordinates using pgf mathematics operations –  Josh Guffin Mar 30 '11 at 19:50

I guess that there are two ways to do this. Either to have two numbered equations (possibly in, say, an align environment), each with its own tikzpicture, and try to align the relevant parts of the tikzpictures, or to emulate the equation numbering inside the tikzpicture. I've chosen the latter approach here.

First, the result. The first two lines are your original exact sequence in the equation environment. The second two are where I've embedded the equation numbers inside the tikzpicture.

Now the code:

\documentclass{amsart}
\thispagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\newcommand\Z[2]{\ensuremath{\mathfrak  Z^{(#1)}_{#2}}}

% labeling, we assume that we are centred on the line
\makeatletter
\newlength{\tikz@label@eqnlen}
\newcommand{\tikzlabel}[2][]{%
\pgfgetlastxy{\tikz@label@x}{\tikz@label@y}%
\path (current bounding box.north east);
\pgfgetlastxy{\tikz@label@nex}{\tikz@label@ney}%
\path (current bounding box.north west);
\pgfgetlastxy{\tikz@label@nwx}{\tikz@label@nwy}%
\pgfmathsetlength{\tikz@label@eqnlen}{(\textwidth - (\tikz@label@nex - \tikz@label@nwx))/2 - (\tikz@label@nwx - \tikz@label@x)}
\begin{pgfinterruptboundingbox}
\path (\tikz@label@x,\tikz@label@y) ++(-\tikz@label@eqnlen,0)  node[anchor=west,inner sep=0pt,#1] {\refstepcounter{equation}(\theequation)};
\end{pgfinterruptboundingbox}
\def\@currentlabel{\theequation}
\ltx@label{#2}}
}

\makeatother

\begin{document}
$$\label{eq:orig} \begin{tikzpicture} [baseline=(current bounding box.center)] \matrix (m) [matrix of math nodes, row sep=1em, column sep=1.5em, text height=1.5ex, text depth=0.25ex] { 0 & \Omega^k_X & \Z k k & \Z k {k-1} & \cdots & \Z k 1 & \Z k 0 & 0\phantom .\\ 0 & \Lambda^k \mathcal E^\vee & \Z k k & \Z k {k-1} & \cdots & \Z k 1 & \Z k 0 & 0.\\ }; \foreach \a in {2,3,...,8} { \pgfmathsubtract{\a}{1} \draw[->] (m-1-\pgfmathresult) -- (m-1-\a); \draw[->] (m-2-\pgfmathresult) -- (m-2-\a); }; \end{tikzpicture}$$

\begin{equation*}
\begin{tikzpicture}
[baseline=(current bounding box.center)]
\matrix (m) [matrix of math nodes, row sep=1em, column sep=1.5em, text height=1.5ex, text depth=0.25ex]
{
0 & \Omega^k_X                & \Z k k & \Z k {k-1} & \cdots & \Z k 1 & \Z k 0 & 0\phantom .\\
0 & \Lambda^k \mathcal E^\vee & \Z k k & \Z k {k-1} & \cdots & \Z k 1 & \Z k 0 & 0.\\
};
\foreach \a in {2,3,...,8} {
\pgfmathsubtract{\a}{1}
\draw[->] (m-1-\pgfmathresult) -- (m-1-\a);
\draw[->] (m-2-\pgfmathresult) -- (m-2-\a);
};
\foreach \b in {1,2} {
\path (m-\b-1.west);
\tikzlabel{eq:se\b}
}
\end{tikzpicture}
\end{equation*}

Let's refer to \eqref{eq:se1} and \eqref{eq:se2}; but not to \eqref{eq:orig}.
\end{document}


The complicated bit is working out where to put the equation numbers. We measure the current bounding box to get the width, then subtract that from the textwidth, and divide by two. This gives us the distance from the bounding box to the left margin (since the equation will be centred). However, we need to work from the edge of the leftmost node in the matrix (to get the right height) and that isn't necessarily at the edge of the bounding box. So we correct for that. We can therefore locate the lefthand margin at the right height and place our equation number where we want it. The other bit of magic is the pgfinterruptboundingbox (a trick I learnt from Jake) to remove the equation numbering from the bounding box calculation, thus ensuring that the exact sequence remains centred.

Added in Edit: I've added labelling capability, and refactored it into a separate command. The syntax is that \tikzlabel[node options]{label} will add an equation number at the left margin on a level with the last point used, and set label to point to it (so that \ref{label} or \eqref{label} work as they should). So in this example, we "use" the point (m-\b-1.west) before calling it. (An alternative would be to feed that point to the \tikzlabel command.) The optional node options get added to the node that places the label. So the label can be coloured, repositioned (for example, if the vertical alignment isn't quite right), or anythinged. The default is to use the west anchor with inner sep=0pt, but any extra options are placed last so can override these.

It's best to place labels last since we have to essentially guess where the left margin is based on the bounding box and can only know the bounding box at the current state of play (I've seen something somewhere that puts code at the end of the picture, this would be a good use of that, but I've not used that myself yet so I'll need to experiment to get it right).

-
Looks fantastic... Just one more question; is there any way to add \label{}'s for the equation numbers? –  Josh Guffin Mar 31 '11 at 20:02
@Josh: Done (simply putting \label{X} didn't work). I took the opportunity to refactor the code into a command to make it easier to use. I can think of a few more improvements myself, but do let me know if you think of any whilst using it (assuming that you do!). –  Loop Space Mar 31 '11 at 21:15