# Add a marginal note when a page break occurs in the middle of a multi-line equation

Thanks to the amsmath's \allowdisplaybreaks macro, it is possible to let page breaks fall where they may, even in the middle of multi-line equations (from e.g. the align environment).

• Advantage of \allowdisplaybreaks: otherwise (prohibiting page breaks in the middle of multi-line equations), the document is likely to contain very ugly underfull vboxes.
• Drawback of \allowdisplaybreaks: "we haven't a view on all the equation in this case", as said in Allow displaybreak only from even to odd pages.

In order to reduce the above drawback, a solution could be to add a marginal note (containing something like "eq. follows next page") when a page break occurs in a multi-line equation.

Is it possible to achieve such a result, e.g. for the align environment?

## Edit

Here is a MWE of what I'd like to achieve, easily obtained by hand but that I'd like obtained automatically. It contains a footnote and a floating figure in order to point out that a marginal note located at the bottom (right) of the page wouldn't be enough since it could be far from the line where the equation is broken.

\documentclass{report}
\usepackage{amsmath}
\usepackage{mwe}
\usepackage{marginnote}
\usepackage[papersize={130mm,140mm},margin=30mm]{geometry}

\allowdisplaybreaks

\newcommand{\eqcont}{\tiny(Cont.\ next page)}

\begin{document}
Foo\footnote{\tiny\lipsum[1]}
\begin{multline}
x = abcdefghijklmopqrstuvwyz                    \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz\marginnote{\eqcont} \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz                     \\
+ abcdefghijklmopqrstuvwyz
\end{multline}

\begin{figure}[b]
\centering
\includegraphics[height=1cm]{image-a}
\caption{A nice figure}
\label{nice-figure}
\end{figure}

\begin{align}
1 + 1 & = 2                     \\
2 + 1 & = 3                     \\
3 + 1 & = 4                     \\
4 + 1 & = 5                     \\
5 + 1 & = 6                     \\
6 + 1 & = 7\marginnote{\eqcont} \\
7 + 1 & = 8                     \\
8 + 1 & = 9                     \\
9 + 1 & = 10
\end{align}
\end{document}


• you can set a \label on the first and last equation and then (with a bit of care or using ifthenelse which takes care for you) compare the pageref of the two labels and do something if they are not the same... – David Carlisle Oct 18 '17 at 16:46
• @DavidCarlisle Does this trick work with non numbered equations (align* environment?). And could this trick be automatized (the document contains a lot of align(*) environments)? – Denis Bitouzé Oct 18 '17 at 16:48
• It doesn't matter if the equations are numbered or not, as you are comparing \pageref. – Ulrike Fischer Oct 18 '17 at 16:57
• @DavidCarlisle Unfortunately, this wouldn't work for a multline equation where at most one \label may be inserted. – Denis Bitouzé Oct 20 '17 at 20:00
• details:-), you don't really need an equation number or real \label you can just use some internal version of \label multline doesn't know about, so long as you can write two things to the aux file and check if they are the same or different. – David Carlisle Oct 20 '17 at 20:02

Perhaps something like this:

I have used \appto (and \csappto) hooks from the etoolbox to set and unset a boolean flag, ifInAlign, in the align and align* environments (other environments can be done in exactly the same way). The idea is that \ifInAlign is true when you are inside an align environment and false otherwise. Then, using the background package I add "background text" to each page that is given by

\ifInAlign\tiny Equation continues...\fi


That is, we see Equation continues... when the Align environment extends across the page and otherwise we see nothing.

Here is the full code:

\documentclass{article}
\usepackage{amsmath}
\usepackage[papersize={100mm,80mm}, margin=20mm]{geometry}% to get short pages

\newif\ifInAlign\InAlignfalse% to determine if we're inside an align environment

\usepackage{etoolbox}
\appto\align{\global\InAligntrue}% InAlign true at start of align
\csappto{align*}{\global\InAligntrue}% InAlign true at start of align*
\appto\endalign{\global\InAlignfalse}% InAlign false at end

