1

I am part of a team working collaboratively to prepare a large document containing tabular and multimedia content using LaTeX (publishing to PDF using lualatex), and we've hit a persistent problem with one page in our document having text content spill over the end of the page as follows:

Example

We believe we've tracked down why, and the following is the MWE used to produce the above:

\documentclass[a4paper]{report}
\usepackage[margin=2.54cm]{geometry}
\usepackage{lipsum}

%\usepackage{showframe}
\usepackage{longtable}
\usepackage[draft]{graphicx} 

%\graphicspath{ {Resources/} }

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\section{Section here}

\subsection{Subsection here}

\lipsum[1-2]

\begin{figure}
\caption{Caption 1}
\includegraphics[height=9.24cm]{image1.png}
\end{figure}

\subsection{Subsection here}

\lipsum[1]

\begin{figure}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consequat ex vitae}
\includegraphics[height=9.60cm]{image2.png}
\end{figure}

\lipsum[1-3]

\begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\cline{1-6}
 & Text & Text & Text & Text & Text\\ 
\cline{1-6}
\end{longtable}

\lipsum[1-6]

\end{document}

The issue we're hitting appears to be the bug documented at https://www.latex-project.org/cgi-bin/ltxbugs2html?pr=tools/3512, caused by the longtable package incorrectly resetting the pagegoal erroneously when it follows a float.

There's a suggested fix on the issue, and this has been discussed before at Pagebreak doesn't work with floats and longtabu, but in both cases the fix now predates the current version of Longtable, and we would ideally like to avoid trying to maintain a patched version of Longtable across our whole team.

Is anyone aware of a document-local fix to this that we could use as an alternative?

7

1 Answer 1

0

Sorry, I try to keep the bug report turnaround to under 20 years, but it's not always easy....

You can make the changes suggested in the report that you reference in-document rather than modifying the .sty file.

\documentclass[a4paper]{report}
\usepackage[margin=2.54cm]{geometry}
\usepackage{lipsum}

%\usepackage{showframe}
\usepackage{longtable}

\makeatletter
\def\LT@start{%
  \let\LT@start\endgraf
  \endgraf\penalty\z@\vskip\LTpre\endgraf
  \dimen@\pagetotal
  \advance\dimen@ \ht\ifvoid\LT@firsthead\LT@head\else\LT@firsthead\fi
  \advance\dimen@ \dp\ifvoid\LT@firsthead\LT@head\else\LT@firsthead\fi
  \advance\dimen@ \ht\LT@foot
  \dimen@ii\vfuzz
  \vfuzz\maxdimen
    \setbox\tw@\copy\z@
    \setbox\tw@\vsplit\tw@ to \ht\@arstrutbox
    \setbox\tw@\vbox{\unvbox\tw@}%
  \vfuzz\dimen@ii
  \advance\dimen@ \ht
        \ifdim\ht\@arstrutbox>\ht\tw@\@arstrutbox\else\tw@\fi
  \advance\dimen@\dp
        \ifdim\dp\@arstrutbox>\dp\tw@\@arstrutbox\else\tw@\fi
  \advance\dimen@ -\pagegoal
  \ifdim \dimen@>\z@\vfil\break\fi
      \global\@colroom\@colht
  \ifvoid\LT@foot\else
%    \advance\vsize-\ht\LT@foot
     \global\advance\vsize-\ht\LT@foot
    \global\advance\@colroom-\ht\LT@foot
    \dimen@\pagegoal\advance\dimen@-\ht\LT@foot\pagegoal\dimen@
    \maxdepth\z@
  \fi
  \ifvoid\LT@firsthead\copy\LT@head\else\box\LT@firsthead\fi\nobreak
  \output{\LT@output}}

\def\endlongtable{%
  \crcr
  \noalign{%
    \let\LT@entry\LT@entry@chop
    \xdef\LT@save@row{\LT@save@row}}%
  \LT@echunk
  \LT@start
  \unvbox\z@
  \LT@get@widths
  \if@filesw
    {\let\LT@entry\LT@entry@write\immediate\write\@auxout{%
      \gdef\expandafter\noexpand
        \csname LT@\romannumeral\c@LT@tables\endcsname
          {\LT@save@row}}}%
  \fi
  \ifx\LT@save@row\LT@@save@row
  \else
    \LT@warn{Column \@width s have changed\MessageBreak
             in table \thetable}%
    \LT@final@warn
  \fi
  \endgraf\penalty -\LT@end@pen
%
  \ifvoid\LT@foot\else
    \global\advance\vsize\ht\LT@foot
    \global\advance\@colroom\ht\LT@foot
    \dimen@\pagegoal\advance\dimen@\ht\LT@foot\pagegoal\dimen@
  \fi
%
  \endgroup
  \global\@mparbottom\z@
%  \pagegoal\vsize
  \endgraf\penalty\z@\addvspace\LTpost
  \ifvoid\footins\else\insert\footins{}\fi}

\def\LT@output{%
  \ifnum\outputpenalty <-\@Mi
    \ifnum\outputpenalty > -\LT@end@pen
      \LT@err{floats and marginpars not allowed in a longtable}\@ehc
    \else
      \setbox\z@\vbox{\unvbox\@cclv}%
      \ifdim \ht\LT@lastfoot>\ht\LT@foot
        \dimen@\pagegoal
%
\advance\dimen@\ht\LT@foot
%
        \advance\dimen@-\ht\LT@lastfoot
        \ifdim\dimen@<\ht\z@
          \setbox\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
          \@makecol
          \@outputpage
%
            \global\vsize\@colroom
%
          \setbox\z@\vbox{\box\LT@head}%
        \fi
      \fi
%      \global\@colroom\@colht
%      \global\vsize\@colht
%      \vbox
%        {\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
      \unvbox\z@\ifvoid\LT@lastfoot\copy\LT@foot\else\box\LT@lastfoot\fi
    \fi
  \else
    \setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
    \@makecol
    \@outputpage
      \global\vsize\@colroom
    \copy\LT@head\nobreak
  \fi}

\makeatother

\usepackage[draft]{graphicx} 

%\graphicspath{ {Resources/} }

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\section{Section here}

\subsection{Subsection here}

\lipsum[1-2]

\begin{figure}
\caption{Caption 1}
\includegraphics[height=9.24cm]{image1.png}
\end{figure}

\subsection{Subsection here}

\lipsum[1]

\begin{figure}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consequat ex vitae}
\includegraphics[height=9.60cm]{image2.png}
\end{figure}

\lipsum[1-3]

\begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\cline{1-6}
 & Text & Text & Text & Text & Text\\ 
\cline{1-6}
\end{longtable}

\lipsum[1-6]

\end{document}
3
  • Thank you for the response, and I can see from the comments above that a fix in the package itself is on the way, as testing with the updated version solves the issue. Commented Aug 13, 2020 at 9:41
  • @JamesGrover yes but that statement has been true since 2001, although the 4.14 branch Ulrike mentioned is a real attempt this year to try to get something out. Fixing reported bugs is the easy part it is not breaking the 30 years of existing documents that is harder... Commented Aug 13, 2020 at 10:05
  • It doesn't work for me and the text still floats over to bottom of the page when a figure and longtable is on the same page.
    – mhellmeier
    Commented Mar 21, 2021 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .