# Vertical space between longtable and top of the page

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.

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.

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}

\begin{document}

\lipsum[2]

\begin{longtable}{|p{0.4\textwidth}|p{0.4\textwidth}|}
\hline
\hline
\lipsum[2] & \lipsum[2] \\
\hline
\lipsum[2] & \lipsum[2] \\
\hline
\end{longtable}

\end{document}

• Jan 31 '18 at 4:07
• @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 '18 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 '18 at 7:47
• You're right, @David. Space before table is correct without \hline. But \endfirsthead without \endhead doesn't solve problem. Jan 31 '18 at 9:31
• @John, in my case the simpliest way is to put \topskip=1.6pt. But it is too simple. ;-) Jan 31 '18 at 9:34

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}
\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}


• It works only for the second page of a table, but doesn't for the third. Jan 31 '18 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 '18 at 10:07

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

%
%
\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
}
\def\LT@output{%