\usepackage{background}% add continued note when InAlign
\backgroundsetup{angle=0,
scale=1,
color=black,
position={0.68\textwidth,-0.95\textheight},
contents={\ifInAlign\tiny Equation continues...\fi}
}

\everymath{\allowdisplaybreaks[4]}% turn on display breaks, always!
\begin{document}

\begin{align}
0 + 1 &= 1\\% some deep multi-lined aligned equations
1 + 1 &= 2\\
2 + 1 &= 3\\
3 + 1 &= 4\\
4 + 1 &= 5\\
5 + 1 &= 6\\
6 + 1 &= 7\\
7 + 1 &= 8\\
8 + 1 &= 9\\
9 + 1 &= 10\\
10 + 1 &= 11\\
11 + 1 &= 12\\
12 + 1 &= 13\\
13 + 1 &= 14\\
14 + 1 &= 15\\
15 + 1 &= 16\\
16 + 1 &= 17\\
17 + 1 &= 18\\
18 + 1 &= 19\\
19 + 1 &= 20
\end{align}

\end{document}


To guarantee that the equation breaks across pages I used the geometry package to make the page size quite small.

• Using the background package to perform a (conditional) action at each page is a very nice idea! Would be even better if the text could be located where would be an (outer) marginal note on the last line of the page: maybe not a big deal thanks to the tikzpagenodes package (and, in such a case, maybe could be enough to use the everypage package loaded by background: I'll have a look). – Denis Bitouzé Oct 18 '17 at 20:51
• I meant "right" and not "outer" margin. – Denis Bitouzé Oct 19 '17 at 4:59
• In fact this trick is not as satisfactory as expected: if the page where the break occurs contains a (possibly long) footnote, the wording "Equation continues..." would be far from the line where the equation is broken. A better solution would be to be able to put the wording as a real marginal note in front of the line where the break occurs. – Denis Bitouzé Oct 20 '17 at 19:59
• @DenisBitouzé So you're saying that if there is a long foot note then the message is in the wrong place... Not sure how to adjust for this. Before using the background package I did actually try to use the shipout commands from atbegshi to place a marginal note but for reasons not clear to me the note always appeared on the wrong page. – Andrew Oct 21 '17 at 7:12
• If I'm right, having the message at the right vertical place in the margin needs, not a global flag telling if we are or not in an align environment, but a more accurate flag telling, for each line (except the 1st one) within such an environment, if it is or not on the same page as the previous line. If not, a \marginpar (or a marginnote's \marginnote) should be to emitted just before the  \\  of this previous line. – Denis Bitouzé Oct 21 '17 at 9:51

OK, I managed, with the hope I didn't forget possible side effects. The following MWE contains comments explaining the approach (later, I'll try to give the expl3 flavor of it).

## Edit

My original code gave an error when the \\ was used in text mode outside tabular environments since, at the end of the multi-line equation environments, \\ was globally redefined as the one inside these environments, which is not the same as the default one.

\documentclass{report}
\usepackage{amsmath}
\usepackage{mwe}
\usepackage{etoolbox}
\usepackage{refcount}
\usepackage{xifthen}
\usepackage{marginnote}
\usepackage[papersize={130mm,140mm},margin=30mm]{geometry}

\allowdisplaybreaks

\newcommand{\eqcont}{\tiny(Cont.\ next page)}

% Each multiline equation will have a unique number.
\newcounter{multilineeq}
% Each line of a multiline equation will have a unique number (local
% to the multiline equation).
\newcounter{lineofmultilineeq}[multilineeq]

