# Aligning linear system in optimization problem

There are a lot of similar problems I found on stackexchange, but none quite worked for my problem.

Given an objective function and a set of (in-)equalities, I want to neatly align the variables in the linear system. With lots of ampersands I managed to stack variables and plusses / minuses on top of each other.

For instance, suppose the problem is to maximize x1 + x2 + x3 subject to x1 - 2x2 + x3 < 4, x2 + 12x3 < -15 and -3x1 - x3 > 6.

My current approach using the array environment looks as follows, resulting in the output shown below.

\begin{array}{rrrrrrrl}
& \max              & x_{1}+x_{2}+x_{3} \\
& \text{subject to} &   x_{1} & - & 2x_{2} & + &   x_{3} & \leq 4 \\
&                   &         &   &  x_{2} & + & 12x_{3} & \leq -15 \\
&                   & -3x_{1} &   &        & - &   x_{3} & \geq 6
\end{array}


In the linear system the variables neatly align. However with the introduction of the objective function, everything is shifted to the right, when in reality the length of the objective function should not affect the inequalities.

I tried nesting the array environment which kinda worked in LaTeX, unfortunately I'm constrained to MathJax which didn't accept this, so nesting arrays may not make be a feasible solution.

How can I make the objective function and the linear system independent from each other?

You can use align and alignedat, which works in both worlds.

\begin{align*}
\max              &\; x_{1}+x_{2}+x_{3} \\
\text{subject to} &\;
\begin{alignedat}[t]{3}
x_{1}   & -{} & 2x_{2} & +{} &   x_{3} & \leq 4 \\
&     &  x_{2} & +{} & 12x_{3} & \leq -15 \\
-3x_{1} &     &        & -{} &   x_{3} & \geq 6
\end{alignedat}
\end{align*}


• Thank you for this! Since I need this pretty often, I decided to write a little tool that makes writing those functions a little easier. This might be useful to some: jassler.github.io/latexalign – Felix Jassler Jun 24 at 11:22
• What is the purpose of the braces after the +'s and -'s? – Jim Hefferon Jun 26 at 21:36
• @JimHefferon There is an implicit {} before left aligned cells, but we need also one after binary operation symbols to ensure the correct spacing. – egreg Jun 26 at 21:41