3

I have several longtables which are auto generated. Since they are not actually long or wide ones, it would be rather space saving for me if I could pack them in a multi column environment.

So, I tried something like this.

\documentclass{article}

\usepackage{longtable}

\usepackage{multicol}

\usepackage{filecontents}

\begin{document}

\begin{filecontents}{\jobname autotables.tex}
  \begin{longtable}{|l|l|} 
    1&2\\
    3&4
  \end{longtable}
  \begin{longtable}{|l|l|} 
    5&6\\
    7&8
  \end{longtable}
  \begin{longtable}{|l|l|} 
    9&A\\
    B&C
  \end{longtable}
  \begin{longtable}{|l|l|} 
    D&E\\
    F&0
  \end{longtable}
\end{filecontents}

\begin{multicols}{4}
  \input \jobname autotables.tex
\end{multicols}

\end{document}

But this generates an error message like,

! Package longtable Error: longtable not in 1-column mode.

Indeed, the package documentation says,

longtable now issues an error if started in the scope of \twocolumn, or the multicols environment.

So, looks like I am facing a stumbling block.

However, I am making this post with the hope that somebody could point me to a solution.

Here are the facts and/or bottom lines,

  1. The tables are auto generated and anything other than longtable can not be used.
  2. The tables are always inside a single file, and the number of tables is not fixed.
  3. Post editing the auto generated file is not an option.

UPDATE 1

Tried the code at https://tex.stackexchange.com/a/46001/14103. Unfortunately, the trick works when there is only one longtable inside multicols.

See the following code where the first case works but the second one does not.

\documentclass{article}

\usepackage{longtable}

\usepackage{multicol}

\usepackage{filecontents}

\makeatletter
\newsavebox\ltmcbox
\def\putdbtables#1{\setbox\ltmcbox\vbox{
    \makeatletter\col@number\@ne
    \input {#1}
    \unskip
    \unpenalty
    \unpenalty}
    \unvbox\ltmcbox}
\makeatother

\begin{document}

\begin{filecontents}{\jobname onetables.tex}
  \begin{longtable}{|l|l|} 
    1&2\\
    3&4
  \end{longtable}
\end{filecontents}

\begin{filecontents}{\jobname twotables.tex}
  \begin{longtable}{|l|l|} 
    1&2\\
    3&4
  \end{longtable}
  \begin{longtable}{|l|l|} 
    5&6\\
    7&8
  \end{longtable}
\end{filecontents}

% This will work
\begin{multicols}{4}
  \putdbtables{\jobname onetables.tex}
\end{multicols}

% This will generate an error
\begin{multicols}{4}
  \putdbtables{\jobname twotables.tex}
\end{multicols}

\end{document}
Masroor
  • 17,842
  • 2
    longtable doesn't work with multicolumn/twocolumn. Use supertabular instead (I have read the last lines, but unless David does something, it won't work). –  Oct 21 '14 at 08:22
  • BTW, there is one question (similar) where David has an answer. I am unable to find it though for now. –  Oct 21 '14 at 08:24
  • @HarishKumar I googled thoroughly before asking this question. If you could point me to the one you mentioned, I can propose to close my question, provided that one provides a solution. – Masroor Oct 21 '14 at 08:29
  • Gotcha! Balancing long table inside multicol in LaTeX. I think this is the one. But there may be others too. Please don't close yet. Let David come for answer. –  Oct 21 '14 at 08:32
  • that link is OK there is (or will be) some experimental longtable v5 code on googlecode (google for it it's not ready yet:-) but for now that answer is what there is. Or you could use supertabular or xtab which work in multicols (but don't take part in the balancing) – David Carlisle Oct 21 '14 at 08:46
  • @DavidCarlisle The trick did not work for me. See the update in my question. – Masroor Oct 21 '14 at 11:31
  • @Masroor you can have as many longtables as you want inside multicols but you need the box/unbox on each separately. – David Carlisle Oct 21 '14 at 11:35
  • @DavidCarlisle Unfortunately I am not in a position to separate the longtables. Please see the enumerated points at the end of my original post (just above UPDATE 1). – Masroor Oct 21 '14 at 12:07
  • @Masroor well splitting the tables is the current solution. – David Carlisle Oct 21 '14 at 12:13
  • @DavidCarlisle Do you mean putting the tables in separate files? That is somewhat tedious (please read almost impossible) since they are too many in number, all of which are auto generated. – Masroor Oct 21 '14 at 12:26
  • They don't need to be in separate files just generate them with the wrapping code around each one. – David Carlisle Oct 21 '14 at 12:34
  • @DavidCarlisle I do not control the generating program at all, nor there is any chance of getting control of this. The (long)tables are in a single file, and need to be used as they are. – Masroor Oct 21 '14 at 12:46

1 Answers1

1

You may try longtblr environment of the new LaTeX3 package tabularray. It works inside multicols environment:

\documentclass{article}

\usepackage{tabularray}

\usepackage{multicol}

\usepackage{filecontents}

\begin{document}

\begin{filecontents}{\jobname autotables.tex} \begin{longtblr}{|l|l|} 1&2\ 3&4 \end{longtblr} \begin{longtblr}{|l|l|} 5&6\ 7&8 \end{longtblr} \begin{longtblr}{|l|l|} 9&A\ B&C \end{longtblr} \begin{longtblr}{|l|l|} D&E\ F&0 \end{longtblr} \end{filecontents}

\begin{multicols}{4} \input \jobname autotables.tex \end{multicols}

\end{document}

enter image description here

L.J.R.
  • 10,932