# how to get rid of extra \hline drawn before start of longtable?

Whenever I have a long table

\begin{longtable}...\hline
....
\\\hline
\end{longtable}


That happens to start at top of a page, I see an \hline drawn at the end of the previous page. However, if the longtable happens to start in the middle of a page, there is no extra \hline. I am having hard time telling Latex to please stop doing this since it is very annoying.

Here is a MWE showing \hline added before table starts at new page

\documentclass[11pt]{article}%ext
\usepackage{blindtext}
\usepackage{longtable}
\begin{document}
\blindtext
\blindtext

\begin{longtable}[t]{|p{0.5\textwidth} | p{0.5\textwidth}|}\hline
\blindtext
&
\blindtext
\\\hline
\end{longtable}
\end{document}


If the table starts in the middle of the page (depending on how much text was before it), there is no extra line:

\documentclass[11pt]{article}%ext
\usepackage{blindtext}
\usepackage{longtable}
\begin{document}
\blindtext
\begin{longtable}[t]{|p{0.5\textwidth} | p{0.5\textwidth}|}\hline
\blindtext
&
\blindtext
\\\hline
\end{longtable}
\end{document}


Question: What is the correct way to write longtable to prevent the extra line for all different cases?

• Well, not writing \hline at the start of the table would be the most reliable way to not get an \hline (see how it is on all the lines that have \begin{longtable} ... \hline?). But, second, ditch the \hlines entirely, and load booktabs and use those lines instead. – jon May 25 '16 at 4:55
• @jon but I want a line at top of the table? I just do not want 2 of them. – Nasser May 25 '16 at 4:56
• Well, you could use the needspace package and do something like \needspace{5\baselineskip} before the start of the table. – jon May 25 '16 at 4:59
• if in doubt you could always read the longtable manual which suggests that if you want to stop a page break before a row or rows of the table then to put those rows into firsthead. (see Christian's answer) – David Carlisle May 25 '16 at 8:01
• See tex.stackexchange.com/a/6353/2388 for code for a \nobreakhline. – Ulrike Fischer May 25 '16 at 8:26

This is only a 'brute force' solution: Use \endhead after the first \hline. This 'guarantees' that there's always a \hline on table pages other than the first one.

The first one is 'empty' because longtable can't break the \blindtext paragraph so it is shifted to the next page.

\documentclass[11pt]{article}%ext
\usepackage{blindtext}
\usepackage{longtable}
\begin{document}
\blindtext
\blindtext

\begin{longtable}[t]{|p{0.5\textwidth} | p{0.5\textwidth}|}
\hline
\blindtext
&
\blindtext
\\\hline
\end{longtable}
\end{document}

• +1 as it says in the manual you can use \endfirsthead to prevent a page break before any number of initial rows. – David Carlisle May 25 '16 at 8:02

I think I'd be inclined to use Christian's answer (+1), but, just to provide other ideas, you could use the needspace package. E.g.,

\documentclass[11pt]{article}%ext
\usepackage{blindtext}
\usepackage{longtable,needspace,etoolbox}
\AtBeginEnvironment{longtable}{\needspace{5\baselineskip}}
\begin{document}
\blindtext
\blindtext

%\needspace{5\baselineskip}
\begin{longtable}[t]{|p{0.5\textwidth} | p{0.5\textwidth}|}\hline
\blindtext
&
\blindtext
\\\hline
\end{longtable}
\end{document}

• (+1), but it would use the needspace for any longtable then, I assume? – user31729 May 25 '16 at 5:09
• @ChristianHupfer -- Indeed. That's why I commented out the \needspace line: in case you only want to do it once/manually, you could use that and not use the etoolbox method. I would want to do some testing, I suppose, before I blindly relied on the etoolbox` method..! – jon May 25 '16 at 5:11
• I see -- well, I can't upvote again ;-) – user31729 May 25 '16 at 5:23