245

I have a table that takes up a full page, but I want it to start on a page that already has some text and then continue onto the next page (i.e. it's ok if the table gets broken up and spans two pages). It is a simple two column table:

\begin{table}[h]
\centering
\begin{tabular}{| p{.20\textwidth} | p{.80\textwidth} |} 
\hline
foo & bar \\ \hline 
foo & bar \\ \hline 
...
\end{tabular}
\end{table}

I guess I need longtable? How do I use it?

lockstep
  • 250,273
NP01
  • 2,935

2 Answers2

261

There are a few options described in How can I make a table that takes up more than a single page?

I've put a MWE below using the longtable package; I deliberately didn't put the longtable in the table environment so that it wouldn't float, and would show that it breaks across pages.

As described in @Werner's comment, the documentation has a lot of detail.

\documentclass{article}

\usepackage{longtable}
\usepackage{lipsum} % just for dummy text- not needed for a longtable

\begin{document}
\lipsum[1]
\lipsum[1]
\lipsum[1]

%\begin{table}[h] 
%\centering
\begin{longtable}{| p{.20\textwidth} | p{.80\textwidth} |} 
\hline
foo & bar \\ \hline 
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
\caption{Your caption here} % needs to go inside longtable environment
\label{tab:myfirstlongtable}
\end{longtable}
%\end{table} 

Table \ref{tab:myfirstlongtable} shows my first longtable.
\end{document}

As a side note, some folks would advocate against using vertical lines in a table- that's a separate discussion though :) Have a look at Why not use vertical lines ('|') in a tabular?

cmhughes
  • 100,947
  • @cmhughes any idea how I can decrease the font size for text within the table? – Goaler444 May 04 '13 at 13:00
  • 4
    @Goaler444 try adding \small just after \begin{longtable} – cmhughes May 04 '13 at 15:22
  • 24
    Is there any way to keep repeating the table header at each new page? – Daniel Bonetti Aug 29 '14 at 01:50
  • 4
    When I make a table longer than a single page, it still starts the table on the next empty page. Is there a way of getting it to start from where it is in the code? – Anake Mar 01 '16 at 22:26
  • 13
    @DanielBonetti:

    \begin{longtable}[H]{|m{0.2\linewidth}|m{0.35\linewidth}|m{0.15\linewidth}|m{0.3\linewidth}|} \hline \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4}\\\hline \endfirsthead \hline \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4}\\\hline \endhead Content ... \end{longtable}

    – Michael S. Sep 05 '16 at 15:26
  • How can I center them? – alper Dec 11 '19 at 18:28
  • Putting the caption above the content throws an error for me. Any idea how to do that? Edit: nvm, I just needed to put "\" after the caption – EzPizza Mar 21 '21 at 22:57
  • I would like to add that sometimes you do not want the table to be split in the middle of a row but rather at the end. For this purpose you might add after each \\ where there is no \hline following a *, e.g. like this: Firstcolumn & Secondcolumn \\* & Secondcolumncontent \\ \hline – Emad Easa Dec 21 '21 at 09:48
  • This works great for me, but for one thing: How do I span a single row across several pages? Say you start with two relatively small rows, then you have a huge one. Well, if your page starts with the beginning of the table, then longtable will leave half of the first page empty and transfer the entire third row to the next page. How to avoid this, i.e. how to allow page breaks within rows? Thanks. – GPWR May 23 '23 at 13:39
  • @GPWR sounds like a new question – cmhughes May 23 '23 at 16:53
  • @cmhughes Yeah, you're right. I'll post that separately when I get the time, then. Peace. – GPWR May 23 '23 at 22:10
  • 1
    Thank you for this response! – cconsta1 Aug 18 '23 at 16:59
13

You may try longtblr environment of tabularray package. It has X column and can be used in two column documents.


\documentclass{article}
\usepackage[a6paper,margin=15mm]{geometry}

\usepackage{tabularray} \usepackage{xcolor}

\begin{document}

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

\begin{longtblr}[ caption = {Long Title}, label = {tab:test}, ]{ colspec = {|XX[4]|}, rowhead = 1, hlines, row{even} = {gray9}, row{1} = {olive9}, } Head & Head \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ foo & bar \ \end{longtblr}

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

\end{document}

enter image description here

L.J.R.
  • 10,932
  • This is good with automatic table title in the second page.

    By the way, to use "tabularray", maybe you should do something else.

    For overlef, one way is useful: Menu -> TeX Live Version -> 2021.

    – ML Xu Oct 31 '22 at 08:58