# Displaymath environment that can align equations and squeezes math

I have a long equation to align. Without squeezing the math, it is just barely longer than \textwidth. However, the align environment doesn't squeeze the math at all, which results in a badbox.

\documentclass[11pt]{article}
\usepackage{amsmath}
\begin{document}
Using \verb|$...$|'':
$1+2+3+4+5+6+7+8+9+10 = 11+12+13+14+15+16+17+18+19+20+21$

Using align'':
\begin{align*}
1+2+3+4+5+6+7+8+9+10
&= 11+12+13+14+15+16+17+18+19+20+21\\
&= 11+12+13+14+15+16+17+18+19+20+21
\end{align*}
\end{document}


## Question:

How can I align equations in a displaymath environment that will also squeeze the math in order to avoid a badbox?

I want the best of both worlds:

1. the squeezing of math (as when using $...$) and
2. the ability to align equations (as when using align).
-
Could you show something more context? If you have just one line, the answer is “use $...$”. The difference is due to the fact that align boxes the material, so the spaces are frozen. – egreg Jan 17 '14 at 20:23
@egreg Of course I don't have just one line. I updated my example. How is that? – Tyson Williams Jan 17 '14 at 20:39
I think that having an environment that lets you split math material across lines and squeezes the material on various lines (possibly to varying degrees) to make it fit into the available text block poses a serious, nay, a tremendous peril to good (math) typography. One of the main objectives of good typography is to create the appearance of even "color" -- better, "grayness" -- on a page. Having a modified align environment that produces different color on adjacent lines because of differing amounts of squeezing going on almost surely wrecks any chance of getting even color. – Mico Jan 20 '14 at 6:36
@Mico I think I understand your concern. In my example, suppose that I removed half of the terms on the RHS of the first line. Then where should the squeezing occur?... just on the LHS and RHS of the second line or also in the RHS of the first line? I think the correct answer depends on how similar the math in the two RHSs. In this case, they are extremely similar, so I think it would look better if both RHSs were squeezed to keep the +s aligned. However, in another situation, I can see how squeezing the math in a shorter side would look bad. – Tyson Williams Jan 20 '14 at 14:12
This complexity makes me think that there is no environment that will do this for me automatically. In my actual situation, I think it is clear that the short side should not have its math squeezed. So if no general answer is possible, I will figure out a hack to get this behavior for my situation. – Tyson Williams Jan 20 '14 at 14:25

