# equations in tikz commutative diagram

equation label within commutative diagram

and I thank everyone again.

But the text is very sensitive to changes, so when I try to change what people suggested, I get error messages.

So I created the smallest possible MWE:

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

\usepackage{hyperref, longtable, tabu, booktabs}
\usetikzlibrary{arrows.meta,shapes.arrows}

\usepackage{tikz-cd}

\begin{document}

\begin{figure}[h]
\begin{equation*} \label{eqn-commutative}
\begin{tikzcd}[row sep=huge, column sep=width("bbbbbbbbbbbbbbbbbbbbbbb")]
\arrow[d,   "\mathrm{some \, down \, text \, here}"  swap]  &     \arrow[d,  "\mathrm{some \, down \, text \, here}" ] B  \\

\end{tikzcd}
\end{equation*}
\vspace{-1.5cm}
\caption{Figure caption here}
\label{figure-commutative}
\end{figure}

In place of the "A" I would like the following:

$$\label{eq:1} \left\{\begin{array}{rl} \sup & c^T x \\ s.t. & Ax \leq b. \end{array} \right.$$

with equation label adjusted to left.

In place of the "B" I would like the following:

$$\label{eq:2} \left\{\begin{array}{rl} \sup & f^T x \\ s.t. & Dx \leq e. \end{array} \right.$$

with equation label adjusted to left.

Then I would like to reference \eqref{eq:1} and \eqref{eq:2} here.

\end{document}


• Perhaps good to know: you can format a block of code by indenting it by four lines (like I've just done). This can be accomplished by either pressing the {}-button at the top or by pressing ctrl-K (or cmd-K in MacOS) while the text is selected, as demonstrated here. – Circumscribe Feb 4 at 21:11
• unrelated but it is best to avoid labels with numbers like \label{eq:1}  they work but will be confusing for anyone looking at the source if the printed numbers are different. – David Carlisle Feb 4 at 21:12
• do you actually want to use the same global equation counter as used in the rest of the document, or do you want a local numbering starting from 1 in this figure? (It is rather rare to use main equation counter in a figure as figures float so can be out of sequence with non-floating equations. – David Carlisle Feb 4 at 21:15
• @David Carlisle I do want to use the same global equation counter. I usually manage to nail the figure in place with say \begin{figure}[H] – district9 Feb 4 at 22:17
• @district9 even [H] takes the figure out of the current context (so for example it is not set as part of a list) The only reason to use figure is to specify that it may move, if you don't want it to move just don't use figure equations are almost never put in floating environments such as figure. – David Carlisle Feb 5 at 12:10

I'd define a suitable command for this. Note that you don't have to guess the width of the single big piece.

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

\makeatletter
\newcommand{\nodeequation}[1]{%
\let\label\ltx@label
\refstepcounter{equation}%
(\theequation)%
}
\makeatother

\begin{document}
\begin{equation*}
\begin{tikzcd}[row sep=4em,column sep=10em,ampersand replacement=\&]
\nodeequation{
\label{eq:1}
\begin{cases}
\begin{alignedat}{2}
\end{alignedat}
\end{cases}
}
\arrow[r,"\text{some text here}"] \arrow[d,swap,"\text{some text here}"] \&
\nodeequation{
\label{eq:2}
\begin{cases}
\begin{alignedat}{2}
\end{alignedat}
\end{cases}
}
\arrow[d,"\text{some text here}"]
\\
C \& D \arrow[l,"\text{some text here}"]
\end{tikzcd}
\end{equation*}
The references are \eqref{eq:1} and \eqref{eq:2}. Another equation to see
that the number is right
$$0=0$$

\end{document}


Here is a proposal. The equation numbers on the left and right go back to David Carlisle's answer. So just squeeze these equations in minipages. And instead of having a \quad and \hspace gathering, just use pos=... in order to move the texts around, which are much more straightforward to typeset with \text BTW.

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

% https://tex.stackexchange.com/a/212099/121799
\makeatletter
\newcommand{\leqnomode}{\tagsleft@true\let\veqno\@@leqno}%
\newcommand{\reqnomode}{\tagsleft@false\let\veqno\@@eqno}%
\newcommand*{\compress}{\@minipagetrue}
\makeatother

\begin{document}

\begin{figure}[h]
\begin{equation*}
\begin{tikzcd}[row sep=huge, column sep=width("bbbbbbbbbbbbbbbbbbbbbbb"),
ampersand replacement=\&]
\begin{minipage}{4cm}
\leqnomode
$$\label{eq:1} \left\{\begin{array}{rl} \sup & c^T x \\ s.t. & Ax \leq b. \end{array} \right.$$
\end{minipage}
\arrow[r,  "\text{some text here}" pos=0.5]
\arrow[d,   "\text{some down text here}"  swap]  \&
\arrow[d,  "\text{some down text here}" ]
\begin{minipage}{4cm}
$$\label{eq:2} \left\{\begin{array}{rl} \sup & f^T x \\ s.t. & Dx \leq e. \end{array} \right.$$
\end{minipage}\\
C        \& D
\arrow[l, "\text{some text here}" pos=0.4 ] \\[-1cm]
\end{tikzcd}
\end{equation*}
\caption{Figure caption here.}
\label{figure-commutative}
\end{figure}

Then I would like to reference \eqref{eq:1} and \eqref{eq:2} here.

\end{document}