# marginpar and orphans

How can I avoid orphan lines if I use \marginpar?

For example, here is a two-page document that does not have any orphans:

\documentclass[letterpaper]{article}
\usepackage{lipsum}
\clubpenalty=10000
\widowpenalty=10000
\begin{document}
\lipsum[1-5]
Foo. \lipsum[6-7]
\end{document}


However, if I add a marginpar, the layout is changed and there is an orphan line:

\documentclass[letterpaper]{article}
\usepackage{lipsum}
\clubpenalty=10000
\widowpenalty=10000
\begin{document}
\lipsum[1-5]
Foo.\marginpar{Test} \lipsum[6-7]
\end{document}


(It seems that \marginnote from the marginnote package does not have the same problem.)

-
There is discussion of this on CTT at http://newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2008-05/msg00031.htm‌​l. – mas Sep 17 '11 at 19:22

I fear the answer is that with 2e's \marginpar this is the expected behavior. The reason for this is that LaTeX2e implements marginal notes by calling the output routine after the current line. Then in the OR it will look at the current status of the page and attach the marginal accordingly, i.e., on the left or right. It then returns the constructed material back to the main galley and let TeX carry on typesetting.

Now why may this result in orphans? To trigger the output routine LaTex has to place a special penalty in that signals a marginal. Now, whenever the page builder is excerised the penalty item that triggers it stays on the top of the "recent contributions" to be looked at after the OR finishes, but this penlty item is also automatically being changed to 10000 (i.e., will never again be a break point).

As a result a place which would normally be a valid break point (e.g., the end of a normal line without a special penalty) might become a non break point since now we suddenly have \penalty 10000in this place. Now that in turn could mean that LaTeX is forced to break the page at an earlier place and thus moves the line with the marginpar (or the float which uses the same mechanism) to the next one. Now all of the LaTeX OR algorithms assume that this never happens, that is, if a float or marginpar is seen on one page then this also means it will appear on this one. Therefore, the OR explicitly adds a penalty back in to ensure that there is a valid break point. Unfortunately in the special case you have this is not what you want.

So other than manual correction there is not much you can do here. What can be useful in this place is to either run the page long or short with \enlargethispage, or set an explicit pagebreak before the line with the marginal, or to try and apply the 2.09 command/environment \samepage, like this:

\documentclass[letterpaper,twoside]{article}
\usepackage{lipsum}
\clubpenalty=10000
\widowpenalty=10000

\begin{document}
\lipsum[1-5]
\begin{samepage}
Foo.\marginpar{Test} \lipsum[6-7]
\end{samepage}

Bar.\marginpar{Test} \lipsum[6-7]
\end{document}


But what goes wrong in that case can be nicely seen (which is why LaTex doesn't do this): the marginal comes out on the wrong side if the twoside option is added as I did for illustrations.

-