# More sophisticated page breaks within aligned equations

This is my current method of allowing page breaks between my aligned equations

{\allowdisplaybreaks
\begin{align*}
left &= right1 \\
&= right2.
\end{align*}}


It works, but it doesn't behave like I want it to.

Suppose the above example breaks between pages. The first line (on the first page) will come out correctly, because it centers based on the whole of "left = right1".

However, I want the second line (on the second page) to be centered with respect to only "= right2". (By default, it is centered taking the "left =" on the previous page into account, which doesn't look natural - the line appears to be more to the right than it should be.)

How can I accomplish this?

-
It would be very hard to do that in TeX, for the same reason it is very hard to have a different paragraph shape at the top of the the page. Math alignments and paragraph alignment happen first and then only later the output routine chops off pages at allowed break points. TeX never goes back and re-adjusts the horizontal material at that point. –  David Carlisle Aug 5 '12 at 9:40

Of course, David is right. Page breaking is done at a later time, when the decisions about the math alignments and paragraph building are already done.

The following example might provide a solution for the simplified example in the question. It contains two equations with two choices. Either the equations are on the same page or they are separated on two pages. This information provides a reference system. I am using module abspage of package zref, because the page numbers should then always be unique and they are numbers, \thepage might be something else.

But the labels cannot be set right away. Package amsmath sets his equations twice. In the first run it measures it and sets them in the second run with a correct placement of the equation number. Therefore we need to set the labels in the second run.

If we know the page numbers of the equations, we can set them differently.

\documentclass{article}
\usepackage{amsmath}
\usepackage{zref-abspage}

\makeatletter
\newcommand*{\amspagelabel}[1]{%
\ifmeasuring@
\else
\zref@labelbyprops{amspg:#1}{abspage}%
\fi
}
\newcommand*{\ifamspageequals}[2]{%
\zref@refused{amspg:#1}%
\zref@refused{amspg:#2}%
\ifnum\zref@extract{amspg:#1}{abspage}=%
\zref@extract{amspg:#2}{abspage}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}

\setlength{\textheight}{2.5\baselineskip}% for testing

\begin{document}

\allowdisplaybreaks

\ifamspageequals{A}{B}{%
\begin{align*}
\amspagelabel{A}
\text{left} &= \text{right1}\\
\amspagelabel{B}
&= \text{right2}
\end{align*}
}{
\begin{gather*}
\amspagelabel{A}
\text{left} = \text{right1}\\
\amspagelabel{B}
= \text{right2}
\end{gather*}
}

\end{document}


However the drawback is that the equations are written twice in the source code. This does not scale well. Each added equation doubles the choices. The equation can be on the same page as the previous equation or on a new page. Thus n equations require (n-1)2 choices.

At least it could be reduced to (n - 1) choices, if the cases are neglected, where the whole equation system spans more than two pages.

-
In practice, I guess I'm just going to have to break things up manually in the end, by doing the align first and then switching to a regular equation on the next page. Agreed? Seems like somewhat of a bummer. One would think that LaTeX allowed some kind of "going back" by means of auxiliary files, i.e. running the compiler more than once, in order to automate this process. –  MayGodBlessKnuth Aug 7 '12 at 2:10