# How to construct a long equation that is split in LHS and RHS to occupy a narrow column?

I have a homework document using 2-column article document class as follows.

\documentclass[twocolumn]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{amsmath}
\begin{document}
\begin{enumerate}
\item The degree of (C) is 3.
\item The degree of (A) is 1.
\item
\begin{aligned}[t] \begin{split} -3x(x+1)&-2x(x-1) \\ &+4(x^2-3x-1) \end{split} &= \begin{split} -3x^2&-3x-2x^2+2x\\ &+4x^2-12x-4 \end{split}\\ &= -x^2-13x-4 \end{aligned}
\end{enumerate}
\end{document}


What I want to achieve are:

• the long equation is split in both LHS and RHS.
• the = is consistently aligned.

The output I want to achieve roughly looks like the following "screenshot".

The red lines represent the first two items and the green one represents the long equation. The black line represents the hypothetical column separator.

How to construct a long equation that is split in LHS and RHS to occupy a narrow column?

-
Is this supposed to be a compilable document? –  mafp Jan 29 '13 at 16:50
@mafp: No. That is why I provide a sketch. –  Oh my ghost Jan 29 '13 at 16:54
Do you mean that the document should have left and right columns, and that once the text / equations have gotten to the bottom of the left column, it should start again on the right? Or is the right column meant to remain blank? –  Niel de Beaudrap Jan 29 '13 at 16:59
@NieldeBeaudrap: The right column will be filled with the next enumerate items that are either equations or texts. It does not matter if an equation split across column. –  Oh my ghost Jan 29 '13 at 17:01

This is based on mafp's answer, but doesn't use aligned in the left. Note the \! in front of \begin{aligned} and the {}+, which are needed for proper spacing!

\documentclass[twocolumn]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{amsmath}
\begin{document}
\begin{enumerate}
\item The degree of (C) is 3.
\item The degree of (A) is 1.
\item%
\!\begin{aligned}[t] -3x(x+1)-2x(x-1) \\ {}+4(x^2-3x-1) &= \!\begin{aligned}[t] -3x^2-3x-2x^2+2x \\ {}+4x^2-12x-4 \end{aligned} \\ &= -x^2-13x-4 \end{aligned}
\end{enumerate}
\end{document}

-
Does the outer aligned need \! as well for proper spacing? –  Oh my ghost Jan 29 '13 at 18:50
@GarbageCollector: Right you are! Corrected. –  Hendrik Vogt Jan 30 '13 at 8:48

The following is a variant of mafp's answer, in which the spacing of the aligned environment relative to the enumeration label is fixed. This is somewhat more complicated than Hendrik's answer, but achieves the alignment on + and - operators which you seemed to want in your original example.

## #1. Basic solution

If there is nothing particularly large on the first line of the equation, the following will suffice.

\documentclass[twocolumn]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{amsmath}
\setlength\columnseprule{0.5pt}
\begin{document}
\begin{enumerate}
\item The degree of (C) is 3.
\item The degree of (A) is 1.
\item \strut\\[\dimexpr-\baselineskip-\lineskip]\strut
\begin{aligned}[t]\! \begin{aligned}[b]\! -3x(x+1) &- 2x(x-1) \\ &+ 4(x^2-3x-1) \end{aligned} &= \begin{aligned}[t]\! -3x^2 &- 3x - 2x^2 + 2x \\ &+ 4x^2 - 12x - 4 \end{aligned} \\ &= -x^2 - 13x - 4 \end{aligned}
\end{enumerate}
\end{document}


The magic line \strut\\[\dimexpr-\baselineskip-\lineskip]\strut forces a line break in order to force the top line of the LHS to sit below the first line of the enumeration item; the \strut commands (a technique suggested by David Carlisle for forcing the vertical spacing between the lines to be well-defined in just such a context) allow us to pull that first line back up to the level of the enumeration item by undoing the vertical space and the glue width.

## #2. General solution

If you have any unusually tall elements in the first line of the math, such as

\left( \sum_\substack{a\\b} \right)


then the previous solution will not quite work: the top of the tallest element will be pulled to the top of the line of text of the enumeration item. You can get around that by \smashing it, but then it has a good chance of overlapping with the previous enumeration item.

In this case, a little bit of manual effort is required, but it should give a fully general solution. What need to do is make a copy of the tall math elements in a box so that TeX can measure its height, and then use that height both to make the appropriate accomodations for space in your enumeration environment, and to make the spacing corrections after the newline. Thanks to Andrew Swann for indicating the correct way to adjust the spacing using \dp\strutbox (see towards the bottom of the comment thread there) in this case.

\documentclass[twocolumn]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{amsmath}
\setlength\columnseprule{0.5pt}
\begin{document}
\begin{enumerate}
\item The degree of (C) is 3.
\item The degree of (A) is 1.

