0
votes

I would like to use the < symbol in a table caption of a Rmarkdown that converts to a docx document. I am using the flextable package as this gives a lot of (needed) flexibility to tables in the docx format.

But I am really confused by the multiple conversion steps through pandoc. It does not seem so easy to get to the < as it is a special coding HTML character. I have read that in HTML you would escape it via &lt;. This gives me the problem that the & has to be escaped, too. The conversion then turns &lt; into &amp;lt; (as it converts the & into &amp;) and \\&lt; would yield me &amp;amp;lt; (as it converts the & of the &amp; again). Latex does not seem to work either, I've tried <, $<$ and $\\textless$ but to no avail.

All combinations basically follow the same logic, i.e. that < are correctly transformed to &lt; but then the HTML is not converted again.

Any idea how to solve this? What do I miss?

Example RMD file:

---
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$")
```
1
If pdf, you should use set_caption("Table: (\\#tab:test) ... < or >", html_escape = FALSE). Also make sure you are using the latest version as there were some fixes about your issue 2/3 releases before the current one (0.6.2)David Gohel
Nice, thank you so much!! The html_escape works in docx, too! Thanks as well for the update suggestions, I will check the version(s) I'm running.bamphe
@DavidGohel Maybe you could help me out with another quick question: Why is the Table: (\\#tab:test) part printing Table Table 1:? If I change anything it is does not understand the label at all though...bamphe
And don't define yourself (\\#tab:test), only add in the chunk option tab.id="test"David Gohel

1 Answers

1
votes

This should answer the question about < and > in the captions


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

You could use the package officedown. It will make the references as real Word references, it also offers few features to customize your captions:

---
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).

enter image description here