如何让表格保持在同一页面上?

如何让表格保持在同一页面上?

我有一份报告,其中包含许多回归输出的汇总表,并使用 Rmarkdown 文档中的 pander 将它们转换为表格。尽管所有表格都少于一页,但它们有时会中断并延续到两页,这很难阅读。

我从 .Rmd 创建了 .tex 文件,并将 longtabs 转换为 booktabs,但无法编译文档。此外,由于表格太多,添加 \pagebreak 或 \newpage 也不是最佳解决方案。

这是 .Rmd 文件的最小示例。此处的代码将粘贴到 .Rmd 文件中。如何使表格位于同一页上?

---
title: "Put tables on the same page"
author: "author"
date: May 16, 2020
output:
  pdf_document:
    extra_dependencies: ["lipsum"]
    number_sections: yes
    toc: yes
geometry: margin=1in
fontsize: 11pt
---


```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE,
                      cache = TRUE,
                      #include = FALSE,
                      message = FALSE,
                      warning = FALSE,
                      error = TRUE,
                      fig.cap = " ",
                      number_sections = TRUE)
```
```{r}
library(tidyverse)
library(pander)
library(rmarkdown)
#panderOptions('keep.trailing.zeros', TRUE)
```


\lipsum[3-5]

```{r}
m5 = lm(log(Volume)~log(Girth)*log(Height)+Girth^2+Girth^3+Girth^4 + Height^2+Height^3+rnorm(31)+rpois(31, 1),data=trees)
pander(m5)
```

我不知道如何保持此代码中的所有块完整。如果造成混淆,请见谅。它正在将 markdown 代码转换为格式。

% relevant lines for longtable package in the preamble of generated tex document and a sample table which breaks onto two pages but is short enough to be on one page.
\usepackage{longtable,booktabs}
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}


\begin{longtable}[]{@{}cccccl@{}}
\caption{Overall Model}\tabularnewline
\toprule
\begin{minipage}[b]{0.24\columnwidth}\centering
~\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Estimate\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\centering
Std. Error\strut
\end{minipage} & \begin{minipage}[b]{0.11\columnwidth}\centering
z value\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Pr(\textgreater\textbar z\textbar)\strut
\end{minipage} & \begin{minipage}[b]{0.09\columnwidth}\raggedright
\strut
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\toprule
\begin{minipage}[b]{0.24\columnwidth}\centering
~\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Estimate\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\centering
Std. Error\strut
\end{minipage} & \begin{minipage}[b]{0.11\columnwidth}\centering
z value\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Pr(\textgreater\textbar z\textbar)\strut
\end{minipage} & \begin{minipage}[b]{0.09\columnwidth}\raggedright
\strut
\end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{(Intercept)}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-14.89\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.038\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-393.9\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var1}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-0.084\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.035\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-2.383\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.017\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
*\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var2}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.016\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.001\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
11.93\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var10}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.158\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.051\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
3.096\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.002\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var3}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.058\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.013\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
4.296\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var4}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-0.169\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.053\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-3.209\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.001\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var5}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.28\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.054\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
5.199\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var11}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.167\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.026\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
6.467\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var6}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.245\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.036\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
6.792\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\bottomrule
\end{longtable}

答案1

我建议您使用kablextable而不是pander。您将获得一个使用标准语法格式化的表格LaTeX,并允许您调用所有kablextable格式化功能。以下简单.Rmd文件改编自此处:更漂亮的结果表与扫帚和针织。它执行以下操作:

  1. 对汽车数据进行线性模型拟合
  2. 整理模型输出
  3. 将模型输出格式化为LaTeX表格。

具体来说,关于不浮动表格的问题,示例使用 设置表格浮动[H],这样它就根本不浮动了。这是通过kable_styling中的函数完成的kableExtra。如果您有longtable,只需将该选项添加到 即可kable()

这是编织文件的输出.Rmd

在此处输入图片描述

这是Rmd文件:

---
title: "TeX.SE"
output: 
   pdf_document:
     keep_tex: true
header-includes:
   - \usepackage{caption}
   - \usepackage{booktabs}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.clean = FALSE)
```

## Fit the model

```{r cars}
lmfit <- lm(mpg ~ wt, mtcars)
lmfit
```

## Cleanup model output and format table

Use \texttt{broom}'s \texttt{tidy} function to create a data frame of the model output and output that dataframe using \texttt{knitr}'s \texttt{kable} function: 

```{r output}
library(broom)        # tidyr
library(magrittr)     # %>%

library(knitr)        # kable
library(kableExtra)   # kable_styling
tidy(lmfit) %>% kable(format="latex",booktabs=TRUE,
                      caption="My table caption",digits=2) %>% 
                kable_styling(latex_options = "HOLD_position")
```
\raggedright Add \texttt{longtable} to \texttt{kable} as required. For example: \par\texttt{kable(longtable=TRUE, format="latex", booktabs=TRUE, caption="My table caption")}

这是从编译文件创建的文件LaTeX中提取的回归参数表的代码。.tex.Rmd

\begin{table}[H]
\caption{\label{tab:output}My table caption}
\centering
\begin{tabular}[t]{lrrrr}
\toprule
term & estimate & std.error & statistic & p.value\\
\midrule
(Intercept) & 37.29 & 1.88 & 19.86 & 0\\
wt & -5.34 & 0.56 & -9.56 & 0\\
\bottomrule
\end{tabular}
\end{table}

如您所见,它只是带有用于表格放置的选项的常规LaTeX代码。[H]

答案2

这是您的表格的改进版本。我删除了所有不必要的代码混乱,并添加了siunitx包以改善数字的对齐。

\documentclass{article}
\usepackage{longtable,booktabs}
\usepackage{caption}
\usepackage{siunitx}
\begin{document}
\begin{table}
\centering
\caption{Overall Model}\label{key}
\begin{tabular}[]{@{}>{\bfseries}lS[table-format=-2.3]S[table-format=1.3]S[table-format=-3.3]S[table-format=1.3]l@{}}
\toprule
 & {Estimate} & {Std. Error} & {z value} & {Pr(\textgreater\textbar z\textbar)} \\
\midrule
(Intercept) & -14.89 & 0.038 & -393.9 & 0     & * * * \\
var1        & -0.084 & 0.035 & -2.383 & 0.017 & *     \\
var2        & 0.016  & 0.001 & 11.93  & 0     & * * * \\
var10       & 0.158  & 0.051 & 3.096  & 0.002 & * *   \\
var3        & 0.058  & 0.013 & 4.296  & 0     & * * * \\
var4        & -0.169 & 0.053 & -3.209 & 0.001 & * *   \\
var5        & 0.28   & 0.054 & 5.199  & 0     & * * * \\
var11       & 0.167  & 0.026 & 6.467  & 0     & * * * \\
var6        & 0.245  & 0.036 & 6.792  & 0     & * * * \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

相关内容