如何在 rmarkdown 中使用 bookdown 创建 huxtable 表标题?
我的 rmarkdown 中有许多 huxtable 表.我想使用 bookdown 为它们添加标题.到目前为止,我一直无法使用其他 R 包生成表格"的 bookdown 说明来执行此操作(请参阅上面的 URL).
I have numerous huxtable tables in my rmarkdown. I'd like to caption them using bookdown. So far I've been unable to do this using the bookdown instructions for "other R packages to generate tables" (see URL above).
这是一个示例,它遵循此答案中的说明:
Here's an example which follows the instructions in this answer:
---
title: "huxtable-mwe"
site: bookdown::bookdown_site
output:
bookdown::html_book
documentclass: book
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(huxtable)
library(magrittr)
```
See table \@ref(tab:bar).
Table (\#tab:foo): Foo
```{r foo, echo=FALSE}
ht <- hux(
foo = c('foo','bar')
) %>%
set_all_borders(1)
ht
```
See table \@ref(tab:foo).
Table (\#tab:bar): Bar
```{r bar, echo=FALSE}
ht <- hux(
foo = c('bar', 'baz')
) %>%
set_all_borders(1)
ht
```
参考文献有效,但我得到以下表格标题:
References work, but I get the following table captions:
表 (#tab:foo): Foo
Table (#tab:foo): Foo
表格(#tab:bar):条形
Table (#tab:bar): Bar
在我预期的时候:
表 1:Foo
表 2:条形
感谢 MWE.
已解决.使用 set_caption(...)
将标题放入 <caption>...</caption>
元素,并且不要对标签进行转义:
Resolved. Use set_caption(...)
to get the caption into a <caption>...</caption>
element, and don't escape the label:
---
title: "huxtable-mwe"
site: bookdown::bookdown_site
output:
bookdown::html_book
documentclass: book
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(huxtable)
library(magrittr)
```
See table \@ref(tab:bar).
```{r foo, echo=FALSE}
ht <- hux(
foo = c('foo','bar')
) %>%
set_all_borders(1) %>%
set_caption('(#tab:foo) Foo')
ht
```
See table \@ref(tab:foo).
```{r bar, echo=FALSE}
ht <- hux(
foo = c('bar', 'baz')
) %>%
set_all_borders(1) %>%
set_caption('(#tab:bar) Bar')
ht
```