3

Consider the following code

\documentclass[a4paper]{article}
\usepackage{longtable}
\usepackage{lipsum}

\begin{document}
    \begin{longtable}{|p{5cm}|}
        \hline
        \lipsum[5] \\\hline
        \lipsum[7] \\\hline
        \lipsum[1] \\\hline
        \lipsum[1-2] \\\hline
        \lipsum[5]\\\hline %no stretching here
    \end{longtable}
    some text below the table
\end{document}

which will lead to the result (minimal zoom level).

screenshot

As you can see the space between the lower border of the column and the lower page border differs for each page (especially on the 2nd one), which doesn't look good for me.

What I want is that each time a page break occurs the height of the last row is extended (stretched), so the distance from the lower line of the table to the bottom of the page is equal on each page. This should not occur for the last row of the table, because it will be embedded in other content. In the MWE I provided the last page therefore should just look as it does right now without any modification.

Do you have any idea how this can be achieved?

[Edit]

A fake screenshot of how the example would look like

enter image description here

9
  • A single cell will never be broken across pages.
    – egreg
    Commented Nov 4, 2013 at 10:07
  • 1
    It would be hard to do really. longtable uses a standard tex page breaking output routine which means that all the page content is set before any decisions about page breaking are made. Commented Nov 4, 2013 at 10:08
  • @egreg I don't want to break single cells. The page breaking behaviour should stay the same, all I want is that after each page break: (1) the distance from the last position / the lower table limit on the last page to the lower page border is calculated and (2) the height of the last row is expanded by the distance from (1)
    – muffel
    Commented Nov 4, 2013 at 10:11
  • @DavidCarlisle I don't know how this routines work internally, but would it be possible to output the table at first, calculate the distances and then recreate the table by using the calculated distances?
    – muffel
    Commented Nov 4, 2013 at 10:13
  • 1
    anything is possible, but it would require rewriting the package, not just a couple of lines of code in an answer here. Commented Nov 4, 2013 at 10:15

1 Answer 1

2

semi-automatic method, using the \pdfsavepos extension to work out where the text ends then constructing a suitable length to use with \\[something] to extend the row. I have not here tried to automatically determine the rows where the page breaks, so at the end of edits just change \\ to \zz{AomeUniqueLabel} You can see the lengths it is making in the aux file and adjust the -150pt to adjust exactly where the table ends. if you edit the document, delete the aux file or it will re-use spurious values. It takes a couple of runs always to use \pdfavepos values.

enter image description here

\documentclass[a4paper]{article}
\usepackage{longtable}
\usepackage{lipsum}

\makeatletter
\def\zz#1{%
\pdfsavepos\write\@auxout{%
\gdef\expandafter\string\csname zz@#1\endcsname{%
 \string\\[\the\dimexpr\the\pdflastypos sp -150pt\relax]}}%
\expandafter\ifx\csname zz@#1\endcsname\relax
\expandafter\\%
\else
\csname zz@#1\expandafter\endcsname
\fi}
\makeatother

\begin{document}
    \begin{longtable}{|p{5cm}|}
        \hline
        \lipsum[5] \\\hline
        \lipsum[7] \zz{a}\hline
        \lipsum[1] \zz{b}\hline
        \lipsum[1-2] \zz{c}\hline
        \lipsum[5]\\\hline %no stretching here
    \end{longtable}
    some text below the table
\end{document}

You must log in to answer this question.

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