\savebox0{$\displaystyle \left( \sum_{\substack{\text{very low} \\ \text{subscripts}}} \text{math} \right)$}
\vspace{\dimexpr\ht0-\baselineskip}
\item \strut\\[\dimexpr-\ht0-\lineskip-\dp\strutbox]\strut
\begin{aligned}[t]\! \begin{aligned}[b]\! -3x(x+1) & \left( \sum_{\substack{\text{very low} \\ \text{subscripts}}} \text{math} \right) \\ &+ 4(x^2-3x-1) \end{aligned} &= \begin{aligned}[t]\! -3x^2 &- 3x - 2x^2 + 2x \\ &+ 4x^2 - 12x - 4 \end{aligned} \\ &= -x^2 - 13x - 4 \end{aligned}
\end{enumerate}
\end{document}


-
Why did you put \! after aligned rather than before it? –  Oh my ghost Aug 30 '13 at 1:25

These two versions do not completely resembles your drawing, but might be worthwhile.

\documentclass[twocolumn]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{amsmath}
\begin{document}
\begin{enumerate}
\item The degree of (C) is 3.
\item The degree of (A) is 1.
\item%
\begin{aligned}[t] \begin{aligned}[b] -3x(x+1)-2x(x-1) \\ {}+4(x^2-3x-1) \end{aligned} &= \begin{aligned}[t] -3x^2-3x-2x^2+2x\\ {}+4x^2-12x-4 \end{aligned}\\ &= -x^2-13x-4 \end{aligned}
\item Some text
\begin{align*}
\begin{aligned}[b]
-3x(x+1)-2x(x-1) \\
{}+4(x^2-3x-1)
\end{aligned}
&=
\begin{aligned}[t]
-3x^2-3x-2x^2+2x\\
{}+4x^2-12x-4
\end{aligned}\\
&= -x^2-13x-4
\end{align*}
\end{enumerate}
\end{document}

-
The alignment [t] does not work properly. –  Oh my ghost Jan 29 '13 at 17:35
@GarbageCollector You mean the first one? Well, it does work somehow, but only considers the second line of the alignment block. I am not sure whether this is intended, or a bug. Maybe barbara beeton knows. –  mafp Jan 29 '13 at 17:39
@GarbageCollector: in fact, it is working perfectly: the baseline of the first line of the aligned environment is the same as the enumeration label. The problem is that within the first line is another aligned environment which has multiple lines, and which aligns its baseline on the final line -- causing its first line to sit above the baseline of the enumeration label. I've been puzzling over "the right way" to fix that problem myself. It's easy enough to fudge with something like ~\\[-2.25ex] before the math, but that's not a robust way to do things. –  Niel de Beaudrap Jan 29 '13 at 17:41
Note that aligned has the very nasty property that it adds a \, at its beginning, so it's best to precede it by \!. See also my answer. (Ping, @Niel.) –  Hendrik Vogt Jan 29 '13 at 17:56
{} should be added before the +4 beginning the second line of the left-hand expression to indicate that it is a continuation. –  barbara beeton Jan 29 '13 at 18:10

The current solutions do not address the question of the right-aligment of subsequent lines. One possibility is the used the multlined environment from mathtools. This will involve speficfying or determining an extra width. In the example below individual right alignment within the two items is demonstrated.

\documentclass{article}

\usepackage{mathtools}

\newlength{\mylonglth}
\setlength{\multlinegap}{0pt}

\begin{document}

\begin{enumerate}
\item The degree of (C) is 3.
\item The degree of (A) is 1.
\item%
\! \begin{aligned}[t] -3x(x+1)-2x(x-1)&\\ {}+4(x^2-3x-1) &= \! \begin{multlined}[t][0.35\textwidth] {-}3x^2-3x-2x^2+2x\\ {}+4x^2-12x-4 \end{multlined} \\ &= -x^2-13x-4\\ &= \! \begin{multlined}[t][0.35\textwidth] {-}3x^2-3x+2x\\ \shoveright{{}+3x-55}\\ {}+4x^2-12x-4 \end{multlined} \end{aligned}
\item%
\settowidth{\mylonglth}{${-}3x^2-3x-2x^2+2x$}%
\! \begin{aligned}[t] -3x(x+1)-2x(x-1)&\\ {}+4(x^2-3x-1) &= \! \begin{multlined}[t][\mylonglth] {-}3x^2-3x-2x^2+2x\\ {}+4x^2-12x-4 \end{multlined} \\ &= -x^2-13x-4\\ &= \! \begin{multlined}[t][\mylonglth] {-}3x^2-3x+2x\\ \shoveright{{}+3x-55}\\ {}+4x^2-12x-4 \end{multlined} \end{aligned}
\end{enumerate}

\end{document}

-
Is & explicitly needed in &\\? –  Oh my ghost Apr 16 '13 at 11:08
@Bugbusters Apperently not, but it helps me keep track of which alignment point is being used. –  Andrew Swann Apr 16 '13 at 12:23