# Any way to check with a macro if an equation is too long to fit into a single line?

I have some very long equations which span a few lines. I'd like to

1. Break lines automatically.
2. Pagebreak.
3. Have an equals sign both at the end of the first line and at the beginning of the second line.
4. Have only one number for the whole thing.

Environments I've tried:

1. split inside equation (manual line breaking, no pagebreaks)
2. dmath (no equals sign at the end of the first line and all workarounds lead to manual line breaking, no pagebreaks) (Putting an equal sign both at the end and at the beginning of the line (dmath))
3. aligned (manual line breaking, no pagebreaks)
4. align (manual line breaking, excessive numbers have to be removed manually, lack of centering looks awful to me (just took a look at my calculus textbook and it seems I'm not the only one who thinks multiline equations should be centered))
5. gather (manual line breaking, excessive numbers have to be removed manually)
6. multline (manual line breaking)

Of those 1-3 aren't usable because I'm left with half of a page blank. Of 4-6 multline seems the best one, alignment looks normal and it doesn't add numbers to every line.

I'd like to to write a macro which would take one parameter. This parameter would be an equation (without any \\ and \nonumber inserted). It would find all equals signs and, at every sign, figure out whether the equals sign should be replaced with =\\=. Then it would put everything inside multline environment.

So here's my question: is there any function which, given an equation as a parameter, could tell me whether it would fit in a single line or no?

-
have you tried breqn? not perfect (and it does need work), but perhaps useful. –  barbara beeton Jul 13 '14 at 13:13
align and friends allow page breaking if you issue \allowdisplaybreaks and don't number every line if you use align* rather than align –  David Carlisle Jul 13 '14 at 13:15
I've tried looking through the breqn package documentation but tried only dmath from it. The biggest problem with dmath was that it does not allow pagebreaks and automatic line breaking does not put the equals sign where I want it. Yes, I know align allows pagebreaks. I am using \allowdisplaybreaks. –  Liisi Jul 13 '14 at 13:18
Equations long enough to require this kind of formatting are likely to be hard on your reader. You seem to have lots of them, since you want a macro to do the job. Formatting each by hand might help minimize the work your readers need to do. So might extracting and naming subexpressions in other equations. –  Ethan Bolker Jul 13 '14 at 13:32
I hate writing text with the passion of a thousand blazing suns. I'm afraid that if I try to stick a line of text in between every two equatons I'll never get the thing written at all. Not to mention I won't be able to follow my own writing. I'm using a long bunch of equations linked by an equals sign only where it's reasonable, when I have to simplify a formula as a part of my proof and when all the transformations are more or less obvious. I wouldn't like to put useless distracting comments in between ("And now I am going to simplify this expression some more...") –  Liisi Jul 13 '14 at 13:41

This modifies multline to do (I think) as you ask. The result looks fairly unreadable to me, but perhaps I'm just not used to reading Russian articles.

multline like all AMS alignments grabs the environment body and sets it twice to measure it, this just modifies it so that if it is too wide on that first pass, = is redefined and the measuring pass is repeated.

You could probably do something similar with any of the amsmath alignments.

\documentclass{article}

\usepackage{amsmath}

\DeclareMathSymbol{\realequals}{\mathrel}{operators}{"3D}
\makeatletter
{\catcode=\active \gdef={\realequals{}\\{}\realequals} }
\def\multline@#1{%
\Let@
\chardef\dspbrk@context\z@
\restore@math@cr
\let\tag\tag@in@align
\global\tag@false \global\let\raise@tag\@empty
\mmeasure@{#1}%
\ifdim\totwidth@>.7\displaywidth
\mathcode=\mathcode' %
\mmeasure@{#1}%
\toks@{#1}%
\else
\toks@{\hfill#1\hfill}%
\fi
\let\tag\gobble@tag \let\label\@gobble
\tabskip \if@fleqn \@mathmargin \else \z@skip \fi
\totwidth@\displaywidth
\if@fleqn
\fi
\halign\bgroup
\hbox to\totwidth@{%
\if@fleqn
\hskip \@centering \relax
\else
\hfil
\fi
\strut@
\m@th\displaystyle{}##\endmultline@math \hfil }% \crcr \if@fleqn \hskip-\@mathmargin \def\multline@indent{\hskip\@mathmargin}% put it back \else \hfilneg \def\multline@indent{\hskip\multlinegap}% \fi \iftagsleft@ \iftag@ \begingroup \ifshifttag@ \rlap{\vbox{% \normalbaselines \hbox{% \strut@ \make@display@tag }% \vbox to\lineht@{}% \raise@tag }}% \multline@indent \else \setbox\z@\hbox{\make@display@tag}% \dimen@\@mathmargin \advance\dimen@-\wd\z@ \ifdim\dimen@<\multlinetaggap \dimen@\multlinetaggap \fi \box\z@ \hskip\dimen@\relax \fi \endgroup \else \multline@indent \fi \else \multline@indent \fi \the\toks@% } \begin{document} \begin{multline} ax^2 +bx^3 +dx^4 = (1+x)^9 -(a+x)(x+a)^3 = a+b =5 \end{multline} \begin{multline} a=5 \end{multline} \begin{multline} ax^2 +bx^3 +dx^4 +ex^5 = ax^2 +bx^3 +dx^4 = (1+x)^9 -(a+x)(x+a)^3 = a+b =5 \end{multline} \end{document}  or with first and last lines centred (which is still pretty hard to read) \documentclass{article} \usepackage{amsmath} \DeclareMathSymbol{\realequals}{\mathrel}{operators}{"3D} \makeatletter {\catcode=\active \gdef={\realequals{}\\{}\realequals} } \def\multline@#1{% \Let@ \@display@init{\global\advance\row@\@ne \global\dspbrk@lvl\m@ne}% \chardef\dspbrk@context\z@ \restore@math@cr \let\tag\tag@in@align \global\tag@false \global\let\raise@tag\@empty \mmeasure@{#1}% \ifdim\totwidth@>.7\displaywidth \mathcode=\mathcode' % \mmeasure@{#1}% \toks@{\hfil#1\hfil}% \else \toks@{\hfill#1\hfill}% \fi \let\tag\gobble@tag \let\label\@gobble \tabskip \if@fleqn \@mathmargin \else \z@skip \fi \totwidth@\displaywidth \if@fleqn \advance\totwidth@-\@mathmargin \fi \halign\bgroup \hbox to\totwidth@{% \if@fleqn \hskip \@centering \relax \else \hfil \fi \strut@\m@th\displaystyle{}##\endmultline@math
\hfil
}%
\crcr
\if@fleqn
\hskip-\@mathmargin
\def\multline@indent{\hskip\@mathmargin}% put it back
\else
\hfilneg
\def\multline@indent{\hskip\multlinegap}%
\fi
\iftagsleft@
\iftag@
\begingroup
\ifshifttag@
\rlap{\vbox{%
\normalbaselines
\hbox{%
\strut@
\make@display@tag
}%
\vbox to\lineht@{}%
\raise@tag
}}%
\multline@indent
\else
\setbox\z@\hbox{\make@display@tag}%
\ifdim\dimen@<\multlinetaggap
\dimen@\multlinetaggap
\fi
\box\z@ \hskip\dimen@\relax
\fi
\endgroup
\else
\multline@indent
\fi
\else
\multline@indent
\fi
\the\toks@%
}

\begin{document}

\begin{multline}
ax^2 +bx^3 +dx^4 =
(1+x)^9 -(a+x)(x+a)^3 =
a+b
=5
\end{multline}

\begin{multline}
a=5
\end{multline}

\begin{multline}
ax^2 +bx^3 +dx^4 +ex^5 =
ax^2 +bx^3 +dx^4 =
(1+x)^9 -(a+x)(x+a)^3 =
a+b
=5
\end{multline}

\end{document}

-
Thanks! As for unreadability, in some texts I've seen all the lines are centered, even the first and the last one. In some where the first one is aligned left and the last one is aligned right, they are long enough to look good. This one does look strange, but probably because the parts of the equation are so short. :) I'll try to modify it to go with gather. –  Liisi Jul 13 '14 at 14:08
@Liisi first and last lines are centred if you ad \hfil to the toks assignment, I'll update: –  David Carlisle Jul 13 '14 at 14:13
Does this code cause a line break at every equals sign? The first equation looks like it could fit on one line, two at most. If I wanted to do that I could just replace every equals sign in my equations with =\\=. –  Liisi Jul 13 '14 at 14:16
@Liisi if the natural width is more than 70% of the display area it breaks at every = if it is smaller than that it is left as one line. changing the 70% cutoff is trivial just change the \ifdim test making it break at some = and not others would be harder (you can manually override by using \realequals which will always giive you a = with no automatic break –  David Carlisle Jul 13 '14 at 14:19
@L they are not designed to be used they are internal macros of the ams alignments, I just "borrowed" them. –  David Carlisle Jul 13 '14 at 21:24

I did it! It is possible to measure the width of a formula in points, compare it to pagewidth and output different strings depending on the result of the comparison.

\documentclass{article}
\usepackage{amsmath}
\usepackage{ifthen}
\makeatletter
\def\measure#1{
\settowidth{\length}{$#1$}
\ifthenelse{\length>\textwidth}{output1}{output2}}
\newlength{\length}
\makeatother
\begin{document}
\end{document}


For some reasons all nice, recursive macros I tried failed. So here's an ugly but fully working solution. A macro for nine formulae did not fit into char limit, so I had to leave it out but it could be easily constructed based on the macro for eight formulae. Macros for more than nine formulae could be constructed by giving a single parameter and then using the xstring package to split it into ten or more parts where the equals sign is and then use a similar algorithm on those parts.

\documentclass{article}
\usepackage{amsmath}
\usepackage{ifthen}
\makeatletter
\def\gathereight#1#2#3#4#5#6#7#8{
\settowidth{\fiveeight}{$=#5=#6=#7=#8$}
\settowidth{\fiveseven}{$=#5=#6=#7=$}
\settowidth{\fivesix}{$=#5=#6=$}
\settowidth{\foureight}{$=#4=#5=#6=#7=#8$}
\settowidth{\fourfive}{$=#4=#5=$}
\settowidth{\fourseven}{$=#4=#5=#6=#7=$}
\settowidth{\foursix}{$=#4=#5=#6=$}
\settowidth{\oneeight}{$#1=#2=#3=#4=#5=#6=#7=#8$}
\settowidth{\onefive}{$#1=#2=#3=#4=#5=$}
\settowidth{\onefour}{$#1=#2=#3=#4=$}
\settowidth{\oneseven}{$#1=#2=#3=#4=#5=#6=#7=$}
\settowidth{\onesix}{$#1=#2=#3=#4=#5=#6=$}
\settowidth{\onethree}{$#1=#2=#3=$}
\settowidth{\onetwo}{$#1=#2=$}
\settowidth{\seveneight}{$=#7=#8$}
\settowidth{\sixeight}{$=#6=#7=#8$}
\settowidth{\sixseven}{$=#6=#7=$}
\settowidth{\threeeight}{$=#3=#4=#5=#6=#7=#8$}
\settowidth{\threefive}{$=#3=#4=#5=$}
\settowidth{\threefour}{$=#3=#4=$}
\settowidth{\threeseven}{$=#3=#4=#5=#6=#7=$}
\settowidth{\threesix}{$=#3=#4=#5=#6=$}
\settowidth{\twoeight}{$=#2=#3=#4=#5=#6=#7=#8$}
\settowidth{\twofive}{$=#2=#3=#4=#5=$}
\settowidth{\twofour}{$=#2=#3=#4=$}
\settowidth{\twoseven}{$=#2=#3=#4=#5=#6=#7=$}
\settowidth{\twosix}{$=#2=#3=#4=#5=#6=$}
\settowidth{\twothree}{$=#2=#3=$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2=\ifthenelse{\twothree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\foureight>\textwidth}{\\\nonumber=#8}{#8}}}}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\threesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\threeseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\threeeight>\textwidth}{\\\nonumber=#8}{#8}}}}}}{#3=\ifthenelse{\twofour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\foureight>\textwidth}{\\\nonumber=#8}{#8}}}}}{#4=\ifthenelse{\twofive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\twosix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\twoseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\twoeight>\textwidth}{\\\nonumber=#8}{#8}}}}}}}{#2=\ifthenelse{\onethree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\foureight>\textwidth}{\\\nonumber=#8}{#8}}}}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\threesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\threeseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\threeeight>\textwidth}{\\\nonumber=#8}{#8}}}}}}{#3=\ifthenelse{\onefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\foureight>\textwidth}{\\\nonumber=#8}{#8}}}}}{#4=\ifthenelse{\onefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\fiveeight>\textwidth}{\\\nonumber=#8}{#8}}}}{#5=\ifthenelse{\onesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\sixeight>\textwidth}{\\\nonumber=#8}{#8}}}{#6=\ifthenelse{\oneseven>\textwidth}{\\\nonumber=#7=\ifthenelse{\seveneight>\textwidth}{\\\nonumber=#8}{#8}}{#7=\ifthenelse{\oneeight>\textwidth}{\\\nonumber=#8}{#8}}}}}}}
\end{gather}}
\def\gatherfive#1#2#3#4#5{
\settowidth{\fourfive}{$=#4=#5$}
\settowidth{\onefive}{$#1=#2=#3=#4=#5$}
\settowidth{\onefour}{$#1=#2=#3=#4=$}
\settowidth{\onethree}{$#1=#2=#3=$}
\settowidth{\onetwo}{$#1=#2=$}
\settowidth{\threefive}{$=#3=#4=#5$}
\settowidth{\threefour}{$=#3=#4=$}
\settowidth{\twofive}{$=#2=#3=#4=#5$}
\settowidth{\twofour}{$=#2=#3=#4=$}
\settowidth{\twothree}{$=#2=#3=$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2=\ifthenelse{\twothree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5}{#5}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5}{#5}}}{#3=\ifthenelse{\twofour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5}{#5}}{#4=\ifthenelse{\twofive>\textwidth}{\\\nonumber=#5}{#5}}}}{#2=\ifthenelse{\onethree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5}{#5}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5}{#5}}}{#3=\ifthenelse{\onefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5}{#5}}{#4=\ifthenelse{\onefive>\textwidth}{\\\nonumber=#5}{#5}}}}
\end{gather}}
\def\gatherfour#1#2#3#4{
\settowidth{\onefour}{$#1=#2=#3=#4$}
\settowidth{\onethree}{$#1=#2=#3=$}
\settowidth{\onetwo}{$#1=#2=$}
\settowidth{\threefour}{$=#3=#4$}
\settowidth{\twofour}{$=#2=#3=#4$}
\settowidth{\twothree}{$=#2=#3=$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2=\ifthenelse{\twothree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4}{#4}}{#3=\ifthenelse{\twofour>\textwidth}{\\\nonumber=#4}{#4}}}{#2=\ifthenelse{\onethree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4}{#4}}{#3=\ifthenelse{\onefour>\textwidth}{\\\nonumber=#4}{#4}}}
\end{gather}}
\def\gatherone#1{
\begin{gather}
#1
\end{gather}}
\def\gatherseven#1#2#3#4#5#6#7{
\settowidth{\fiveseven}{$=#5=#6=#7$}
\settowidth{\fivesix}{$=#5=#6=$}
\settowidth{\fourfive}{$=#4=#5=$}
\settowidth{\fourseven}{$=#4=#5=#6=#7$}
\settowidth{\foursix}{$=#4=#5=#6=$}
\settowidth{\onefive}{$#1=#2=#3=#4=#5=$}
\settowidth{\onefour}{$#1=#2=#3=#4=$}
\settowidth{\oneseven}{$#1=#2=#3=#4=#5=#6=#7$}
\settowidth{\onesix}{$#1=#2=#3=#4=#5=#6=$}
\settowidth{\onethree}{$#1=#2=#3=$}
\settowidth{\onetwo}{$#1=#2=$}
\settowidth{\sixseven}{$=#6=#7$}
\settowidth{\threefive}{$=#3=#4=#5=$}
\settowidth{\threefour}{$=#3=#4=$}
\settowidth{\threeseven}{$=#3=#4=#5=#6=#7$}
\settowidth{\threesix}{$=#3=#4=#5=#6=$}
\settowidth{\twofive}{$=#2=#3=#4=#5=$}
\settowidth{\twofour}{$=#2=#3=#4=$}
\settowidth{\twoseven}{$=#2=#3=#4=#5=#6=#7$}
\settowidth{\twosix}{$=#2=#3=#4=#5=#6=$}
\settowidth{\twothree}{$=#2=#3=$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2=\ifthenelse{\twothree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7}{#7}}}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\threesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\threeseven>\textwidth}{\\\nonumber=#7}{#7}}}}}{#3=\ifthenelse{\twofour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7}{#7}}}}{#4=\ifthenelse{\twofive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\twosix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\twoseven>\textwidth}{\\\nonumber=#7}{#7}}}}}}{#2=\ifthenelse{\onethree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7}{#7}}}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\threesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\threeseven>\textwidth}{\\\nonumber=#7}{#7}}}}}{#3=\ifthenelse{\onefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fourseven>\textwidth}{\\\nonumber=#7}{#7}}}}{#4=\ifthenelse{\onefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\fiveseven>\textwidth}{\\\nonumber=#7}{#7}}}{#5=\ifthenelse{\onesix>\textwidth}{\\\nonumber=#6=\ifthenelse{\sixseven>\textwidth}{\\\nonumber=#7}{#7}}{#6=\ifthenelse{\oneseven>\textwidth}{\\\nonumber=#7}{#7}}}}}}
\end{gather}}
\def\gathersix#1#2#3#4#5#6{
\settowidth{\fivesix}{$=#5=#6$}
\settowidth{\fourfive}{$=#4=#5=$}
\settowidth{\foursix}{$=#4=#5=#6$}
\settowidth{\onefive}{$#1=#2=#3=#4=#5=$}
\settowidth{\onefour}{$#1=#2=#3=#4=$}
\settowidth{\onesix}{$#1=#2=#3=#4=#5=#6$}
\settowidth{\onethree}{$#1=#2=#3=$}
\settowidth{\onetwo}{$#1=#2=$}
\settowidth{\threefive}{$=#3=#4=#5=$}
\settowidth{\threefour}{$=#3=#4=$}
\settowidth{\threesix}{$=#3=#4=#5=#6$}
\settowidth{\twofive}{$=#2=#3=#4=#5=$}
\settowidth{\twofour}{$=#2=#3=#4=$}
\settowidth{\twosix}{$=#2=#3=#4=#5=#6$}
\settowidth{\twothree}{$=#2=#3=$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2=\ifthenelse{\twothree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6}{#6}}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\threesix>\textwidth}{\\\nonumber=#6}{#6}}}}{#3=\ifthenelse{\twofour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6}{#6}}}{#4=\ifthenelse{\twofive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\twosix>\textwidth}{\\\nonumber=#6}{#6}}}}}{#2=\ifthenelse{\onethree>\textwidth}{\\\nonumber=#3=\ifthenelse{\threefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6}{#6}}}{#4=\ifthenelse{\threefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\threesix>\textwidth}{\\\nonumber=#6}{#6}}}}{#3=\ifthenelse{\onefour>\textwidth}{\\\nonumber=#4=\ifthenelse{\fourfive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\foursix>\textwidth}{\\\nonumber=#6}{#6}}}{#4=\ifthenelse{\onefive>\textwidth}{\\\nonumber=#5=\ifthenelse{\fivesix>\textwidth}{\\\nonumber=#6}{#6}}{#5=\ifthenelse{\onesix>\textwidth}{\\\nonumber=#6}{#6}}}}}
\end{gather}}
\def\gatherthree#1#2#3{
\settowidth{\onethree}{$#1=#2=#3$}
\settowidth{\onetwo}{$#1=#2=$}
\settowidth{\twothree}{$=#2=#3$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2=\ifthenelse{\twothree>\textwidth}{\\\nonumber=#3}{#3}}{#2=\ifthenelse{\onethree>\textwidth}{\\\nonumber=#3}{#3}}
\end{gather}}
\def\gathertwo#1#2{
\settowidth{\onetwo}{$#1=#2$}
\begin{gather}
#1=\ifthenelse{\onetwo>\textwidth}{\\\nonumber=#2}{#2}
\end{gather}}
\def\rbr#1{\left(#1\right)}
\newlength{\fiveeight}
\newlength{\fiveseven}
\newlength{\fivesix}
\newlength{\foureight}
\newlength{\fourfive}
\newlength{\fourseven}
\newlength{\foursix}
\newlength{\oneeight}
\newlength{\onefive}
\newlength{\onefour}
\newlength{\oneseven}
\newlength{\onesix}
\newlength{\onethree}
\newlength{\onetwo}
\newlength{\seveneight}
\newlength{\sixeight}
\newlength{\sixseven}
\newlength{\threeeight}
\newlength{\threefive}
\newlength{\threefour}
\newlength{\threeseven}
\newlength{\threesix}
\newlength{\twoeight}
\newlength{\twofive}
\newlength{\twofour}
\newlength{\twoseven}
\newlength{\twosix}
\newlength{\twothree}
\makeatother
\begin{document}
\gatherfour{ax_2+bx^3+dx^4}{\rbr{1+x}s^9-\rbr{a+x}\rbr{x+a}^3}{a+b}{5}
\gathertwo{a}{5}
\gatherfive{ax^2+bx^3+dx^4+ex^5}{ax^2+bx^3+dx^4}{\rbr{1+x}^9-\rbr{a+x}\rbr{x+a}^3}{a+b}{5}
\end{document}


One could easily modify the location of equation number (by changing where \nonumber is) and how much of pagewidth do you allow to get filled up (by tinkerng with conditions).

-