4

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:

book equations

and here is what I have so far:

my equations

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*}
2

3 Answers 3

4

output

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}
3
  • Same solution as mine, but some seconds faster. Jun 19, 2013 at 22:31
  • 2
    @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 \;?) Jun 19, 2013 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. Jun 19, 2013 at 22:41
3

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}

enter image description here

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:

enter image description here

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}

enter image description here

2

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.

enter image description here

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

You must log in to answer this question.