【问题标题】:Rmarkdown to docx less-than symbol in table captionRmarkdown 到 docx 小于表标题中的符号
【发布时间】:2021-01-25 19:47:35
【问题描述】:

我想在转换为 docx 文档的 Rmarkdown 的表格标题中使用 < 符号。我正在使用 flextable 包,因为这为 docx 格式的表格提供了很多(需要的)灵活性。

但我真的对通过pandoc 进行的多个转换步骤感到困惑。找到< 似乎并不容易,因为它是一个特殊的编码 HTML 字符。我读过在 HTML 中你可以通过< 转义它。这给了我一个问题,& 也必须被转义。然后转换将< 转换为<(因为它将& 转换为&)并且\\< 将产生<(因为它再次转换&&)。乳胶似乎也不起作用,我尝试过<$<$$\\textless$,但无济于事。

所有的组合基本上都遵循相同的逻辑,即<被正确转换为<但随后HTML不再转换。

知道如何解决这个问题吗?我错过了什么?

RMD 文件示例:

---
title: "Untitled"
author: "Unkown"
date: "1/25/2021"
output: bookdown::word_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(tidyverse)
```

## R Markdown

This is an R Markdown document, see Table \@ref(tab:test).


```{r test, echo = F}
flextable(head(cars, n = 10)) %>% 
  bold(part = "header") %>%
  autofit() %>% 
  set_caption("Table: (\\#tab:test) Example caption with less-than symbol: \\&lt; or &lt; or < or $<$ or $\\textless$")
```

【问题讨论】:

  • 如果是pdf,你应该使用set_caption("Table: (\\#tab:test) ... &lt; or &gt;", html_escape = FALSE)。还要确保您使用的是最新版本,因为在当前版本 (0.6.2) 之前的问题 2/3 版本中有一些修复
  • 很好,非常感谢!! html_escape 也可以在 docx 中使用!也感谢您的更新建议,我会检查我正在运行的版本。
  • @DavidGohel 也许你可以帮我解决另一个问题:为什么Table: (\\#tab:test) 部分打印Table Table 1:?如果我改变任何东西,它根本不理解标签......
  • 并且不要定义自己(\\#tab:test),只添加块选项tab.id="test"

标签: r r-markdown docx flextable


【解决方案1】:

这应该回答了标题中关于&lt;&gt;的问题


---
title: "Untitled"
output: bookdown::word_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(tidyverse)
```

## R Markdown

This is an R Markdown document, see Table \@ref(tab:test).


```{r test, echo = F, tab.id="test"}
flextable(head(cars, n = 10)) %>% 
  bold(part = "header") %>%
  autofit() %>% 
  set_caption("Example caption with less-than symbol: > and <")
```

您可以使用软件包 officedown。它将引用作为真正的 Word 引用,它还提供了一些自定义标题的功能:

---
output:
  bookdown::markdown_document2:
    base_format: officedown::rdocx_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, tab.cap.style="Table Caption")
library(flextable)
library(tidyverse)
```


```{r test1}
flextable(head(cars, n = 10)) %>% 
  bold(part = "header") %>%
  autofit() %>% 
  set_caption("Example caption with less-than symbol: > and <")
```


```{r "test2", tab.cap="Example caption with less-than symbol: > & <"}
flextable(head(cars, n = 10)) %>% 
  bold(part = "header") %>%
  autofit()
```



\newpage 

See \@ref(tab:test1).

See \@ref(tab:test2).

【讨论】:

  • 太棒了!并感谢使用 officedown 的提示 :)
猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 2015-08-07
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多