0

With the environment longtable we make a table crossing the page boundary. If a table has a head (\endhead command), longtable works perfectly: the baseline of the first line in a usual text page and the baseline of the first line of text in the table are coinside.

longtable with head

But, if a table doesn't have a head, the baseline of the first line of text in the table is lower then the baseline of the first line in a usual text page.

longtable without head

How to reduce the space between top of a page and a table without head?

Is it possible to slightly modify the longtable package so that all tables in a document would be fixed?

\documentclass{article}

\usepackage[a5paper]{geometry}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{longtable}

\pagestyle{fancy}
\rhead{\thepage}
\lhead{\thepage}

\begin{document}

\lipsum[2]

\begin{longtable}{|p{0.4\textwidth}|p{0.4\textwidth}|}
 \hline
  Header & Header \\
 %\endhead %% Uncomment this line for table with head
 \hline
  \lipsum[2] & \lipsum[2] \\
 \hline
  \lipsum[2] & \lipsum[2] \\
 \hline
\end{longtable}

\end{document}
5
  • 1
  • @John, thank you for pointing that. Вut setting \topskip to 0pt doesn't solve the problem. Actually, we should reduce \topskip by some amount of space. In my case, this amount is height of vertical rule, which presents part of left border of the first cell. It depends on the content of the first lines of top table cells. In my case it is always 8.4pt, because font doesn't change and lines contain only letters, and start with capital letters. This \topskip later will be reduced by 0.4pt, the thickness of top border. I think, it can be somehow implemented in longtable.sty. Jan 31, 2018 at 5:18
  • it's not really that it doesn't have a head, more that the first thing is an \hline and so that aligns on the first baseline. Probably the easiest thing is to always have a head, if you put the hline and the first row in \endfirsthead then it should work I think. You don't need to have a head on the later pages. Jan 31, 2018 at 7:47
  • You're right, @David. Space before table is correct without \hline. But \endfirsthead without \endhead doesn't solve problem. Jan 31, 2018 at 9:31
  • @John, in my case the simpliest way is to put \topskip=1.6pt. But it is too simple. ;-) Jan 31, 2018 at 9:34

2 Answers 2

1

You can use \firsthline from the array package instead of \hline:

\documentclass{book}%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array,longtable}
\usepackage{geometry,tikz}
\usepackage{eso-pic}
\geometry{top=2cm}
\AddToShipoutPictureFG{%
 \tikz[overlay,remember picture] \draw[red] ([yshift=-2cm-\topskip]current page.north west) -- ++(\textwidth,0);}

\begin{document}
some text
\newpage 
\begin{longtable}{l}
\hline
abc
\end{longtable}
\newpage
\begin{longtable}{l}
\firsthline
abc
\end{longtable}

\end{document}

enter image description here enter image description here enter image description here

2
  • It works only for the second page of a table, but doesn't for the third. Jan 31, 2018 at 9:13
  • If this means that you want to repeat the header line on following pages than setting topskip locally is probably your best option beside patching longtable. An alternative is to check for the first line on a page and draw the header line with tikz instead of using \endhead (see tex.stackexchange.com/a/384925/2388 for a first line check). Jan 31, 2018 at 10:07
0

The next patch to longtable works for me, assuming that if \hline is present in table, it is present after all \\.

%
% Adjustment of longtable environment
%
\makeatletter
\newskip\ax@LT@topskip
\newif\ifax@LT@hline
% Save original definitions
\let\ax@LT@longtable=\longtable
\let\ax@LT@endlongtable=\endlongtable
\let\ax@LT@output=\LT@output
\let\ax@LT@hline=\LT@hline
% Remember, there was \hline
\def\LT@hline{%
 \noalign{\global\ax@LT@hlinetrue}%
 \ax@LT@hline
}
% Forget about \hline, store initial value of \topskip
\def\longtable{%
 \global\ax@LT@hlinefalse
 \ax@LT@topskip=\topskip
 \ax@LT@longtable
}
% Adjust \topskip, if there was \hline and wasn't \endhead
\def\LT@output{%
 \ifax@LT@hline\ifvoid\LT@head
  \global\topskip=\ax@LT@topskip
  \global\advance\topskip by -\ht\@arstrutbox
 \fi\fi
 \ax@LT@output
}
% Restore \topskip
\def\endlongtable{%
 \ax@LT@endlongtable
 \global\topskip=\ax@LT@topskip
}
\makeatother

You must log in to answer this question.

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