\makeatletter
% We globally store the original double backslash in a macro
\global\let\original@double@backslash\\
% We define the patched macro of the double backslash one
\newcommand{\patched@double@backslash}{%
% We increment the counter of the current line.
\stepcounter{lineofmultilineeq}%
% We set the label of the current line.
\ltx@label{\lineofmultilineeq@label}%
% If (and only if) the pageref of the next line if greater than the
% current line's one, we insert a marginal note telling the equation
% continues on next page.
\ifthenelse{%
\cnttest{%
\getpagerefnumber{\nextlineofmultilineeq@label}%
}{>}{%
\getpagerefnumber{\lineofmultilineeq@label}%
}%
}{%
\marginnote{\eqcont}%
}{}
% We break the line with the original double backslash macro.
\original@local@double@backslash%
}
% We define a hack to be applied at the beginning of each multiline equation
% environment (currently, only align(*)' and multline(*)').
\def\beginhack{%
% We set the unique number that has the current multiline equation.
\stepcounter{multilineeq}
% The number that has the 1st line of the current multiline equation is 1.
\stepcounter{lineofmultilineeq}
% We globally define the strings of the labels identfying:
% - the current line,
% - the line following the current one,
% of the current multiline equation.
\gdef\lineofmultilineeq@label{multilineeq-\themultilineeq-line-\thelineofmultilineeq}
\gdef\nextlineofmultilineeq@label{multilineeq-\themultilineeq-line-\the\numexpr\thelineofmultilineeq+1}
% We set the label of the current line.
\ltx@label{\lineofmultilineeq@label}
% Within the current multiline equation, we patch the double backslash macro
% (for more details, see https://tex.stackexchange.com/a/59117/18401).
\ifundef{\original@local@double@backslash}{%
\global\let\original@local@double@backslash\\%
\global\let\\\patched@double@backslash%
}{}
}
% We define a hack to be applied at the end of each multiline equation
% environment (currently, only align(*)' and multline(*)').
\def\endhack{%
% We globally unpatch the double backslash macro
\global\let\\\original@double@backslash%
\global\undef\original@local@double@backslash
}
\makeatother

\appto\align{\beginhack}
\csappto{align*}{\beginhack}
\appto\endalign{\endhack}
%
\appto\multline{\beginhack}
\csappto{multline*}{\beginhack}
\appto\endmultline{\endhack}

\begin{document}
Foo\footnote{\tiny\lipsum[1]}
\begin{multline}
x = abcdefghijklmopqrstuvwyz \\
+ abcdefghijklmopqrstuvwyz  \\[1cm]
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz  \\
+ abcdefghijklmopqrstuvwyz
\end{multline}

Foo\\Bar.

\begin{tabular}{l}
Foo\\Bar.
\end{tabular}

\begin{figure}[b]
\centering
\includegraphics[height=1cm]{example-image-a}
\caption{A nice figure}
\label{nice-figure}
\end{figure}

\begin{align}
1 + 1 & = 2 \\
2 + 1 & = 3 \\
3 + 1 & = 4 \\
4 + 1 & = 5 \\
5 + 1 & = 6 \\
6 + 1 & = 7 \\
7 + 1 & = 8 \\
8 + 1 & = 9 \\
1 + 1 & = 2 \\
2 + 1 & = 3 \\
3 + 1 & = 4 \\
4 + 1 & = 5 \\
5 + 1 & = 6 \\
6 + 1 & = 7 \\
7 + 1 & = 8 \\
8 + 1 & = 9 \\
1 + 1 & = 2 \\
2 + 1 & = 3 \\
3 + 1 & = 4 \\
4 + 1 & = 5 \\
5 + 1 & = 6 \\
6 + 1 & = 7 \\
7 + 1 & = 8 \\
8 + 1 & = 9 \\
1 + 1 & = 2 \\
2 + 1 & = 3 \\
3 + 1 & = 4 \\
4 + 1 & = 5 \\
5 + 1 & = 6 \\
6 + 1 & = 7 \\
7 + 1 & = 8 \\
8 + 1 & = 9 \\
9 + 1 & = 10
\end{align}
\end{document}