# Align across cases

I have the following equations:

I'd like to align all of the equals signs, as well as the for-all statements and put an explanatory word centered vertically on the left-hand side of each bracket.

My code at the moment follows. It's just a series of cases. I'm not sure how to enforce alignment between the cases or handle multiple equals signs within each case.

Vertically aligning the explanatory text is also problematic, but I figure I can fudge that with non-breaking spaces or hbox.

\documentclass{article}
\usepackage{amsmath}
\usepackage{rotating}

\begin{document}

\begin{align}
\begin{rotate}{90}
Init
\end{rotate}
&\begin{cases}
P_{1,1}    &= P_0 \\
S_{1,1}    &= S_0                                                                         \\
R_{1,1}    &= R_0                                                                         \\
\end{cases} \\
&\begin{cases}
g(P_{y,t}) &= u_{y,t,P}+u_{y,t,S}+u_{y,t,R}                \forall y \forall t>1          \\
\end{cases} \\
&\begin{cases}
P_{y,t+1}  &=  P_{y,t}+c_P \cdot u_{y,t,P}\cdot\Delta t   \forall y \forall t<T          \\
S_{y,t+1}  &=  S_{y,t}+c_S \cdot u_{y,t,S}\cdot\Delta t                      \forall y \forall t<T          \\
R_{y,t+1}  &=  R_{y,t}+c_R \cdot u_{y,t,R}\cdot\Delta t                      \forall y \forall t<T          \\
\end{cases} \\
&\begin{cases}
P_{y,1}    &= \gamma_P P_{y-1,T}+\gamma_W S_{y-1,T}        \forall y>1                    \\
S_{y,1}    &= 0                                                              \forall y>1  \\
R_{y,1}    &= 0                                                              \forall y>1
\end{cases}
\end{align}

\end{document}


I would only use rotating for sidewaystable the other features such as the rotate environment are really just for compatibility with the old latex2.09 version of the package, on which graphicx was based. It is just a very thin compatibility wrapper around graphicx and in this case loses the option of setting to rotation to be around the centre.

For the rest, I just forced some explicit spacing, the only tricky bit being that a large { is wider than a standard one.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}

\begin{document}