In order to build multiline displays, TeX must box the pieces in order to measure them. This fixes the glue around binary operations (the only one having shrinkability, if you don't use flexible \hspace in the formula) to their natural width. On the contrary, when typesetting a single equation with a natural width exceeding the available space, TeX just does something similar to

\hbox to \displaywidth{$\displaystyle <formula>$}


and so the glue can shrink.

A general solution for your problem would require multiple passes over the material in an align: if one of the lines results in an overfull the boxes on that line must be retypeset with a shrinkage factor and the alignment must be retried.

The TeXnical complications are intimidating, I should say. And the result wouldn't be worthy the trouble, probably: for a single equation, a slight shrinking can go almost unnoticed, in a display it would create very dissimilar lines next to each other: a line with a high shrinking is visually incompatible with one that has no shrinking. So a real solution should also apply less shrinkage to the lines next to the cause of the whole business; and so on.

Maybe a single case can be solved in this way, the general answer is: such a device is difficult to build and probably as useless as a Rube Goldberg machine. For a single tough equation where we don't need beautiful typography, here'a a possibility: a command \sq (for “squeeze”) that can set the \medmuskip to the desired value.

\documentclass[11pt,draft]{article}
\usepackage{amsmath}
\newcommand{\sq}[2][0]{% "sq" for "squeeze"
\mbox{$\medmuskip=#1mu\displaystyle#2$}%
}

\begin{document}
Using \verb|$...$|'':
$1+2+3+4+5+6+7+8+9+10 = 11+12+13+14+15+16+17+18+19+20+21$
Using align'':
\begin{align*}
\sq{1+2+3+4+5+6+7+8+9+10}
&=\sq{11+12+13+14+15+16+17+18+19+20+21}\\
\sq[.1]{1+2+3+4+5+6+7+8+9+10}
&=\sq[.1]{11+12+13+14+15+16+17+18+19+20+21}\\
\sq[.2]{1+2+3+4+5+6+7+8+9+10}
&=\sq[.2]{11+12+13+14+15+16+17+18+19+20+21}\\
\sq[.3]{1+2+3+4+5+6+7+8+9+10}
&=\sq[.3]{11+12+13+14+15+16+17+18+19+20+21}
\end{align*}
\end{document}


Only the last line is overfull by 4pt.

-
Also there is the issue readability, if you have a construction where stretching comes into play to determine if it fits on a page. It is probably better for the reader to rewrite the equation. – daleif Jan 23 '14 at 11:20
@daleif I agree. – egreg Jan 23 '14 at 11:26
I agree with you (egreg) that it would probably take at least three passes to get this behavior, which is probably too many. Ultimately, I ended up rewriting the equation as part of a larger edit to simplify things. This answer gets accepted because it taught me something. – Tyson Williams Jan 28 '14 at 14:31

Use the multline from amsmath package, a numbered environment (or its starred version to unnumbered)

\documentclass{report}
\usepackage{amsmath}

\begin{document}

\begin{multline*}
1+2+3+4+5+6+7+8+9+10+11+12+13+14\\ +15+16+17+18+19+20+21
\end{multline*}
\end{document}


If you want to align from left just start each line with &

\begin{align*}
& 1+2+3+4+5+6+7+8+9+\\
& 10+11+12+13+14+15+16+17+18+19+\\
& 20+21
\end{align*}


Or just break each line to align to right.

\begin{align*}
1+2+3+4+5+6+7+8+9\\
+10+11+12+13+14+15+16+17+18+19\\
+20+21
\end{align*}

-
How do you align? Also, this displaymath environment doesn't appear to squeeze math. – Tyson Williams Jan 17 '14 at 19:39
I don't think you read my question very well. I want to align AND have math squeezed. Neither multiline nor align satisfies these conditions. – Tyson Williams Jan 17 '14 at 19:45
What do you mean by squeeze? I'm not getting bad box with that example. – Sigur Jan 17 '14 at 19:47
The goal is NOT to avoid a badbox my any means, it is to avoid a badbox by having the math squeezed. See the difference in the amount of space around the + signs in my example. – Tyson Williams Jan 17 '14 at 19:49
If you don't break the line it is not possible to fit the whole expression. It is too long, even using negative spaces between the characters. – Sigur Jan 17 '14 at 19:51

i'm not sure that what you want is possible.

in order to align multiple disparate equations, it is necessary to measure the pieces. in order to measure a piece, it is necessary to put it in a box. putting something in a box "freezes" the spacing.

in order to verify this, we can check the mechanism available with plain tex to set multi-line math. here is the result, followed by the input.

\hsize=30pc
\def\folio{}
\def\cs#1{{\tt\char\\#1}}

The first (one-line) equation is too wide for the page, but
it is automatically squeezed by \TeX\ ({\sl\TeX boook}, pp.188-189).
First, set it in an \cs{hbox} to show the natural width.

\noindent
\hbox{$1+2+3+4+5+6+7+8+9+10 = 11+12+13+14+15+16+17+18+19+20+21$}

Now set it as a math display.
$$1+2+3+4+5+6+7+8+9+10 = 11+12+13+14+15+16+17+18+19+20+21$$

Expand it to multiple lines using \cs{eqalign} ({\sl\TeX book}, p.190).
\eqalign{ 1+2+3+4+5+6+7+8+9+10 &= 11+12+13+14+15+16+17+18+19+20+21\cr &= 21+22+23+24+25\cr &= 11+12+13+14+15+16+17+18+19+20+21\cr }

\bye


this should not be a surprise, since the texbook says (p.190; double-dangerous bend):

TeX also allows "alignment displays," which are not processed in math mode because they contain no formulas at the outer level.

since squeezing of multi-line displays is not possible in plain tex, i don't hold out much hope that it can be made available, much less automatic, in latex.

perhaps something could be concocted with luatex, but (depending on the result of exploring what happens using plain tex) that might no longer match the specs of the program described in the texbook.

-
No offense to luatex, but I use pdflatex and won't switch just to have an elegant solution to this problem. I figured out a hack for my specific situation that I would use instead, which is to manually squeeze the math in select locations using \!. Not only does this hack not scale, it doesn't look perfect because the space around operators varies within the same line of math. – Tyson Williams Jan 23 '14 at 3:22