How to align 'parts' of these equations?
Here's one possibility; I used an align
environment to align the quantified expressions; to get the left parts centered, I used some boxes with centered content; the \Ceneq
command places its argument in a box whose width correspomds to the wider expression:
\documentclass{report}
\usepackage{amsmath}
\newlength\longest
\newcommand\Ceneq[1]{%
\makebox[\longest][c]{$#1$}}
\begin{document}
\settowidth\longest{$0 \leq k - U_a(t, C) \leq (1-a_t) \cdot Z$}
\begin{align}
\label{eqn:eraserStart}
\Ceneq{\max d} \\
\Ceneq{a_t \in \{0,1\}} &\qquad\forall t \in T \\
\Ceneq{\sum_{\forall t \in T} a_t = 1} \\
\Ceneq{a_t \in [0,1]} &\qquad\forall t \in T \\
\Ceneq{\sum_{\forall t \in T} c_t \leq m} \\
\Ceneq{d - U_d(t, C) \leq (1-a_t) \cdot Z} &\qquad\forall t \in T \label{eqn:eraserEnd} \\
\Ceneq{0 \leq k - U_a(t, C) \leq (1-a_t) \cdot Z} &\qquad\forall t \in T
\end{align}
\end{document}
Using \displaystyle
before \sum
, you get the limits under the summation symbols:
\documentclass{report}
\usepackage{amsmath}
\newlength\longest
\newcommand\Ceneq[1]{%
\makebox[\longest][c]{$#1$}}
\begin{document}
\settowidth\longest{$0 \leq k - U_a(t, C) \leq (1-a_t) \cdot Z$}
\begin{align}
\label{eqn:eraserStart}
\Ceneq{\max d} \\
\Ceneq{a_t \in \{0,1\}} &\qquad\forall t \in T \\
\Ceneq{\displaystyle\sum_{\forall t \in T} a_t = 1} \\
\Ceneq{a_t \in [0,1]} &\qquad\forall t \in T \\
\Ceneq{\displaystyle\sum_{\forall t \in T} c_t \leq m} \\
\Ceneq{d - U_d(t, C) \leq (1-a_t) \cdot Z} &\qquad\forall t \in T \label{eqn:eraserEnd} \\
\Ceneq{0 \leq k - U_a(t, C) \leq (1-a_t) \cdot Z} &\qquad\forall t \in T
\end{align}
\end{document}
I did find an alternate way and this seems to work just fine. Probably a bit simpler than Gonzalo's solution. Caveat: You need to have either the IEEEtran
document class or use the IEEEtrantools
package
\begin{IEEEeqnarray}{Ccc} %3 column layout to align the equations
\label{eqn:startEraser} \max d && \\ %empty 2nd/3rd columns
\label{eqn:a_t=1} a_t \in \{0,1\} & \quad & \forall t \in T \\ %add \quad in 2nd column for spacing and \forall... in 3rd
\label{eqn:sum a_t} \sum_{\forall t \in T} a_t = 1 && \\
\label{eqn:c_t=1} c_t \in [0,1] & \quad & \forall t \in T \\ %same here
\label{eqn:sum c_t} \sum_{\forall t \in T} c_t \leq m && \\
\label{eqn:dZ} d - U_d(t, C) \leq (1-a_t) \cdot Z & \quad & \forall t \in T \\ %and here
\label{eqn:eraserEnd} 0 \leq k - U_a(t, C) \leq (1-a_t) \cdot Z & \quad & \forall t \in T %and here
\end{IEEEeqnarray}
Here's the output:
Here's my attempt at this. Looking at the problem, it seems you need three things:
- Each row should have two columns.
- The first column should be centered, and the second column should be aligned (say) a
\quad
to the right of it. - The lines should be numbered.
Multiple columns suggests align
; centering of equations suggests gather
. You can't have both at once, though you could do parallel aligned
and gathered
subenvironments as in this answer of mine...except that would give only one number for the whole display, violating the last requirement. A tabular
would be perfect for the combination of the first two requirements, but that also doesn't number its lines. Still, there is no other environment that treats each line as a whole and also allows control over the alignment of each column, so tabular
it is.
Well, tabu
. I don't really use any special features but it's a complete improvement over the built-in tabular
so I should advocate for it. I do the following:
- There are, as promised, two columns of type
c
andl
. - Between them there is a
\quad
by default. - Before the first column, we set up the line numbering by calling
\refstepcounter
onequation
, the counter used by all the equation environments (includingalign
andgather
). Alas, this only sets the label locally, and thus it is forgotten once we reach the actual text where you might write\label
, so I am forced to do some hackery with\@currentlabel
to globalize it. - After that, we make a box the width of the entire line, stick a line number at the right end of it, and then declare the whole thing to have width zero so as not to disturb the rest of the table. That is, the line number is an overlay on the line. This is not how the
amsmath
environments do it, so it will cause problems if your lines are unusually long (or the page is unusually narrow). - Everything is between
$$
so as to set it off as a display, like a real equation. Do not ever use this in real life! It's a literally primitive command and I only did it because I really want only the primitive behavior, not the stuff that makesequation
work well. - For convenience, the whole thing is packaged as an environment I call
align+
for lack of a better name.
I also wrote a simple macro \forallt
to shorten the stuff in the second column. Now you can use it basically like you'd use any array environment.
\documentclass{article}
\usepackage{amsmath,tabu}
\makeatletter
\newenvironment{align+}
{$$\tabu
{
@{\refstepcounter{equation}%
\global\let\@currentlabel\@currentlabel
\rlap{\makebox[\linewidth]{\hfil$(\theequation)$}}
}%
>{\displaystyle}c%
@{\quad}%
>{\displaystyle}l%
}
}
{\endtabu$$\ignorespacesafterend}
\makeatother
\newcommand*\forallt{\forall t \in T}
\begin{document}
\noindent
In the table below:
\begin{align+}
\label{eq1}\max d \\
a_t \in \{0,1\} & \forallt \\
\sum_{\forall t \in T} a_t = 1 \\
a_t \in [0,1] & \forallt \\
\label{eq5}\sum_{\forall t \in T} c_t \leq m \\
d - U_d(t, C) \leq (1-a_t) \cdot Z & \forallt \\
0 \leq k - U_a(t, C) \leq (1-a_t) \cdot Z & \forallt
\end{align+}
on lines \ref{eq1} and \ref{eq5}\dots
\end{document}