【问题标题】:R markdown pdf change caption's labels of side by side tabelsR markdown pdf更改并排表格的标题标签
【发布时间】: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 结构不正确。

..有什么建议吗?

【问题讨论】:

标签: r pdf knitr kable


【解决方案1】:

如果您想要两个独立的表而不是两个连接的子表,您可以使用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") 
```

结果:

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2015-09-03
    • 2017-10-15
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多