\begin{align}
\rotatebox[origin=c]{90}{Init}
&\begin{cases}
\makebox[2.5em][l]{$P_{1,1}$}&= P_0 \\
\makebox[2.5em][l]{$S_{1,1}$}&= S_0                                                                         \\
\makebox[2.5em][l]{$R_{1,1}$}&= R_0                                                                         \\
\end{cases} \\
&\begin{cases}
\sbox0{$\Bigg\{$}
\sbox2{$\big\{$}
\kern\dimexpr\wd0-\wd2\relax
\makebox[2.5em][l]{$g(P_{y,t})$}&= u_{y,t,P}+u_{y,t,S}+u_{y,t,R}                \forall y \forall t>1          \\
\end{cases} \\
&\begin{cases}
\makebox[2.5em][l]{$P_{y,t+1}$}&=  P_{y,t}+c_P \cdot u_{y,t,P}\cdot\Delta t   \forall y \forall t<T          \\
\makebox[2.5em][l]{$S_{y,t+1}$}&=  S_{y,t}+c_S \cdot u_{y,t,S}\cdot\Delta t                      \forall y \forall t<T          \\
\makebox[2.5em][l]{$R_{y,t+1}$}&=  R_{y,t}+c_R \cdot u_{y,t,R}\cdot\Delta t                      \forall y \forall t<T          \\
\end{cases} \\
&\begin{cases}
\makebox[2.5em][l]{$P_{y,1}$}&= \gamma_P P_{y-1,T}+\gamma_W S_{y-1,T}        \forall y>1                    \\
\makebox[2.5em][l]{$S_{y,1}$}&= 0                                                              \forall y>1  \\
\makebox[2.5em][l]{$R_{y,1}$}&= 0                                                              \forall y>1
\end{cases}
\end{align}

\end{document}

• @daleif oh I didn't fully read the question or even attempt to align the forall:-) In that case someone should post an answer not using cases at all. but using an array or aligned in which case I might delete this.... – David Carlisle May 2 '19 at 8:28
• You can use eqparbox (as I did), I just did not notice the issue with the second cases. – daleif May 2 '19 at 8:33
• @daleif ah OK I see you undeleted yours, If you steal bits of this rather than referring to it, I will delete this one, I'll leave it for now as you refer to it, and if I delete it it won't be visible to most users. – David Carlisle May 2 '19 at 8:35
• I'd rather have both. Have you noticed that if you replace my manual adjustment with yours, then they don't exactly match in the = alignment, it is slightly too mich to the right. Ohh, there is an error in yours.... (#blamedavid). box 2 should use \big\{ – daleif May 2 '19 at 8:38
• @daleif oops I'll fix – David Carlisle May 2 '19 at 8:53

Here is a suggestion using the eqparbox package. It requires a few compilations.

The adjustment needed for the second cases is bue to Davis Carlisle.

\documentclass{article}
\usepackage{amsmath}
\usepackage{rotating}

\usepackage{eqparbox}
\newcommand\MB[3][]{%
\eqmakebox[#2][#1]{$#3$}
}

\begin{document}

\begin{align}
\begin{rotate}{90}
Init
\end{rotate}
&
\begin{cases}
\MB[r]{A}{P_{1,1}}  = \MB[l]{B}{P_0}                                                                 \\
\MB[r]{A}{S_{1,1}}  = \MB[l]{B}{S_0}                                                                 \\
\MB[r]{A}{R_{1,1}}  = \MB[l]{B}{R_0}                                                                 \\
\end{cases}                                                                                          \\
&
\begin{cases}
% manually adjusted bacause of the { width difference
\sbox0{$\Bigg\{$}
\sbox2{$\big\{$}
\kern\dimexpr\wd0-\wd2\relax
\MB[r]{A}{g(P_{y,t})} = \MB[l]{B}{u_{y,t,P}+u_{y,t,S}+u_{y,t,R}}             & \forall y \forall t>1 \\
\end{cases}                                                                                          \\
&
\begin{cases}
\MB[r]{A}{P_{y,t+1}}  =  \MB[l]{B}{P_{y,t}+c_P \cdot u_{y,t,P}\cdot\Delta t} & \forall y \forall t<T \\
\MB[r]{A}{S_{y,t+1}}  =  \MB[l]{B}{S_{y,t}+c_S \cdot u_{y,t,S}\cdot\Delta t} & \forall y \forall t<T \\
\MB[r]{A}{R_{y,t+1}}  =  \MB[l]{B}{R_{y,t}+c_R \cdot u_{y,t,R}\cdot\Delta t} & \forall y \forall t<T \\
\end{cases}                                                                                          \\
&
\begin{cases}
\MB[r]{A}{P_{y,1}}    = \MB[l]{B}{\gamma_P P_{y-1,T}+\gamma_W S_{y-1,T}}     & \forall y>1           \\
\MB[r]{A}{S_{y,1}}    = \MB[l]{B}{0}                                         & \forall y>1           \\
\MB[r]{A}{R_{y,1}}    = \MB[l]{B}{0}                                         & \forall y>1
\end{cases}
\end{align}

\end{document}


• Could you add some comments explaining the manual adjustment portion? – Richard May 2 '19 at 20:28
• @Richard it should be fairly self explanatory. We have a problem with the second cases, that is clear from the image. Thus for the contents to lign up properly, we need an adjustment. We box two suitable braces, and insert their width difference – daleif May 2 '19 at 20:42

Here is another option using a matrix of nodes from the tikz package.

There is a bit of trickery using tikpagenodes to "manually" place the equation numbers. The alignment of the equals signs is not perfect but the matrix of nodes gives you full control over any "decorations" that you want to add to these equations.

Here's the code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{matrix,fit,decorations.pathreplacing}

\begin{document}

\begin{center}
\begin{tikzpicture}[remember picture]
\matrix (M)[
matrix of math nodes,
column 1/.style = {anchor=base east, minimum width=3.6em},
column 2/.style = {anchor=base west},
column 3/.style = {anchor=base west},
]{
P_{1,1}    &= P_0 \\
S_{1,1}    &= S_0 \\
R_{1,1}    &= R_0 \\
g(P_{y,t}) &= u_{y,t,P}+u_{y,t,S}+u_{y,t,R}            &\forall y \forall t>1 \\
P_{y,t+1}  &= P_{y,t}+c_P \cdot u_{y,t,P}\cdot\Delta t &\forall y \forall t<T \\
S_{y,t+1}  &= S_{y,t}+c_S \cdot u_{y,t,S}\cdot\Delta t &\forall y \forall t<T \\
R_{y,t+1}  &= R_{y,t}+c_R \cdot u_{y,t,R}\cdot\Delta t &\forall y \forall t<T \\
P_{y,1}    &= \gamma_P P_{y-1,T}+\gamma_W S_{y-1,T}    &\forall y>1 \\
S_{y,1}    &= 0                                        &\forall y>1 \\
R_{y,1}    &= 0                                        &\forall y>1 \\
};
% add braces around equation groups
\foreach \top/\bot/\lab in {1/3, 4/4, 5/7, 8/10} {
\draw[thick,decorate, decoration={mirror,brace}]
([yshift=-1pt]M-\top-1.north west)--([yshift=1pt]M-\bot-1.south west);
}
\node[rotate=90] at ([xshift=-3mm]M-2-1.west){Init};
% and now add the equation lines, using tikzpagenodes to put
% them on the righthand margin
\begin{scope}[overlay]
\foreach \eq in {2,4,6,9} {
\refstepcounter{equation}
\node[anchor=east] at ([xshift=-3em]M-\eq-1-|current page text area.east) {(\theequation)};
}
\end{scope}
\end{tikzpicture}
\end{center}

\end{document}


I ended up fusing a few of the answers and adding additional space because I didn't like one of the braces being narrow than the others:

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{eqparbox}
\newcommand\MB[3][]{%
\eqmakebox[#2][#1]{$#3$}
}

\begin{document}

{\small\allowdisplaybreaks
\begin{align}
\rotatebox[origin=c]{90}{\footnotesize \parbox{1.8cm}{\centering Initial \\ Conditions}}
&\left\{
\begin{aligned} \label{equ:mod1_init}
\MB[r]{A}{P_{1,1}}  = \MB[l]{B}{P_0}                                                                 \\
\MB[r]{A}{S_{1,1}}  = \MB[l]{B}{S_0}                                                                 \\
\MB[r]{A}{R_{1,1}}  = \MB[l]{B}{R_0}                                                                 \\
\end{aligned}
\right.           \\
\rotatebox[origin=c]{90}{\footnotesize \parbox{1.8cm}{\centering Mass\\ Conservation}}
&\left\{
\begin{aligned} \label{equ:mod1_masscons}
% manually adjusted bacause of the { width difference
\sbox0{$\Bigg\{$}
\sbox2{$\big\{$}
\kern\dimexpr\wd0-\wd2\relax
&&\\
\MB[r]{A}{g(P_{y,t})} &= \MB[l]{B}{u_{y,t,P}+u_{y,t,S}+u_{y,t,R}}             & \forall y \forall t>1 \\
&&\\
\end{aligned}
\right.           \\
\rotatebox[origin=c]{90}{\footnotesize Integration}
&\left\{
\begin{aligned} \label{equ:mod1_integration}
\MB[r]{A}{P_{y,t+1}}  &=  \MB[l]{B}{P_{y,t}+c_P \cdot u_{y,t,P}\cdot\Delta t} & \forall y \forall t<T \\
\MB[r]{A}{S_{y,t+1}}  &=  \MB[l]{B}{S_{y,t}+c_S \cdot u_{y,t,S}\cdot\Delta t} & \forall y \forall t<T \\
\MB[r]{A}{R_{y,t+1}}  &=  \MB[l]{B}{R_{y,t}+c_R \cdot u_{y,t,R}\cdot\Delta t} & \forall y \forall t<T \\
\end{aligned}
\right. \\
\rotatebox[origin=c]{90}{\footnotesize Continuity}
&\left\{
\begin{aligned} \label{equ:mod1_continuity}
\MB[r]{A}{P_{y,1}}    &= \MB[l]{B}{\gamma_P P_{y-1,T}+\gamma_W S_{y-1,T}}     & \forall y>1           \\
\MB[r]{A}{S_{y,1}}    &= \MB[l]{B}{0}                                         & \forall y>1           \\
\MB[r]{A}{R_{y,1}}    &= \MB[l]{B}{0}                                         & \forall y>1
\end{aligned}
\right.
\end{align}
}

\end{document}