1

According to this it is possible to put at least one element between \endhead and \endlastfoot on automatic pagebreak by putting the last element into the \endlastfoot. But if I also want to have the actual endlastfoot at the buttom of the page, the *[\fill] does not work like it would between the last element and the endlastfoot.

\documentclass[10pt, a4paper]{article}
\usepackage{longtable}
\pagestyle{empty}

\begin{document}
\vspace*{17.4cm}

\begin{longtable}{@{}lc@{}}
Date &       Grade
\endhead
%
2016-01-15 & C\\ *[\fill]
\bfseries Summary:
\endlastfoot
%
2016-01-01 & A\\
2016-01-15 & B\\
2016-01-01 & A\\
2016-01-15 & B\\
2016-01-01 & A\\

\end{longtable}
\end{document}

2 Answers 2

1

Interestingly, I couldn't even use \rule in the lastfoot to add extra space. So I split it into two tabulars.

\documentclass[10pt, a4paper]{article}
\usepackage{longtable}
\pagestyle{empty}
\usepackage{showframe}

\begin{document}
\vspace*{17.4cm}

\begin{longtable}{@{}lc@{}}
Date &       Grade
\endhead
2016-01-15 & C \\
\phantom{\bfseries Summary:}
\endlastfoot
2016-01-01 & A\\
2016-01-15 & B\\
2016-01-01 & A\\
2016-01-15 & B\\
2016-01-01 & A\\
\end{longtable}
\vfill
\noindent\hfil
\begin{tabular}[b]{@{}lc@{}}
\phantom{2016-01-15} & \phantom{Grade}\\
\bfseries Summary:
\end{tabular}
\end{document}

It is possible to get all the column widths from the aux file instead of using \phantom. See here. By using two longtables one can share widths. The downside is that to shrink the widths one has to delete the old aux file.

\documentclass[10pt, a4paper]{article}
\usepackage{longtable}
\pagestyle{empty}
\usepackage{showframe}

\newlength{\LTwidth}
\newcommand{\LTwidthA}{0pt}
\newcommand{\LTwidthB}{0pt}

\makeatletter
\newcommand*{\getLTwidth}[1][0]% #1 = offet (optional): 0 = previous, 1 = next
{\bgroup% for \count1 and \LT@entry
  \count1=#1\relax
  \advance\count1 by \value{LT@tables}%
  \@ifundefined{LT@\@roman{\count1}}{\LTwidth=\textwidth}{%
    \dimen0=0pt%
    \count2=0
    \def\LT@entry##1##2{\advance\dimen0 by ##2\relax
      \advance\count2 by 1
      \expandafter\xdef\csname LTwidth\@Alph{\count2}\endcsname{##2}}
    \csname LT@\@roman{\count1}\endcsname
  }%
  \global\LTwidth=\dimen0
\egroup}
\makeatother

\newcommand{\maxcell}[3]% #1 = width, #2 = l/r/c, #3 = text
{\bgroup
  \dimen0=#1\relax
  \sbox0{#3}%
  \ifdim\wd0>\dimen0
    \usebox0
  \else
    \makebox[\dimen0][#2]{\usebox0}%
  \fi
\egroup}

\begin{document}
\vspace*{17.8cm}
\getLTwidth[2]% get widths from second table
\begin{longtable}{@{}lc@{}}
Date &       Grade
\endhead
2016-01-15 & C
\endlastfoot
\maxcell{\dimexpr \LTwidthA-\tabcolsep}{l}{2016-01-01} &
\maxcell{\dimexpr \LTwidthB-\tabcolsep}{c}{A} \\% any row will do
2016-01-15 & B\\
2016-01-01 & A\\
2016-01-15 & B\\
2016-01-01 & A\\
\end{longtable}
\getLTwidth% get widths from previous table
\vfill
\noindent\hfil
\begin{longtable}{@{}lc@{}}
  %\rule{\LTwidthA}{1pt}\hspace{-\tabcolsep} & \hspace{-\tabcolsep}\rule[1pt]{\LTwidthB}{1pt} \\
  \maxcell{\dimexpr \LTwidthA-\tabcolsep}{l}{\bfseries Summary:} &
  \maxcell{\dimexpr \LTwidthB-\tabcolsep}{c}{ }
\end{longtable}\unskip% remove gap at bottom
\end{document}
4
  • Is the same hack possible with xltabular? I try to get a \hline above the summary, but it does not have the same width as my xltabular width.
    – Namal
    Feb 16, 2022 at 8:41
  • 1
    I just read that xltabular uses longtable, so it should work. Note that it may take several runs to settle down. Also, if you add more stuff to the second tabular it may get wider than the longtable. Feb 16, 2022 at 15:54
  • I need to replace \begin{tabular} with \begin{tabularx}{\textwidth}{@{}lc@{}} in your example to get \hline to the same length. Then it works!
    – Namal
    Feb 17, 2022 at 6:54
  • I revised the second solution to use two longtables and use the widths from the second longtable to initialize the first. \maxcell is used in case the text inside turns out to be the widest entry (as is the case for Summary:). Feb 17, 2022 at 21:13
1

The longtable syntax here matches the syntax of tabular (in fact it is just using the under lying tabular implementation) \\[...] (normally) adds a strut (zero width rule) with the specified depth to force the row to have greater depth. A rule dimension is always a fixed length and will take the natura length if a stretchy length is supplied, which is 0pt here. In some cases \\[...] adds a vertical skip rather than a rule in which case a stretch space might do something, but thelongtable foot is a box that is inserted so always has fixed lengths,

You must log in to answer this question.

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