R markdown pdf更改并排表格的标题标签

R markdown pdf change caption's labels of side by side tabels

关于这个问题:

按照彼得对相关问题的回答中的一个小例子。

    ---
    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") 
    ```

我可以并排打印两个 table。我要做的是将两个 table 的标题标签从“(a)”更改为 "Table 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 结构不正确。

..有什么建议吗?

如果您想要两个独立的表而不是两个连接的子表,您可以使用 this TEX.SE answer 中建议的方法之一,例如

---
output:
  pdf_document:
      keep_tex: yes
header-includes:
-  \usepackage{booktabs}
---


```{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 `minipage` environment and added labels and
captions.

```{r}
t1 <- gsub("\begin{table}[H]",
           "\begin{minipage}{0.48\linewidth}\n\caption{\label{tab:1}This is Table 1.}\n",
           t1, fixed = TRUE)
t1 <- gsub("\end{table}", "\end{minipage}", t1, fixed = TRUE) 

t2 <- gsub("\begin{table}[H]",
           "\begin{minipage}{0.48\linewidth}\n\caption{\label{tab:2}Shorter caption.}",
           t2, fixed = TRUE)
t2 <- gsub("\end{table}", "\end{minipage}", t2, fixed = TRUE) 
```

Place the tables into the document.

```{r, results = "asis"}
cat("",
    "\begin{table}[!htb]",
    "\centering",
    t1,
    t2,
    "\end{table}",
    "",
    sep = "\n") 
```

结果: