簡體   English   中英

R markdown pdf 更改並排表的標題標簽

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

參考這個問題: Z25F7E525B5C0641E1EB1FB3BDEBEB15B5Z kable 並排表“不在外部標准模式”

以下是彼得對相關問題的回答中的一個小例子。

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

我可以並排打印兩張桌子。 我要做的是將兩個表的標題 label 從“(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 結構不正確。

..有什么建議嗎?

如果你想要兩個獨立的表而不是兩個連接的子表,你可以使用這個 TEX.SE 答案中建議的方法之一,例如

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

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM