49

I'm trying to compile a document with the example code for longtable but I get an error saying that longtable is not in 1-column mode. Suggest me the correct way?

2
  • 1
    Don't use longtable in twocolumn. Feb 20, 2014 at 9:21
  • 1
    On the other hand, you can use supertabular in two column.
    – user11232
    Feb 20, 2014 at 9:25

8 Answers 8

29

longtable doesn't work well with twocolumn. But you can use supertabular. Here is an example:

\documentclass[twocolumn]{article}
\usepackage{supertabular,booktabs}
\usepackage[textheight=10cm]{geometry}   %% just for this example.

\begin{document}

\tablefirsthead{\toprule First&\multicolumn{1}{c}{Name} \\ \midrule}
%
\tablehead{%
\multicolumn{2}{c}%
{{\bfseries  Continued from previous column}} \\
\toprule
First&\multicolumn{1}{c}{Name}\\ \midrule}
%
\tabletail{%
\midrule \multicolumn{2}{r}{{Continued on next column}} \\ \midrule}
\tablelasttail{%
\\\midrule
\multicolumn{2}{r}{{Concluded}} \\ \bottomrule}
\begin{supertabular}{ll}
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes here too \\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text
\end{supertabular}%
\end{document}

enter image description here

As suggested by egreg, xtab is another choice. xtab is built on supertabular while avoiding its weaknesses.

\documentclass[twocolumn]{article}
\usepackage{xtab,booktabs}
\usepackage[textheight=10cm]{geometry}   %% just for this example.

\begin{document}
\topcaption{This is top caption}
\bottomcaption{This is bottom caption}
\tablecaption{this is table caption}
\tablefirsthead{\toprule First&\multicolumn{1}{c}{Name} \\ \midrule}
%
\tablehead{%
\multicolumn{2}{c}%
{{\bfseries  Continued from previous column}} \\
\toprule
First&\multicolumn{1}{c}{Name}\\ \midrule}
%
\tabletail{%
\midrule \multicolumn{2}{r}{{Continued on next column}} \\ \midrule}
\tablelasttail{%
\\\midrule
\multicolumn{2}{r}{{Concluded}} \\ \bottomrule}
\begin{xtabular}{ll}
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text comes here too \\
    content & some text\\
    content & some text\\
    content & some text\\
    content & some text
\end{xtabular}%
\end{document}
2
  • 3
    Or maybe xtab that's based on supertabular, but it's supposed to enhance it.
    – egreg
    Feb 20, 2014 at 11:23
  • @egreg Could you please tell me how to replace Continued on next column with , for example, continue Table 1 where the table number refers to the respective table?
    – Diaa
    Jan 22, 2018 at 20:24
25

If you still want to stick to longtable (e.g. because you are generating LaTeX through pandoc) you could redefine the \longtable command.

A possible solution is this one:

