2

If I make longtable, how can I remove the spacing between preceding text and it? Tabular doesn't have this spacing.

This example should make clear to you what I'm asking:

\documentclass[a4paper]{article}
\usepackage[margin = 5mm]{geometry}
\usepackage{longtable}
\begin{document}
\centering
Can you see how little vertical space there is between this line and the table down here \ldots\\
\begin{tabular}{|c|c|c|c|}
        \hline
        0 & 1 & 2 & 3 \tabularnewline
        \hline
        4 & 5 & 6 & 7 \tabularnewline
        \hline
        8 & 9 & 0 & 1 \tabularnewline
        \hline
        2 & 3 & 4 & 5 \tabularnewline
        \hline
        6 & 7 & 8 & 9 \tabularnewline
        \hline
\end{tabular}\\
\vspace{3cm}
\ldots compared to this line and this table?\\
\begin{longtable}{|c|c|c|c|}
        \hline
        0 & 1 & 2 & 3 \tabularnewline
        \hline
        4 & 5 & 6 & 7 \tabularnewline
        \hline
        8 & 9 & 0 & 1 \tabularnewline
        \hline
        2 & 3 & 4 & 5 \tabularnewline
        \hline
        6 & 7 & 8 & 9 \tabularnewline
        \hline
\end{longtable}
\end{document}
Hanlon
  • 265
  • 2
  • 12

1 Answers1

0

tabular is an inline construct, like a big letter, so it does not have any spacing associated with it at all. longtable is a display construct like a displayed equation or a quote environment so always takes the full width of the page and is offset with vertical space. The space before is \LTpre which you can set to 0.

Avoid mis-using \\ at the end of a paragraph 9such as before longtable the bad effects of doing that are hidden here by a quirk of the \centering implementation, but normally it would produce an underfull box of badness 10000

enter image description here

\documentclass[a4paper]{article}
\usepackage[margin = 5mm]{geometry}
\usepackage{longtable}
\setlength\LTpre{0pt}

\begin{document}
\centering
Can you see how little vertical space there is between this line and the table down here \ldots

\begin{tabular}{|c|c|c|c|}
        \hline
        0 & 1 & 2 & 3 \tabularnewline
        \hline
        4 & 5 & 6 & 7 \tabularnewline
        \hline
        8 & 9 & 0 & 1 \tabularnewline
        \hline
        2 & 3 & 4 & 5 \tabularnewline
        \hline
        6 & 7 & 8 & 9 \tabularnewline
        \hline
\end{tabular}

\vspace{3cm}
\ldots compared to this line and this table?

\begin{longtable}{|c|c|c|c|}
        \hline
        0 & 1 & 2 & 3 \tabularnewline
        \hline
        4 & 5 & 6 & 7 \tabularnewline
        \hline
        8 & 9 & 0 & 1 \tabularnewline
        \hline
        2 & 3 & 4 & 5 \tabularnewline
        \hline
        6 & 7 & 8 & 9 \tabularnewline
        \hline
\end{longtable}
\end{document}
David Carlisle
  • 757,742
  • Thanks. Having no new line before longtable looks ok. I assumed that new line is required for longtable since it's required by tabular. – Hanlon Mar 31 '18 at 15:02
  • But you use <CR> instead of \. Aren't those two the same thing? – Hanlon Mar 31 '18 at 15:05
  • @Vuk no they are completely different! (there is a question/answer about that) I'll add a link – David Carlisle Mar 31 '18 at 15:09
  • @Vuk https://tex.stackexchange.com/a/82666/1090 – David Carlisle Mar 31 '18 at 15:10
  • @Vuk compare with inline math with $x$ and display math with \[x\] the distinction between tabular and longtable is similar, tabular is an inline construct, lontable is a display one; you can not have a longtable in the same line as the preceding text, just as you can not have display math. – David Carlisle Mar 31 '18 at 15:12