【发布时间】:2019-09-23 10:18:02
【问题描述】:
参考这个问题: latex kable side-by-side tables “Not in outer par mode”
按照彼得对相关问题的回答中的一个小例子。
---
title: "example"
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")
```
我可以并排打印两张桌子。我要做的是将两个表的标题标签从“(a)”更改为“表1”。 我试图直接从字符串中更改它:
```{r}
t1 <- gsub("\\begin{table}[H]", "\\begin{subtable}[b]{0.48\\linewidth}\n\\caption{\\label{tab:Table 1}This is Table 1 in the example, but now labeled with a (a).}\n", t1, fixed = TRUE)
t2 <- gsub("\\begin{table}[H]", "\\begin{subtable}[b]{0.48\\linewidth}\n\\caption{\\label{tab:Table 2}Shorter caption.}", t2, fixed = TRUE)
```
但没有任何改变。 如果我直接在 kable 选项中更改标签:
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE, labels="Table",caption="Test") %>%
kable_styling(latex_options = c("striped"), font_size=5)
我得到了错误
! Package caption Error: \caption outside float.
因为 Latex 结构不正确。
..有什么建议吗?
【问题讨论】: