PhelypeOleinik / lipsum

150 paragraphs of Lorem ipsum dummy text for LaTeX
12 stars 7 forks source link

problem with wrapfig as one paragraph text is not correctly terminated #1

Closed u-fischer closed 3 years ago

u-fischer commented 3 years ago

moved from https://github.com/patta42/lipsum/issues/16

lipsum add a (imho unnecessary) group around the whole processing. And if only one paragraph is typeset it doesn't add a par before ending the group. That is a problem for wrapfig as it looses its \everypar settings:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\textwidth=5cm
\begin{document}

\begin{wrapfigure}{o}{2cm}
XXX\\XXX\\XXX
\end{wrapfigure}
\lipsum[43]

\bigskip

%wrong:
\begin{wrapfigure}{o}{2cm}
XXX\\XXX\\XXX
\end{wrapfigure}
{abc abc abc abc abc abc abc abc abc abc
 abc abc abc abc abc abc abc abc abc abc}

\bigskip
% better (but not group at all would be best):
\begin{wrapfigure}{o}{2cm}
XXX\\XXX\\XXX
\end{wrapfigure}
{abc abc abc abc abc abc abc abc abc abc
 abc abc abc abc abc abc abc abc abc abc
 \par}

\end{document}

image

frougon commented 3 years ago

Hi,

I also found out some time ago that this grouping done by \lipsum prevents the \clubpenalty\@M in \@afterheading from doing its job:

\documentclass{article}
\usepackage[papersize={12cm,2.2cm}]{geometry} % try 2.2cm -> 2.21cm with the
\usepackage{lipsum}                           % “abcd abcd...” text.

\begin{document}

\section{Foo}

% abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd.
\lipsum[1][1-2]

\end{document}

With the \lipsum command, there is a page break after the first line of the paragraph following the \section:

with lipsum

PhelypeOleinik commented 3 years ago

@frougon I removed the grouping around \lipsum so that shouldn't be a problem anymore. I tried it with the new version and it produces:

Screenshot from 2021-07-14 10-22-05

I've just pushed my local version, if you'd like to try it.


Interestingly, with the current version (v2.3), this:

\documentclass{article}
\usepackage[papersize={12cm,2.2cm}]{geometry}
\usepackage{lipsum}
\begin{document}
\section{Foo}
\lipsum[1][1-2]

\clearpage

\section{Foo}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\end{document}

produces the wrong behaviour you reported:

Screenshot from 2021-07-14 10-25-49

but swapping their order:

\documentclass{article}
\usepackage[papersize={12cm,2.2cm}]{geometry}
\usepackage{lipsum}
\begin{document}
\section{Foo}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.

\clearpage

\section{Foo}
\lipsum[1][1-2]
\end{document}

gives the correct output:

Screenshot from 2021-07-14 10-26-21

frougon commented 3 years ago

@PhelypeOleinik

Thanks for the fix! I haven't tried it yet, but surely will one of these days.

Your last example is interesting. What happens is that after the \clearpage, one still has \clubpenalty set to 10000 as well as the \everypar setting from \@afterheading that is supposed to reset \clubpenalty to the normal value \@clubpenalty as soon as the next paragraph starts. This next paragraph is started by the second \section{Foo}, precisely by this code from \@sect:

\begingroup
  #6{%
    \@hangfrom{\hskip #3\relax\@svsec}%
      \interlinepenalty \@M #8\@@par}%
\endgroup

The paragraph starts when \@hangfrom does \noindent. At this point, \clubpenalty is restored to \@clubpenalty and \everypar is made empty, but since these are local assignments, as soon as TeX executes the end-group character } that follows \@@par, \clubpenalty is “restored” to 10000 and \everypar to the value set by \@afterheading. Here is the corresponding trace (the \box command is from the expansion of \@hangfrom):

{\box}
{\interlinepenalty}
{the letter F}
{\par}
{vertical mode: end-group character }}
{restoring \interlinepenalty=0}
{restoring \everypar=\if@nobreak \@nobreakfalse \clubpenalty \ETC.}
{restoring \clubpenalty=10000}
{restoring \hangindent=0.0pt}
{restoring \box30=void}
{\endgroup}

The \endgroup doesn't change \clubpenalty nor \everypar since they haven't been locally assigned between the \begingroup and the explicit begin-group character { after #6 (#6 is \normalfont \Large \bfseries here). Then we proceed with \sectionmark, \addcontentsline and \@xsect, which in this case does:

\par \nobreak
\vskip \@tempskipa
\@afterheading
(...) % nothing (skipped alternative)
\ignorespaces

At this point, \lipsum[1][1-2] is read and processed, with \clubpenalty still equal to 10000 from the first \section.

In short, page breaks are prevented in this example after the first line of the paragraph following the second section title not because of the \@afterheading done by the second \section, but because the normal resetting of \clubpenalty to \@clubpenalty after the first \section couldn't happen before the second \section, and thus \clubpenalty=\@M was still in effect when \lipsum began its group.

Edit: I've rephrased the last paragraph.