5

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}

Mathematica graphics

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}

Mathematica graphics

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

5
  • 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
  • 1
    @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
  • 1
    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
5

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
\endhead
\blindtext
&
\blindtext
\\\hline
\end{longtable}
\end{document}
1
  • 2
    +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
3

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}
3
  • (+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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.