Home > database >  Using hex code to change text color in RMarkdown PDF (R)
Using hex code to change text color in RMarkdown PDF (R)

Time:10-14

I'm trying to change the text color in an RMD file using a PDF output. I'm doing this for work and need to use a specific hex color code.

I was able to successfully change the text color using the command:

Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}

Is it possible to change the text color in a PDF output using color hex codes? Example:

Roses are \textcolor{#1C8FCE}{#1C8FCE}, violets are \textcolor{#BBD531}{#BBD531}

Any and all help is appreciated.

CodePudding user response:

Yes, you can, see there and there:

You can also use CSS:

e.g.:

```{css echo=FALSE}
.my color {
  color: #FF0000;
  font-weight: 700
}
```


#R Markdown
`r sprintf("<span class='my color'>BLAH-BLAH</span>")`

To make a pdf from html. "Knit to Html" - > "Open in Browser" -> "Save as PDF..."

CodePudding user response:

It is easy in html, where you can just use <span>. In PDF, one can first define custom colors in a latex preamble and then use this in the markdown text.

Create a file preamble.tex and put it at a suitable location, e.g. drive d: where we definecustom colors:

\usepackage{color}
\definecolor{myred}{HTML}{E05428}
\definecolor{myviolet}{HTML}{7653B5}

Then it can be used in a markdown document, e.g. file test.Rmd:

---
output:
  pdf_document:
      includes:
          in_header: "d:/preamble.tex"
---

Roses are \textcolor{myred}{red}, violets are \textcolor{myviolet}{violet}
  •  Tags:  
  • r hex
  • Related