15

When I have two floating environments on a page (one figure and one table), the longtable can push the content below it into the bottom margin of the page. Here is a minimal example:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{lipsum}
\usepackage{longtable,booktabs}
\usepackage[demo]{graphicx}

\title{R Markdown v2 Demo}
\author{Li Lei \\ Han Meimei}
\date{2015/01/01}


\begin{document}
\maketitle

\lipsum


\begin{figure}
\includegraphics[width=0.5\linewidth]{foo-1}
\includegraphics[width=0.5\linewidth]{foo-2}
\caption{Two plots placed side by side.}
\end{figure}


\begin{longtable}[c]{lrrrrrrrrrrr}
\caption{A table of the first 10 rows of the mtcars
data.}\tabularnewline
\toprule
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear &
carb\tabularnewline
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 &
4\tabularnewline
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4
& 4\tabularnewline
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 &
1\tabularnewline
\bottomrule
\end{longtable}


\begin{table}
\caption{A Tale of Two Tables.}
\centering
\begin{tabular}{rr}
\toprule
Sepal.Length & Sepal.Width\\
\midrule
5.1 & 3.5\\
4.9 & 3.0\\
4.7 & 3.2\\
\bottomrule
\end{tabular}
\begin{tabular}{lrrr}
\toprule
  & mpg & cyl & disp\\
\midrule
Mazda RX4 & 21.0 & 6 & 160\\
Mazda RX4 Wag & 21.0 & 6 & 160\\
Datsun 710 & 22.8 & 4 & 108\\
Hornet 4 Drive & 21.4 & 6 & 258\\
Hornet Sportabout & 18.7 & 8 & 360\\
\bottomrule
\end{tabular}
\end{table}

\lipsum

\end{document}

Here is a screenshot of the second page of the PDF created from pdflatex:

longtable problem

If I remove the longtable, the text can be correctly broken onto the next page. I wonder what the problem is in this document.

$ pdflatex --version
pdfTeX 3.14159265-2.6-1.40.16 (TeX Live 2015)
kpathsea version 6.2.1
Copyright 2015 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
Compiled with libpng 1.6.17; using libpng 1.6.17
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with xpdf version 3.04

EDIT (cfr)

Here's a more minimal example demonstrating the same problem. (Could probably be pared down further: I mostly tried to eliminate packages.)

\documentclass{article}
\usepackage{lipsum}
\usepackage{longtable}
\title{R Markdown v2 Demo}
\author{Li Lei}
\date{2015/01/01}

\begin{document}
\maketitle

\lipsum

\begin{figure}
  ab
  \caption{Two plots placed side by side.}
\end{figure}

\begin{longtable}[c]{lrrrrrrrrrrr}
  \caption{A table of the first 10 rows of the mtcars
    data.}\tabularnewline
  \hline
  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear &
  carb\tabularnewline
  \hline
  Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 &
  4\tabularnewline
  Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4
  & 4\tabularnewline
  Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 &
  1\tabularnewline
  \hline
\end{longtable}

\begin{table}
  \caption{A Tale of Two Tables.}
  \centering
  \begin{tabular}{rr}
    \hline
    Sepal.Length & Sepal.Width\\
    \hline
    5.1 & 3.5\\
    4.9 & 3.0\\
    4.7 & 3.2\\
    \hline
  \end{tabular}
\end{table}

\lipsum

\end{document}

Obviously, the spilling into the right margin is expected here as I've not adapted the table to the standard text width. But the spilling into the bottom presumably is not.

spill into bottom margin

9
  • As far as I can see, the number of rows in the longtable is rather small. Why longtable then?
    – user31729
    Nov 5, 2015 at 6:59
  • Because the LaTeX code for the table was generated automatically from Pandoc and Markdown, and I don't have control over what environment Pandoc uses for tables.
    – Yihui Xie
    Nov 5, 2015 at 16:55
  • You can remove the dependency on geometry and graphicx.
    – cfr
    Nov 8, 2015 at 3:51
  • Please roll back my edit if the example I've posted no longer reflects your intention. However, it seems to show that longtable alone is sufficient to create the issue. (Unless lipsum is to blame which seems unlikely.)
    – cfr
    Nov 8, 2015 at 3:57
  • 1
    As it is noticed by the OP the problem is due to floating in top area, one can prevent this by \@topnum=0(\z@ is 0) now we can noticed that the problem still if the figure go to the bottom area so \@botnum\z@. The conclusion (not very sure) if a longtable go in only one page, this page can't have floats in top or bottom. you can see this if you use \begin{figure}[hbp]
    – touhami
    Nov 10, 2015 at 6:13

3 Answers 3

6
+500

In your markdown source code add the \suppressfloats command before the offending floats (tables, figures), and compile with pandoc -f markdown+raw_tex.

Tested on this kantlip.md input

![Two plots placed side by side.](frog.jpg)

\kant[2-3]

\suppressfloats
\begin{figure}
\includegraphics[width=0.5\linewidth]{frog}
\includegraphics[width=0.5\linewidth]{frog}
\caption{Two plots placed side by side.}
\end{figure}

\begin{longtable}[c]{lrrrrrrrrrrr}
\caption{A table of the first 10 rows of the mtcars
data.}\tabularnewline
\toprule
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear &
carb\tabularnewline
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 &
4\tabularnewline
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4
& 4\tabularnewline
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 &
1\tabularnewline
\bottomrule
\end{longtable}

    Sepal.Length   Sepal.Width
  -------------- -------------
             5.1           3.5
             4.9           3.0
             4.7           3.2

  : A Tale of Two Tables.

\kant[17-19]

and with the kantlip.txt file

\usepackage{kantlipsum}

I compiled this example stating pandoc -Hkantlip.txt -f markdown+raw_tex -o kantlip.pdf kantlip.md.

6
  • Thanks! I wish I don't have to deal with this case by case. What if I add/delete some content before/after the offending floats, or move the longtable around... I certainly don't want to be in this situation: researchinprogress.tumblr.com/post/34692517685/… :)
    – Yihui Xie
    Nov 10, 2015 at 18:10
  • iiuc, the \suppressfloats is local to the current page, so that you only need to add it whenever your floats misbehave.
    – jarnosz
    Nov 10, 2015 at 18:29
  • @Yihui you can always use \suppresfloats[t] immediately after \end{longtable} as you never want a floating table entered after the table to "float back" before or into the table. (you should probably give erreka the bounty:-) Nov 10, 2015 at 20:33
  • @DavidCarlisle Are you sure it is only after \end{longtable}, and not also before \begin{figure}?
    – jarnosz
    Nov 10, 2015 at 23:41
  • @erreka reasonably sure:-) Nov 10, 2015 at 23:42
4

It seems that you need not the long tables breakable to the pages but your preprocessor uses this marking for all tables. In such case, you can define longtable as normal table. It means, don't load longtable package, i.e don't use \usepackage{longtable} but define this:

\newenvironment{longtable}[2][c]
   {\table[h]
    \bgroup \def\caption##1{\omit\gdef\captiontext{##1}}
    \ifx c#1\centering\fi \ifx l#1\raggedright\fi \ifx r#1\raggedleft\fi
    \tabular{#2}
   }
   {\endtabular
    \egroup
    \ifx\captiontext\undefined \else
       \caption{\captiontext} \global\let\captiontext=\undefined \fi
    \endtable
   }

The problem is that LaTeX's longtable implementation cannot be combined with inserts (floats in LaTeX terminology), because it uses its own special \output routine for buffering.

If you really needs to combine inserts with long tables breakable to the pages then you can be inspired by my implementation of long tables for plain TeX. It works with inserts together because it doesn't use special \output routine. But this is another story, because this isn't LaTeX.

Edit If you need to have the caption before the table, then you can define:

\newenvironment{longtable}[2][c]
   {\table[h]
    \setbox0=\vbox\bgroup \def\caption##1{\omit\gdef\captiontext{##1}}
    \ifx c#1\centering\fi \ifx l#1\raggedright\fi \ifx r#1\raggedleft\fi
    \tabular{#2}
   }
   {\endtabular
    \egroup
    \ifx\captiontext\undefined \else 
       \caption{\captiontext}\par\medskip \global\let\captiontext=\undefined \fi
    \unvbox0 
    \endtable
   }
9
  • not related. Isn't better if the caption go before the tabular?
    – touhami
    Nov 10, 2015 at 7:15
  • @touhami IMHO, it is related. I've added the alternative right now.
    – wipet
    Nov 10, 2015 at 7:35
  • Thanks! That is very helpful. Originally I was looking for a fix using LaTeX instead of TeX, but the comment "LaTeX's longtable implementation cannot be combined with inserts" has largely solved my puzzle. Even if the answer is "No, this is not possible", I'd still consider it a valid answer.
    – Yihui Xie
    Nov 10, 2015 at 18:14
  • 3
    @Yihui Not really, You can not have floats within a longtable (but you shouldn't want that) but floats on the page that starts or ends the longtable should work (they don't always, but that's a bug) this particular one is related to this (but not fixed by the patch there) latex-project.org/cgi-bin/ltxbugs2html?pr=tools/3512 Nov 10, 2015 at 20:29
  • @DavidCarlisle As you mentioned, the second document in the OP doesn't work although the patched longtable.sty is used (from the patch mentioned by you). Because the whole longtable.sty is conceptually bad and the patch solves fragile parts by another fragile parts. I have studied the longtable.sty yesterday because of this thread and I have experimented with \pagegoal, of course. The problem is deeper than the patch.
    – wipet
    Nov 10, 2015 at 20:55
0

1.In between longtable \end{table} and \lipsum command use \pagebreak command

2.Use minipage environment as shown below:

\begin{minipage}[t]{0.5 \linewidth}
\begin{tabular*}{0.9 \textwidth}{@{\extracolsep{\fill}}rr}

and

\begin{minipage}[b]{0.5 \linewidth}
\begin{tabular*}{0.9 \textwidth}{@{\extracolsep{\fill}}lrrr} 

and end with \end{tabular*} in both tabular* environments.3. To separate the long table and minipage tables you can use \vspace{2cm} command so that the \lipsum will start on third page. The idea is to separate the table and running matters so as to get the correct page breaks at the bottom.

1
  • I fixed you wiki markup (you can not use \\ to get \ ) but it isn't clear that this answer addresses the question at all, which is about longtable Nov 5, 2015 at 13:03

You must log in to answer this question.

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