0

What is the best way to align equal signs and sum symbols in the following MWE?

\documentclass[a4paper]{report}
\usepackage{amsmath}
\begin{document}

\begin{equation}
\begin{alignedat}{4}
y_\text{L} = & \sum_{s=1}^n \left( Y_s \, \lambda_s \right) & \quad &
             \\
         x = & \sum_{s=1}^n \left( X_s \, \lambda_s \right) & \quad & x \geq 0
             \\
             & \sum_{s=1}^n \lambda_s = 1 & \quad & \lambda_s \geq 0
\end{alignedat}
\end{equation}

\end{document}

This is not perfect but works. As far as I know &= should be preferred over =& since the latter causes the space shrieked. But I cannot achieve desired alignment with &=.

  • Could you please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Could you also show an example of the output you get now and the output you want? – darthbith Sep 19 '14 at 11:51
  • On compiling, it looks OK to me. One might consider another layout if the last line doesn't have the same semantic status as the two above. – Bernard Sep 19 '14 at 12:02
  • It compiles, but the horizontal space between = and Σ is smaller than I would like (I'd like it to be as in the ordinary \begin{equation} y_\mathrm{L} = \sum_{s=1}^n (Y_s \, \lambda_s) \end{equation}). – MMSt Sep 19 '14 at 15:56
2

Please clarify what exactly you want to achieve! Here is your code how I would have written it:

\documentclass[a4paper]{report}
\usepackage{mathtools}
\begin{document}    
    \begin{equation}
    \begin{alignedat}{2}
    y_\mathrm{L} &= \sum_{s=1}^n (Y_s \, \lambda_s)\\
    x &= \sum_{s=1}^n (X_s \, \lambda_s) &&\qquad x \geq 0\\
    &\mathrel{\hphantom{=}} \sum_{s=1}^n \lambda_s = 1 &&\qquad \lambda_s \geq 0\\
    \sum_{s=1}^n \lambda_s &= 1 &&\qquad \lambda_s \geq 0
    \end{alignedat}
    \end{equation}  
\end{document}

You may decide if you want to have the last or the previous line.

enter image description here

Please note that the use of \left( and \right) is not needed here and does change the spacing. For this very case, \sum_{s=1}^nY_s\lambda_s would be enough to be understood right.

If you like, you may write x_{\phantom{s}}\geq 0 in order to get the inequalities aligned as well.

  • It looks very good. I'll go this way. Can you, please, explain how \mathrel{\hphantom{=}} differs from e.g. \phantom{{}={}} or \phantom{{}=}. – MMSt Sep 19 '14 at 15:38
0

very simple use &= in the first two equation on the third line you add \quad in front of equation as shown below:

\begin{alignedat}{4}

y_\text{L} &= \sum_{s=1}^n \left( Y_s \, \lambda_s \right) & \quad &\\

   x       &= \sum_{s=1}^n \left( X_s \, \lambda_s \right) & \quad & x \geq 0\\

         & \quad\sum_{s=1}^n \lambda_s = 1 & \quad & \lambda_s \geq 0
\end{alignedat}

All the three lines will be perfectly aligned.

  • This is the easiest way, indeed. But it is not 'perfectly' aligned. The \quad is a bit (5.2626 % :-)) to small. – LaRiFaRi Sep 19 '14 at 13:12
  • @LaRiFaRi is 100 % right. It's easy, but not perfectly aligned. – MMSt Sep 19 '14 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.