2

I have a long equation in Latex and I need to break it into lines. The multiline environment works fine except the first line is indented in an annoying way.

My equation has a long left-hand side which is a sum of nine terms.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{multline}a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{multline}
\end{document}

It would be nice to break it up into three lines of three terms each, aligned so that terms 1, 4, 7 are placed above each other, and so are 2, 5, 8 and 3, 6, 9. The RHS is only 0, so it can be left (um, right I mean...) on the last line. What I get instead is the first line indented - shifted towards the right - the way it is often done in paragraphs of text. The other two lines are aligned as I wanted.

I have tried a number of align commands, but to no avail.

Xirdal
  • 67
  • 1
  • 10
  • What is the multiline environment? Maybe you mean 'multline' from amsmath? Or maybe you use non standard packages? In general a minimal working example with all the packages is required so that people can reproduce the problem. You can [edit] your question to add it. – Alain Merigot Jun 26 '19 at 10:51
  • Thank you for spotting the typo. I was indeed using multiline, whatever that is, but when I changed it to multline it's no better - I get the stairsteps-shaped layout typical of multline, still not the "stacked" alignment I would like. I have edited my post to include the full code. – Xirdal Jun 26 '19 at 11:17

3 Answers3

2

There are many ways to align lines in an equation and I do not know what you want.

Here are some possibilities:

  • eqnarray from vanilla LaTeX (but spacing is weird as you can see and its use is not recommanded).
  • flalign from amsmath
  • multline from amsmath that you used. Its standard behavior is to flush left first line, to flush right the last one and to center the others. You can use \shoveright or \shoveleft to flush a line differently and control its position with additional spaces (\qquad or others)
  • equation+split from amsmath with different ways to provide an alignment depending on the position of the alignment tabs
  • or you can just left latex decide where to to line breaking with the breqn package and the dmath environment.
\documentclass{article}
\usepackage{amsmath}
\usepackage{breqn}
\begin{document}

Eqnarray
\begin{eqnarray}
\notag a^2 X_1 X_2  &+& ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
\notag&&a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
&&a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{eqnarray}

flalign
\begin{flalign}
\notag a^2 X_1 X_2  + &ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +&\\
\notag&a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +&\\
&a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{flalign}


multline
\begin{multline}
a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{multline}

multline+shovedright
\begin{multline}
a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
\shoveright{a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\qquad}\\
a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{multline}

equation+split
\begin{equation}
  \begin{split}
a^2 X_1 X_2  &+ ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
 &a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
 &a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0\\   
  \end{split}
\end{equation}

equation+split flushed left
\begin{equation}
  \begin{split}
 & a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
 &a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
 &a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0\\   
\end{split}
\end{equation}

equation+split flushed  right
\begin{equation}
  \begin{split}
 a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +&\\
 a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +&\\
 a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0\\   
  \end{split}
\end{equation}

breqn+dmath
\begin{dmath}
a^2 X_1 X_2   + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +
 a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +
 a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{dmath}
\end{document}

enter image description here

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31
1

Quote from the amsmath documentation:

Like multline, the split environment is for single equations that are too long to fit on one line and hence must be split into multiple lines. Unlike multline, however, the split environment provides for alignment among the split lines, using & to mark alignment points.

So if you want to align the lines, you can try with split:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\[
\begin{split}
a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{split}
\]

\[
\begin{split}
&a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
&a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
&a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{split}
\]
\end{document}

To get alignment on a term-by-term basis:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\[
\begin{aligned}
a^2 X_1 X_2  + &&ab (X_1Y_2+X_2Y_1) &&+b^2 Y_1 Y_2 &&+\\
a^2 X_5 X_6  + &&ab (X_5Y_6+X_6Y_5) &&+ b^2 Y_5 Y_6 &&+\\
a^2 X_9 X_{10} +&&ab (X_9Y_{10}+X_{10}Y_9) &&+ b^2 Y_9 Y_{10} &&= 0
\end{aligned}
\]
\end{document}
  • 1
    May you live forever, may your offspring grow healthy, and may your camels never wander astray. The version with the ampersand at the beginning of each line worked like a charm. (without the ampersand, it also worked OK in the example file, but in my full text, for whatever reason, it flushed right leaving the bottom line sticking out to the left because of the "=0" at the end. Thanks again! – Xirdal Jun 26 '19 at 13:23
1

As per the official docs (page 9 of the PDF), for multiline equations with alignment, use the split environment:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation}
\begin{split}
  &a^2 X_1 X_2  + ab (X_1Y_2+X_2Y_1) + b^2 Y_1 Y_2 +\\
  &a^2 X_5 X_6  + ab (X_5Y_6+X_6Y_5) + b^2 Y_5 Y_6 +\\
  &a^2 X_9 X_{10} + ab (X_9Y_{10}+X_{10}Y_9) + b^2 Y_9 Y_{10} = 0
\end{split}
\end{equation}

\end{document}

The output:

enter image description here

Das_Geek
  • 2,775
  • 7
  • 20
  • 26