将字幕添加到R Markdown中的Kablextra

发布于 2025-01-29 06:48:23 字数 1390 浏览 4 评论 0原文

用于在R-Markdown PDF中报告:
我想从所需的点(由于\ n不做工作,我不知道该怎么办)中的标题行()中断标题行),或者在主标题下添加字幕。

```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "Main Title Main Title Main Title Main Title Main Title 
      Main Title Main Title Main Title Main Title Main Title Main Title",
      align = "c") %>%
  kable_styling()
```

任何想法或参考都将受到高度赞赏。

编辑。 如果我添加格式=“ pandoc”,额外的标头将消失,脚注会失去其样式。

df %>%
  kbl(caption = "Main Title Main Title Main Title Main Title \nMain Title 
      Main Title Main Title Main Title Main Title \nMain Title Main Title",
      align = "c",
      format = "pandoc") %>%
  kable_styling() %>%
  add_header_above(c(" ", " ", "params" = 2)) %>%
  add_footnote(c("note1", "note2"),
               notation = "number")

for reporting in R-Markdown pdf:
I want to either break the title line in kbl() table from the desired point ( which I do not know how to since \n does not do the job) OR add a subtitle under the main title.

```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "Main Title Main Title Main Title Main Title Main Title 
      Main Title Main Title Main Title Main Title Main Title Main Title",
      align = "c") %>%
  kable_styling()
```

enter image description here

Any idea or reference will be highly appreciated.

EDIT. If I add format="pandoc", the extra header will disappear and footnote loses its style.

df %>%
  kbl(caption = "Main Title Main Title Main Title Main Title \nMain Title 
      Main Title Main Title Main Title Main Title \nMain Title Main Title",
      align = "c",
      format = "pandoc") %>%
  kable_styling() %>%
  add_header_above(c(" ", " ", "params" = 2)) %>%
  add_footnote(c("note1", "note2"),
               notation = "number")

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2025-02-05 06:48:23

使用字幕软件包和easce = false您可以直接将latex用于字幕参数以获取字幕...

通过说明:
\\\\\创建一个线路断开
\\是逃脱乳胶ScriptSize:它可以让应用程序知道这是设置字体大小的乳胶命令。如果您不需要更改字体大小,则只需错过\\ scriptsize即可。

---
title: "table subscript title"
output: pdf_document

header-includes:
- \usepackage{caption}
---


```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

kbl(df, caption = "Main Title\\\\
      \\scriptsize subscript title  subscript title subscript title\\\\
      \\scriptsize subscript title line 2  subscript title  line 2 subscript title  line 2",
      align = "c",
      escape = FALSE) %>%
  kable_styling() %>%
  add_header_above(c(" ", " ", "params" = 2)) %>%
  add_footnote(c("note1", "note2"),
               notation = "number")

    ```

With the caption package and escape = FALSE you can use latex directly for the caption argument to get a subtitle...

By way of explanation:
\\\\ creates a line break
\\ is required to escape LaTeX scriptsize: which lets the application know this is a latex command to set the font size. If you do not need to change the font size then just miss out \\scriptsize.

---
title: "table subscript title"
output: pdf_document

header-includes:
- \usepackage{caption}
---


```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

kbl(df, caption = "Main Title\\\\
      \\scriptsize subscript title  subscript title subscript title\\\\
      \\scriptsize subscript title line 2  subscript title  line 2 subscript title  line 2",
      align = "c",
      escape = FALSE) %>%
  kable_styling() %>%
  add_header_above(c(" ", " ", "params" = 2)) %>%
  add_footnote(c("note1", "note2"),
               notation = "number")

    ```

enter image description here

在你怀里撒娇 2025-02-05 06:48:23

尝试在您的kbl中设置format =“ pandoc”,并使用\ n for subtitle,例如:

---
title: "Test"
author: "Author"
date: '2022-05-16'
output: pdf_document
---


```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "Main Title \n
      subtitle: Blabla",
      align = "c", format = "pandoc") %>%
  kable_styling() 
```

output:output:

Try setting the format = "pandoc" in your kbl and use \n for subtitle like this:

---
title: "Test"
author: "Author"
date: '2022-05-16'
output: pdf_document
---


```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "Main Title \n
      subtitle: Blabla",
      align = "c", format = "pandoc") %>%
  kable_styling() 
```

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文