Home > Net >  Using markdown in flextable caption
Using markdown in flextable caption

Time:11-05

I want to use bold and italics in a flextable caption. I know that it is possible to apply font syles to text within the header or body of a flextable using the ftExtra package and one can choose different output styles for a caption in word output by officeR. But either these do not work with the caption (ftExtra) or do not provide a style that allows selective application of bold and italics.

Here is some code with the intended markdown in the caption:

library(flextable)
library(dplyr)

iris[1:10,] %>% 
  flextable() %>% 
  set_caption(caption = "**Tab. 1:** First ten (*n* = 10) samples of the Iris species dataset")

The caption should read as "Tab. 1: First ten (n = 10) samples of the Iris species dataset"

UPDATE

based on the comment by dario a possible solution would be:

  library(flextable)
  library(dplyr)

  iris[1:10,] %>% 
  flextable() %>% 
  
  add_header_lines("") %>% 
  
  compose(
    i = 1, part = "header",
    value = as_paragraph(
      as_chunk("Table 1: ", props = fp_text(bold = TRUE)),
      as_chunk("First ten ("),
      as_chunk("n", props = fp_text(italic = TRUE)),
      as_chunk(" = 10) samples of the Iris species dataset")
    )
  )

Thank you very much. In the end I need to produce about 20 tables in different chapters and so I will try the suggestion by David to build the captions during knitting a markdown. But it is good to know that there is way, although clunky and not flexible using compose.

CodePudding user response:

I prepared a markdown template for you:

---
title: "Hello World"
header-includes:
- \usepackage{caption}

output:
  pdf_document: 
       latex_engine: xelatex
---

```{r, include = FALSE}
library(flextable)
library(dplyr)    
table2 <- flextable(mtcars[1:5, 1:6])        
```     
   
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

\begin{table}[hbtp]
\captionof{table}{Text of the caption.}
`r table2`
\end{table}

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

An output

enter image description here

  • Related