Search code examples
rr-markdowndocxflextable

Rmarkdown to docx less-than symbol in table caption


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

Solution

  • 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