# Align and fit two sets of equations onto

I am having a problem with beamer slide. There are several problems that I have tried to solve.

1. How to scale the equations correctly
2. How to align the four restrictions on the same height
3. How to write two vertical lines with variables between as depicted in the attached figure.

Thank you for any ideas!

\documentclass{beamer}

\usetheme{CambridgeUS}

\begin{document}

\begin{frame}{Primal-dual}

\begin{columns}[T]
\begin{column}{.5\textwidth}
\small
$\text{Min} \sum_{i=1}^{m+1}\sum_{j=1}^{n+1}c_{ij}x_{ij}$
\scalebox{0.8}{%  \hskip4.5em \text{s.a.} \quad \begin{aligned}[t] &\sum_{j=1}^{n}x_{ij}-x_{i,n+1}= a_i \qquad \forall i=1,\hdots,m\\ & \textcolor{blue}{\sum_{j=1}^{n}}x_{m+1,j}+\textcolor{magenta}{x_{m+1,n+1}}=a_{m+1}\\ &\sum_{i=1}^{m}x_{ij}+x_{m+1,j}= b_j \qquad \forall j=1,\hdots,n\\ &-\textcolor{red}{\sum_{i=1}^{m}}x_{i,n+1}+\textcolor{magenta}{x_{m+1,n+1}}=b_{n+1}\\ &x_{ij}\ge 0 \qquad \left\{ \begin{aligned} \forall i=&1,\hdots,m\\ \forall j=&1,\hdots,n\\ \end{aligned} \right. \end{aligned} }
\normalsize
\end{column}
\hfill%
\begin{column}{.5\textwidth}
\small
$\text{Max} \sum_{i=1}^{m+1}u_i a_i+\sum_{j=1}^{n+1}v_j b_j$
\scalebox{0.8}{%  \hskip5em \text{s.a.} \quad \begin{aligned}[t] & u_i+v_j \le c_{ij}\\ & \\ & -u_i-v_{n+1} \le c_{i,n+1}\\ & \\ & u_{m+1}+v_j \le c_{m+1,j}\\ & \\ & u_{m+1}+v_{n+1} \le c_{{m+1},{n+1}}\\ \end{aligned} }
\normalsize
\end{column}
\end{columns}

\end{frame}

\end{document}

The idea is the same as Werner's of using array, but with some small details (and the color you showed in your answer.

\documentclass{beamer}
\usetheme{CambridgeUS}

\usepackage{mathtools,array}

\DeclareMathOperator{\Min}{Min}
\DeclareMathOperator{\Max}{Max}
\newcommand{\nominus}{\mathllap{-}}

\begin{document}

\begin{frame}{Primal-dual}

\scriptsize
\begin{equation*}
\renewcommand{\arraystretch}{2}
\begin{array}{
@{}
>{$}c<{$} % s.a.
>{\displaystyle}l % main column 1
|@{\,} % vertical line
>{\displaystyle}c % middle column
@{\,}| % vertical line
>{$}c<{$} % s.a.
>{\displaystyle}l % main column 2
@{}
}
\multicolumn{2}{@{}>{\displaystyle}l}{
\! \Min \sum_{i=1}^{m+1} \sum_{j=1}^{n+1}c_{ij}x_{ij}
}
&
\multicolumn{1}{c}{} % no vertical rule here
&
\multicolumn{2}{>{\displaystyle}l}{
\! \Max \sum_{i=1}^{m+1}u_i a_i+\sum_{j=1}^{n+1}v_j b_j
}
\\
% first row
s.a. &
\sum_{j=1}^{n}x_{ij}-x_{i,n+1}= a_i  \qquad\hfill \forall i=1,\hdots,m &
u_i &
s.a. &
u_i+v_j \le c_{ij}
\\
% second row
&
\mathop{\textcolor{blue}{\sum_{j=1}^{n}}}x_{m+1,j}+\textcolor{magenta}{x_{m+1,n+1}}=a_{m+1} &
u_{m+1} &
&
\nominus u_i-v_{n+1} \le c_{i,n+1}
\\
% third row
&
\sum_{i=1}^{m}x_{ij}+x_{m+1,j}= b_j  \qquad\hfill \forall j=1,\hdots,n &
v_j &
&
u_{m+1}+v_j \le c_{m+1,j}
\\
% fourth row
&
\nominus\!\mathop{\textcolor{red}{\sum_{i=1}^{m}}}x_{i,n+1}
+\textcolor{magenta}{x_{m+1,n+1}}=b_{n+1} &
v_{n+1} &
&
u_{m+1}+v_{n+1} \le c_{{m+1},{n+1}}
\\
% fifth row
&
\multicolumn{1}{l}{
\left\{
\begin{aligned}
\forall i&=1,\dots,m\\
\forall j&=1,\dots,n
\end{aligned}
\right.
}
\end{array}
\end{equation*}

\end{frame}

\end{document}

With \nominus a minus sign sticking to the left is added, but we need to fix the spacing next to the colored summation. The \multicolumn commands override the default alignments.

With \qquad\hfill we ensure the spacing before the “for all” symbols, but also right alignment of the conditions.

Note that \scriptsize has to go outside the equation* environment.

• Very neat solution. Thanks for the useful explanations. Commented Mar 15, 2021 at 19:02

Due to the numerous alignments and vertical rules, I'd set this using an array:

\documentclass{beamer}

\usepackage{amsmath,array,graphicx}

\begin{document}

\begin{frame}

\resizebox{\linewidth}{!}{$\renewcommand{\arraystretch}{2} \begin{array}{ r >{\displaystyle}l | c | r @{} >{{}}l } & \multicolumn{1}{c}{\displaystyle \min \sum_{i = 1}^{m + 1} \sum_{j = 1}^{n + 1} c_{ij} x_{ij} } & \multicolumn{1}{c}{ \renewcommand{\arraystretch}{1}\begin{tabular}{@{} c @{}} variable \\ dual \end{tabular}} & \multicolumn{2}{c}{\displaystyle \max \sum_{i = 1}^{m + 1} u_i a_i + \sum_{j = 1}^{n + 1} v_j b_j } \\ \text{s.t.} & \sum_{j = 1}^n x_{ij} - x_{i, n + 1} = a \quad \forall i = 1, \dotsc, m & u_i & u_i + v_j &\leq c_{ij} \\ & \sum_{j = 1}^n x_{m + 1, j} + x_{m + 1, n + 1} = a_{m + 1} & u_{m + 1} & -u_i - v_{n + 1} &\leq c_{i, n + 1} \\ & \sum_{i = 1}^m x_{ij} + x_{m + 1, j} = b_j \quad \forall j = 1, \dotsc, n & v_j & u_{m + 1} + v_j &\leq c_{m + 1, j} \\ & -\sum_{i = 1}^m x_{i, n + 1} + x_{m + 1, n + 1} = b_{n + 1} & v_{n + 1} & u_{m + 1} + v_{n + 1} &\leq c_{m + 1, n + 1} \\ & \multicolumn{2}{l}{ \renewcommand{\arraystretch}{1.5} x_{ij} \geq 0 \quad \left\{ \begin{array}{@{} l @{}} \forall i = 1, \dotsc, m \\ \forall j = 1, \dotsc, n \end{array}\right. } \end{array}$}

\end{frame}

\end{document}

If you wish to have some additional alignments for the left-hand column, here's an option:

\documentclass{beamer}

\usepackage{amsmath,array,graphicx}

\begin{document}

\begin{frame}

\resizebox{\linewidth}{!}{$\renewcommand{\arraystretch}{2} \begin{array}{ r >{\displaystyle}r @{} >{{}}l | c | r @{} >{{}}l } & \multicolumn{2}{c}{\displaystyle \min \sum_{i = 1}^{m + 1} \sum_{j = 1}^{n + 1} c_{ij} x_{ij} } & \multicolumn{1}{c}{ \renewcommand{\arraystretch}{1}\begin{tabular}{@{} c @{}} variable \\ dual \end{tabular}} & \multicolumn{2}{c}{\displaystyle \max \sum_{i = 1}^{m + 1} u_i a_i + \sum_{j = 1}^{n + 1} v_j b_j } \\ \text{s.t.} & \sum_{j = 1}^n x_{ij} - x_{i, n + 1} &= a \quad \forall i = 1, \dotsc, m & u_i & u_i + v_j &\leq c_{ij} \\ & \sum_{j = 1}^n x_{m + 1, j} + x_{m + 1, n + 1} &= a_{m + 1} & u_{m + 1} & -u_i - v_{n + 1} &\leq c_{i, n + 1} \\ & \sum_{i = 1}^m x_{ij} + x_{m + 1, j} &= b_j \quad \forall j = 1, \dotsc, n & v_j & u_{m + 1} + v_j &\leq c_{m + 1, j} \\ & -\sum_{i = 1}^m x_{i, n + 1} + x_{m + 1, n + 1} &= b_{n + 1} & v_{n + 1} & u_{m + 1} + v_{n + 1} &\leq c_{m + 1, n + 1} \\ & x_{ij} &\multicolumn{1}{@{} l @{}}{ \renewcommand{\arraystretch}{1.5} {}\geq 0 \quad \left\{ \begin{array}{@{} l @{}} \forall i = 1, \dotsc, m \\ \forall j = 1, \dotsc, n \end{array}\right. } \end{array}$}

\end{frame}

\end{document}
• Is there any reason for aligning at the = signs? I see none, except for producing awkward output. ;-) Commented Mar 15, 2021 at 18:06
• @egreg: True. I included it as an optional extra, but it's not necessary. It's easier taking it out rather than having to amend the answer if that was something that the OP wanted to include.
– Werner
Commented Mar 15, 2021 at 18:27
• This approach is also very good. Commented Mar 15, 2021 at 19:03

I have come up with a new solution to my own question which gives an almost satisfactory result.

\documentclass{beamer}

\usetheme{CambridgeUS}

\begin{document}

\begin{frame}{Primal-dual}

\begin{equation*}
\scriptsize
\begin{aligned}[c]
\text{Min} & \sum_{i=1}^{m+1}\sum_{j=1}^{n+1}c_{ij}x_{ij}\\
& \textcolor{blue}{\sum_{j=1}^{n}}x_{m+1,j}+\textcolor{magenta}{x_{m+1,n+1}}=a_{m+1}\\
-&\textcolor{red}{\sum_{i=1}^{m}}x_{i,n+1}+\textcolor{magenta}{x_{m+1,n+1}}=b_{n+1}\\
\left\{
\begin{aligned}
\forall i=&1,\hdots,m\\     \forall j=&1,\hdots,n\\
\end{aligned}
\right.
\end{aligned}
\begin{vmatrix}
\phantom{}\\
u_i \\
\phantom{}\\
\phantom{}\\
u_{m+1} \\
\phantom{}\\
\phantom{}\\
v_j \\
\phantom{}\\
\phantom{}\\
v_{n+1}\\
\phantom{}\\
\end{vmatrix}
\begin{aligned}[c]
\text{Max} & \sum_{i=1}^{m+1}u_i a_i+\sum_{j=1}^{n+1}v_j b_j\\
& \phantom{}\\
& u_i+v_j \le c_{ij}\\
& \\
-& u_i-v_{n+1} \le c_{i,n+1}\\
& \\
& u_{m+1}+v_j \le c_{m+1,j}\\
& \\
& u_{m+1}+v_{n+1} \le c_{{m+1},{n+1}}\\
& \phantom{}\\
& \phantom{}\\
& \phantom{}\\
& \phantom{}\\
\end{aligned}
\normalsize
\end{equation*}

\end{frame}

\end{document}

• Use \mathop{\textcolor{red}{\sum_{i=1}^m}} to get the right spacing around the summation symbol Commented Mar 15, 2021 at 18:09