4

For a long time, I have been looking for a nice way to have balanced tables inside a multicol environment. Especially, I would like to have automatically balancing multicol-tables inside a table float. While I always thought that this is merely impossible with LaTeX in its current state, I have recently discovered an extraordinary useful answer on this topic:

Balancing long table inside multicol in LaTeX

However, when I apply this technique to the case I'm investigating, I encounter a somewhat odd behaviour. Please consider the following example:

\documentclass{article}

\usepackage{longtable}
\usepackage{multicol}
\usepackage{lipsum}

\title{Lorem ipsum}
\date{\today}
\author{\LaTeX}

\makeatletter
\newsavebox\ltmcbox
\newenvironment{multicolslongtable}[1]{
  \setbox\ltmcbox\vbox\bgroup
  \col@number\@ne
  \begin{longtable}{#1}
}{
  \end{longtable}
  \unskip
  \unpenalty
  \unpenalty\egroup
  \unvbox\ltmcbox
}  
\makeatother

\begin{document}

\maketitle

\begin{multicols}{2}

\lipsum[1-3]

\begin{table*}[t]
\begin{multicols}{2}
\begin{multicolslongtable}{c l}
1 &Lorem\tabularnewline
2 &ipsum\tabularnewline
3 &dolor\tabularnewline
4 &si\tabularnewline
5 &amet\tabularnewline
6 &consectetuer\tabularnewline
7 &adipiscing\tabularnewline
8 &elit\tabularnewline
\end{multicolslongtable}
\end{multicols}
\caption{Words of the first sentence by index.}
\end{table*}

\lipsum[4-10]

\end{multicols}

\end{document}

page 1 page2

Please note that:

  • There is a large gap in the right column of the first page.
  • The text of both columns overruns the boundary on the first page, but not on the subsequent page(s)

I would be glad for any suggestions on how to fix this and improve my multicolslongtable environment.

carsten
  • 2,966

1 Answers1

5

I blame Frank:-)

That is, I think the problem is unrelated to longtable, but rather that it is dangerous to nest multicols environments.

If you use the inner multicols to hold the longtable before starting the outer one, and save the result in a box to add to the float. it works better:

\documentclass{article}

\usepackage{longtable}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage{capt-of}

\title{Lorem ipsum}
\date{\today}
\author{\LaTeX}

\makeatletter
\newsavebox\ltmcbox
\newsavebox\xxbox
\newenvironment{multicolslongtable}[1]{
  \setbox\ltmcbox\vbox\bgroup
  \col@number\@ne
  \begin{longtable}{#1}
}{
  \end{longtable}
  \unskip
  \unpenalty
  \unpenalty\egroup
  \unvbox\ltmcbox
}  
\makeatother

\begin{document}

\maketitle

\savebox\xxbox{\begin{minipage}{\textwidth}
\begin{multicols}{2}
\begin{multicolslongtable}{c l}
1 &Lorem\tabularnewline
2 &ipsum\tabularnewline
3 &dolor\tabularnewline
4 &si\tabularnewline
5 &amet\tabularnewline
6 &consectetuer\tabularnewline
7 &adipiscing\tabularnewline
8 &elit\tabularnewline
\end{multicolslongtable}
\end{multicols}
\captionof{table}{Words of the first sentence by index.}
\end{minipage}}

\begin{multicols}{2}

\lipsum[1-3]

\begin{table*}[t]
\usebox\xxbox
\end{table*}

\lipsum[4-10]

\end{multicols}

\end{document}
David Carlisle
  • 757,742
  • These two boxes could also be merged, or not? – Speravir Feb 23 '14 at 00:45
  • @Speravir they could here but best not really, the inner one is local use just within the environment, to wrap longtable. the extra one declared here needs 1 box per table or at least one box for all tables in a given multicol as it globally needs to hold the precomputed float body. – David Carlisle Feb 23 '14 at 00:47