[Tex/LaTex] Displaymath environment that can align equations and squeezes math

amsmathequationshorizontal alignmentmath-modespacing

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}

enter image description here

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).

Best Answer

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.

enter image description here