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.

  • Just as reference for other people: “Because of the requirement to auto-balance material and the use of columns with different width there are some restrictions with respect to floats: it is only possible to use full-width floats as the notion of a general column width doesn't exist and the balancing would make float placements very complicated.” – Frank Mittelbach in What are the main advantages to using the multicol package over the twocolumn documentclass option? – Speravir Feb 23 '14 at 0:41
  • @Speravir yes but that quote isn't the issue: the OP was using a full width float. – David Carlisle Feb 23 '14 at 0:49
  • @DavidCarlisle: Yes, but I did not immediately realize and wanted to give a hint for others. – Speravir Feb 23 '14 at 0:53
4

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}
  • These two boxes could also be merged, or not? – Speravir Feb 23 '14 at 0: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 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.