# What is \section and itemize doing to the penalty system? (problem with eledmac column balancing)

I still have a strange problem with column balancing in eledmac.sty two column footnotes. But I have a new MWE, possibly showing that this might not be a problem specific to eledmac (this is why I post it here to a wider audience).

I have this simple MWE:

\documentclass{article}
\usepackage{lipsum}

\usepackage{eledmac}
\foottwocolX{A}
\def\footnote#1{\footnoteA{#1}}

\begin{document}

% Either remove this section:

\lipsum[1]

% ... or remove the item / quote environment:
\begin{itemize}
%\begin{quote}
\item The same problem happens if this is not an itemize but a quote
environment.
%\end{quote}
\end{itemize}

Bla\footnote{Test.}
Foo\footnote{Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test }
Bar \footnote{Test.}

\end{document}

In this MWE, the two column footnotes look unbalanced like this:

But if I remove either the \section or the itemize environment (putting normal text instead so that it keeps the same vertical space), the columns will be balanced like this:

The same holds if I use a quote environment instead of the itemize.

Is it possilble that itemize and quote and \section doing anything strange to the penalty-system so the penalty calculation for the column balancing in the footnote gets "irritated" or something??

In my previous post I pointed out that it is not possible influencing this behavior properly by means of setting up the panelty variables. In another post, I extracted the code of the column balancing algorithm so you can play around with it.

Woah, the LaTeX kernel is still able after two decades to come up with surprises!

What a nasty bug and in there since day -1.

This is not a problem of eledmac it is a problem of the way LaTeX tries to keep at least 2 lines of text after a section heading. It does that by setting \clubpenalty to 10000 and then uses \everypar to restore the original setting when it starts the second paragraph.

Unfortunately the second paragraph in the MWE is an environment and so the resetting happens inside a group. Therefore at the end of the environment \clubpenalty gets its prohibiting value back. That wouldn't be too bad as \everypar is also getting its definition back to reset it.

However, after the environment group has ended LaTeX runs further code to handle so called "paragraph environment" (envs that have blank lines around them as they get extra spacing) and that mechanism (\@doendpe) also uses \everypar to restore some settings and along the way the code to restore \clubpenaltygets lost.

As a result from there on `\clubpenalty stays at 10000 keeping in all further paragraphs the first two lines together --- and that is what makes the difference in the footnotes: the second footnote doesn't have a breakpoint between its first and second line any longer).

In normal text or indeed in most LaTeX document that goes unnoticed as LaTeX usually finds a nearby page break that is acceptable (and club lines aren't so great anyway :-) ) but here is clearly shows.

\documentclass{article}

\def\X{\showthe\clubpenalty\showthe\everypar}

\begin{document}

\X

a little bit of text

\X

% Uncomment this para then the issue goes away

%Another paragraph that makes \verb=\clubpenalty= okay again.

\X

% ... or remove the item / quote environment:
\begin{itemize}
\item \X
The same problem happens if this is not an itemize but a quote
environment.

\end{itemize}
\X

\par

\X  Here is the issue: \verb=\clubpenalty= is restored back to 10000 but the \verb=\@doendpe= kills the also restored \verb=\everypar= so that it is never set back to normal.

Bla\footnote{Test.}
Foo\footnote{\X Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test }
Bar \footnote{Test.}

\X

Another paragraph

\X

\end{document}

If you run the above version you can see how the drama enfolds.

A possible fix might be the following (but I say might as this really is an extremely delicate area and I'm not sure I have thought about all implications ... so take this as an untested idea -- it basically sets \clubpenalty unconditionally back to its saved value when the end of a par env is reached, might well be the case that this needs to be done conditionally:

\makeatletter
\def\@doendpe{\@endpetrue
\def\par{\@restorepar
\clubpenalty \@clubpenalty
\everypar{}\par\@endpefalse}%
\everypar{{\setbox\z@\lastbox}\everypar{}\clubpenalty \@clubpenalty
\@endpefalse}}
\makeatother
• May 2, 2015 at 9:34
• @DavidCarlisle hmm, if you know about that one why haven't we looked at this from the kernel perspective? May 2, 2015 at 9:37
• I read your answer and put \doendpe \clubpenalty into google to see if it hat hit anyone before.... I think I must have just missed mbork's bug report (as we all did it seems:( May 2, 2015 at 9:40
• as eledmac maintener, I am happy to know that is not eledmac fault ;-) May 2, 2015 at 20:54
• Thanks a lot for this great analysis. It seems the patch works in my situation. I'm still testing it on a big project. Also the other thread linked by David is very helpful. May 3, 2015 at 9:20