Kim - 1 year ago 234
LaTeX Question

# Getting R printed texts to have color esp. in R markdown knits?

Very simple question: I love writing R notebooks/markdowns, and with something like

highlight: tango
I can give background color to codes when I knit my notebook to pdfs.

However, I don't know how to give colored backgrounds or colored fonts to printed outputs in R. For example, suppose I have the following chunk.

{r, echo = FALSE}
writeLines("Help")



I'd like to see the word Help to be highlighted, say in red font with gray background. How can I achieve this?

For PDF output, below are some latex commands to get colored text and shading. (For additional shading options, see this answer at the Tex Stack Exchange site.) However, I'm not sure how to get the output from writeLines shaded. Enclosing the code chunk in a \shaded environment causes an error. Hopefully someone will come along with a solution that works with code chunk output.

---
output: pdf_document
- \usepackage{xcolor}
- \usepackage{framed}
---

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


\color{red}

{r}
writeLines("help")


Red text with a gray background.

Red text.

\color{black}

Black text.

Black text with a red background.