\makeatletter
\let\oldlt\longtable
\let\endoldlt\endlongtable
\def\longtable{\@ifnextchar[\longtable@i \longtable@ii}
\def\longtable@i[#1]{\begin{figure}[t]
\onecolumn
\begin{minipage}{0.5\textwidth}
\oldlt[#1]
}
\def\longtable@ii{\begin{figure}[t]
\onecolumn
\begin{minipage}{0.5\textwidth}
\oldlt
}
\def\endlongtable{\endoldlt
\end{minipage}
\twocolumn
\end{figure}}
\makeatother

NB: it works with two columns, which is what you'll find very often in journal article document classes.

To use this fragment with pandoc, you can e.g. use the -H switch which lets you include some additional code before your actual content. Just create a file called preamble.tex and insert the code above. You can then run pandoc -H preamble.tex and the code will automatically be included.

4
  • 1
    Thanks for this solution! The snippet works when added after the packages are used.
    – Martin
    Oct 5, 2015 at 8:12
  • 1
    You are a life saver thank you! I also added this line \setlength{\LTcapwidth}{\linewidth} to the snippet to make the table caption span over the line without exceeding it.
    – iamaziz
    Nov 23, 2015 at 8:06
  • 1
    How would you modify this to allow a longtable* environment where the table's width spans both columns (e.g. here)?
    – Neal Kruis
    Nov 17, 2016 at 17:35
  • Does this also work with longtabu?
    – einpoklum
    May 23, 2018 at 8:16
15

Another option, not covered in the previous answers, is to force single column mode along with a \clearpage before your longtable and then reset it to two column mode after. This would look like:

\clearpage
\onecolumn
\begin{longtable}
...
\end{longtable}
\clearpage
\twocolumn

This is potentially more useful than the previous examples as it doesn't require rebuilding much, and has the added bonus of working in platforms like arXiv.

3

For the benefit of Huan and others: for hellobenallans answer to work you must first include \usepackage{xtab} in your header.

2
  • 2
    Thanks for contributing. Normally, comments belong in the comments section, but I think you can't comment with too little reputation. I added a link to your answer in the comments of the answer you commented.
    – thymaro
    Jun 5, 2020 at 4:02
  • 1
    Thanks @thymaro, it was indeed the case that I didn't have enough reputation to post a proper comment. Jun 6, 2020 at 17:28
1

I was also stuck trying to use multiple columns with pandoc, and similar to Marco Torchiano's answer I redefined the longtable environment in a preamble, but in my case I redefined it to use the xtabular environment.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine longtable because it doesn't work in multicolumn
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
\def\longtable{\@ifnextchar[\newlongtable@i \newlongtable@ii}
\def\newlongtable@i[#1]{%
\renewcommand{\endhead}{\ignorespaces}
\xtabular[#1]}
\def\newlongtable@ii{%
\renewcommand{\endhead}{\ignorespaces}
\xtabular}
\def\endlongtable{\endxtabular}
\makeatother

The code above isn't the prettiest, but seems to work all right when I call pandoc using the --include-in-header (or -H) option:

pandoc                                    \
  --standalone                            \
  --from=markdown                         \
  --template=pandoc-template.tex          \
  --include-in-header=pandoc-preamble.tex \
  --pdf-engine=xelatex                    \
  -o out.pdf                              \
  in.md

This can produce a pdf which contains tables in a two-column layout, apparently without issue.

2
  • 2
    I got a Error producing PDF. ! Undefined control sequence. \newlongtable@i ...ad }{\ignorespaces } \xtabular [#1] l.138 \begin{longtable}[]
    – Huan
    Oct 3, 2018 at 14:13
  • Apparently, you need to add xtab. See this comment-anwer.
    – thymaro
    Jun 5, 2020 at 4:04
1

An alternative solution with longtblr environment of tabularray package: it can be used in twocolumn documents.

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

\usepackage{tabularray}

\begin{document}

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.

\begin{longtblr}[
  caption = {Long Long Long Tabular},
  entry = {Short Caption},
  label = {tblr:test},
]{
  colspec = {XXX}, hlines,
  rowhead = 2, rowfoot = 1,
  row{1-2,Z} = {font=\bfseries},
}
 Head    & Head  & Head    \\
 Head    & Head  & Head    \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Alpha   & Beta  & Gamma   \\
 Epsilon & Zeta  & Eta     \\
 Iota    & Kappa & Lambda  \\
 Nu      & Xi    & Omicron \\
 Rho     & Sigma & Tau     \\
 Phi     & Chi   & Psi     \\
 Foot    & Foot  & Foot    \\
\end{longtblr}

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.

\end{document}

enter image description here

1

I came across this page looking to fix the issue with pandoc using longtable in twocolumn mode. I had use the supertabular fix above last year and it worked, but with some updates this year it was giving a \noalign error that I could not sort out.

Putting the code below (which replaces longtable with tabular) in the preamble fixed my issues (and I do not actually want multi-page tables, so this is a fine change for me).

\renewenvironment{longtable}{\begin{center}\begin{tabular}}{\end{tabular}\end{center}}
\renewcommand{\endhead}{}
\renewcommand{\toprule}[2]{\hline}
\renewcommand{\midrule}[2]{\hline}
\renewcommand{\bottomrule}[2]{\hline}
0

When using Marcos Answer with longtable for acronyms, one might change the figures to be displayed "here" ([t]->[h]), and therefore at the \printacronyms command.

\makeatletter
\let\oldlt\longtable
\let\endoldlt\endlongtable
\def\longtable{\@ifnextchar[\longtable@i \longtable@ii}
\def\longtable@i[#1]{\begin{figure}[h]
  \onecolumn
  \begin{minipage}{0.5\textwidth}
    \oldlt[#1]
    }
\def\longtable@ii{\begin{figure}[h]
  \onecolumn
  \begin{minipage}{0.5\textwidth}
    \oldlt
    }
\def\endlongtable{\endoldlt
  \end{minipage}
  \twocolumn
\end{figure}}
\makeatother

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .