編輯 (cfr)

編輯 (cfr)

当我在页面上有两个浮动环境(一个figure和一个table)时,longtable可以将其下方的内容推入页面的底部边缘。这是一个简单的例子:

\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}

以下是从中创建的 PDF 第二页的屏幕截图pdflatex

长表问题

如果我删除longtable,文本就可以正确地拆分到下一页。我想知道这个文档中存在什么问题。

$ 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

編輯 (cfr)

这是一个更简单的示例,演示了同样的问题。(可能可以进一步缩减:我主要尝试消除包裹。)

\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}

显然,溢出到右边距是意料之中的事情,因为我没有将表格调整到标准文本宽度。但溢出到底部大概就不是了。

溢出至底部边缘

答案1

在你的 markdown 源代码中,\suppressfloats在有问题的浮动元素(表格、图形)之前添加命令,然后使用它进行编译pandoc -f markdown+raw_tex

已针对此kantlip.md输入进行测试

![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]

以及kantlip.txt文件

\usepackage{kantlipsum}

我编写了这个例子,说明pandoc -Hkantlip.txt -f markdown+raw_tex -o kantlip.pdf kantlip.md

答案2

看起来你不需要可拆分到页面的长表,但你的预处理器对所有表都使用了此标记。在这种情况下,你可以将 longtable 定义为普通表。这意味着,不要加载 longtable 包,即不要使用,\usepackage{longtable}而是定义这个:

\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
   }

问题在于 LaTeX 的 longtable 实现不能与插入(LaTeX 术语中的浮点数)结合,因为它使用自己的特殊\output例程进行缓冲。

如果你真的需要将插入页与可拆分到页面的长表格结合起来,那么你可以从中得到启发我对纯 TeX 长表的实现。它可以与插入件一起使用,因为它不使用特殊\output例程。但这是另一个故事,因为这不是 LaTeX。

编辑如果需要在表格前添加标题,那么可以定义:

\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
   }

答案3

1.在 longtable\end{table}\lipsum命令之间使用\pagebreak命令

2.使用minipage环境如下图:

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

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

并在两个 tabular* 环境中以 结尾。3 \end{tabular*}. 要分离长表和小页表,可以使用\vspace{2cm}命令,以便\lipsum从第三页开始。这样做的目的是将表格和运行内容分开,以便在底部获得正确的分页符。

相关内容