r - latex kable 并排桌“不在外部标准模式”
问题描述
我正在尝试创建一个带有并排表格的页面。我使用其他 SO 答案来为一个简单的表执行此操作
```{r start-block, include=F,echo=F}
library(dplyr)
library(knitr)
library(kableExtra)
```
```{r sample, echo=FALSE, results='asis'}
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE) %>% kable_styling(latex_options = c("striped"), font_size=5)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE) %>% kable_styling(latex_options = c("striped"), font_size=5)
cat("\n")
cat("\\newpage")
cat("\\begin{table}[!htb]")
cat(c("\\begin{minipage}{.5\\linewidth}
\\caption{}
\\centering",
t1,
"\\end{minipage}%
\\begin{minipage}{.5\\linewidth}
\\centering
\\caption{}",
t2,
"\\end{minipage}") )
cat("\\end{table}")
```
我正在尝试使用自定义表(.RDS 格式文件:tbl)重现相同的行为。这在没有并排表的情况下工作正常。
```{r table, echo=FALSE, results='asis'}
tbl <- readRDS("table.RDS") #load file using the link "tbl"
cat(tbl)
```
当我并排尝试此表时,我收到错误消息。
```{r table2, echo=FALSE, results='asis'}
cat("\n")
cat("\\newpage")
cat("\\begin{table}[!htb]")
cat(c("\\begin{minipage}{.5\\linewidth}
\\caption{}
\\centering",
tbl,
"\\end{minipage}%
\\begin{minipage}{.5\\linewidth}
\\centering
\\caption{}",
tbl,
"\\end{minipage}") )
cat("\\end{table}")
```
!LaTeX 错误:不在外部标准模式下。
解决方案
我建议使用 LaTeX 包子标题来构建并排表格/图形。
.Rmd 文件示例:
---
title: "Answer for SO Question 50879745"
header-includes:
\usepackage{subcaption}
\usepackage{booktabs}
\usepackage[table]{xcolor}
---
```{r start-block}
library(dplyr)
library(knitr)
library(kableExtra)
opts_chunk$set(echo = FALSE)
```
Build two example tables based on the `mtcars` data set.
```{r sample, results='asis'}
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE) %>% kable_styling(latex_options = c("striped"), font_size=5)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE) %>% kable_styling(latex_options = c("striped"), font_size=5)
```
Modify the tables to use the `subtable` environment and added labels and
captions.
```{r}
t1 <- gsub("\\begin{table}[H]", "\\begin{subtable}[b]{0.48\\linewidth}\n\\caption{\\label{tab:1a}This is Table 1 in the example, but now labeled with a (a).}\n", t1, fixed = TRUE)
t1 <- gsub("\\end{table}", "\\end{subtable}", t1, fixed = TRUE)
t2 <- gsub("\\begin{table}[H]", "\\begin{subtable}[b]{0.48\\linewidth}\n\\caption{\\label{tab:1b}Shorter caption.}", t2, fixed = TRUE)
t2 <- gsub("\\end{table}", "\\end{subtable}", t2, fixed = TRUE)
```
Place the tables into the document.
```{r, results = "asis"}
cat("",
"\\begin{table}[!htb]",
"\\centering",
"\\caption{\\label{tab:tab1}Two tables, side-by-side.}",
t1,
t2,
"\\end{table}",
"",
sep = "\n")
```
Another example, start by reading in a rds file.
```{r table, results = "asis"}
tbl <- readRDS("table.RDS") #load file using the link "tbl"
cat(tbl)
```
A few modifications to Table~\ref{tab:comments-block} will be made so that we
can show the table twice in a `subtable`.
```{r table2_mod}
tbl <- gsub("\\begin{table}", "\\begin{subtable}[t]{0.48\\linewidth}", tbl, fixed = TRUE)
tbl <- gsub("\\end{table}", "\\end{subtable}", tbl, fixed = TRUE)
```
Printing the tables works, see \ref{tab:tab3}, but there is overlap as the
tables are too wide. Consider the `tabularx` package as a solution or an ad hoc
approach of changing the font size.
```{r, results = "asis"}
cat("",
"\\begin{table}[!htb]",
"\\centering",
"\\caption{\\label{tab:tab3}Two tables, side-by-side.}",
# "\\scriptsize",
tbl,
tbl,
"\\end{table}",
# "\\normalsize",
"",
sep = "\n")
```
变成了这个pdf:
此示例的文件和图形可以在这里找到:https ://github.com/dewittpe/so/tree/master/50879745
推荐阅读
- amazon-web-services - 适用于 Windows 的 Cloudformation CF 脚本不从 s3 复制文件
- typescript - 在类基础组件中无法访问 vue 3 中的道具
- python-3.x - django 如何在主 urls.py 之前获取请求
- android-studio - 在kotlin中重置按钮的颜色
- ansible - 使用 ansible 在 vxworks 上运行 ssh 命令
- javascript - Selenium:元素在点(x,y)处不可点击
- rust - 如何在 `&Box 上进行模式匹配
` 不使用 `as_ref` - javascript - 将数据库显示为表格 - html
- flutter - _TypeError(类型'String'不是'index'类型'int'的子类型)
- r - 规范化 R 中的每行数据