3

I need to be able to highlight all text in an r-markdown document that has been inserted using an inline code chunk. E.G r TEXT. This is to enable editing of the automated Word document creation.

I have tried using

.highlight {
  background-color: lightpink;
  border: 3px solid red;
  font-weight: bold;
}

r sprintf("<span class='highlight'>%s</span>",PNAME)

AND

r text_spec(TEXT, color = "red")

However, I suspect these are not working due to the reference .docx that I am using over-riding the styles. Is there a way to still use the reference doc and have the highlighting??

Thanks in advance.

Silas

silas
  • 41
  • 4

1 Answers1

2

Using officedown::rdocx_document: default as the output type, we can use ftext and fp_text function from {officer} package to highlight text that were inserted using inline r code chunk.

---
title: "Inline code styling"
output:
  officedown::rdocx_document: default
---

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

library(officer)
ft <- officer::fp_text(shading.color = "yellow")

word_spec <- function(x, prop = ft) ftext(text = toString(x) ,prop =  ft)
```


## Inline code highlighting  for word document

We can highlight text in an r-markdown document that has been inserted using an inline code for output type word document too.

- `r word_spec("text in inline code is highlighted")`
- The sum of 2 + 2 is `r word_spec(2 + 2)`
- The sequence from 1 to 10 is `r word_spec(1:10)`

And the rendered word document looks like this,

inline_code_style_for_word_output

shafee
  • 15,566
  • 3
  • 19
  • 47