How to align equations

I am trying to write a set of equations similar to some found in a book I am reading. Below is how the book has formatted the equations:

and here is what I have so far:

How can I align my equations so that "maximize" and "subject to" are aligned, while also aligning the sums? I do not want to display "(KP)" or any equation numbers on the side.

Here is the code I wrote:

\begin{equation*}
\begin{aligned}
\text{maximize} \sum_{j=1}^{n}p_j x_j \\
\text{subject to} \sum_{j=1}^{n}w_j x_j \leq c, \\
x_j \in \{0,1\}, j = 1, \ldots, n.
\end{aligned}
\end{equation*}

• use the align or align* environments from the AMS packages, with &\null as your separator and \text{} for text. – Sean Allred Jun 19 '13 at 22:25
• You could look at A macro for optimization problems – egreg Jun 19 '13 at 22:35

Use the align environments from amsmath:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
\text{maximize} &\null \sum_{j=1}^n p_j x_j \\
\text{subject to} &\null \sum_{j=1}^n w_j x_j \leq c, \\
&\null x_j \in \{0, 1\}, \quad j=1,\dots,n.
\end{align}
\end{document}

• Same solution as mine, but some seconds faster. – Benedikt Bauer Jun 19 '13 at 22:31
• @BenediktBauer Although I wonder if taking out \null and aligning at \sum is better -- I'm not sure. (Perhaps even aligning at a spacer like \;?) – Sean Allred Jun 19 '13 at 22:33
• Hm, now I see there's a slight difference between your and my solution. I aligned at the sum sign. This will surely be no problem as long as the super- and subscripts are not wider. We would have to check what happens if those are wider than the sum sign. Things might get uglier in this case. – Benedikt Bauer Jun 19 '13 at 22:41

The simple solution

As long as the super- and subscripts of the summation signs are narrower than the sign itself, the solution is quite simple:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
\text{maximize } &\sum_{j=1}^{n}p_j x_j \\
\text{subject to } &\sum_{j=1}^{n}w_j x_j \leq c, \\
&x_j \in \{0,1\}, j = 1, \ldots, n.
\end{align}
\end{document}


The more sophisticated solution

As soon as one of the super- or subscripts gets wider than the sign, this will break the alignment (note eq. (2)):

\begin{align}
\text{maximize } &\sum_{j=1}^{n}p_j x_j \\
\text{subject to } &\sum_{j=10000}^{n}w_j x_j \leq c, \\
&x_j \in \{0,1\}, j = 1, \ldots, n.
\end{align}


As everything is aligned at the most left part after the & sign, the alignment happens in eq. (2) at the "j" subscript, making the sum signs no longer stand right below each other:

But there is help! The mathtools package provides a command called \mathclap that in principle sets the box width of its argument to zero (for exact explanation cf. http://math.arizona.edu/~aprl/publications/mathclap/perlis_mathclap_24Jun2003.pdf). If we use this, the alignment will work even if the descriptors of the sum sign are wider than the sign itself, by allowing the descriptors to slide over the alignment barrier:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
\begin{align}
\text{maximize } &\sum_{j=1}^{n}p_j x_j \\
\text{subject to } &\sum_{\mathclap{j=10000}}^{n}w_j x_j \leq c, \\
&x_j \in \{0,1\}, j = 1, \ldots, n.
\end{align}
\end{document}


You can use

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{alignat}{2}
text & equation\\
text & equation\\
& equatin
\end{alignat}
\end{document}


Additionally, with alignat, you can specify multiple alignment points. If we had 3, we could align equations like this:

f(x) & = & x^2 +2x + 1\\
& = & (x + 1)^2


Or you could go with even more alignment options depending on your needs.

• Could you flesh this out a bit? Maybe with a MWE and a picture? – Sean Allred Jun 19 '13 at 22:31
• @SeanAllred there you go. – dustin Jun 19 '13 at 22:35