Julien Massardier
Julien Massardier

Reputation: 1466

change the title color Rmd file

I am trying to change the color of the title in an Rmd file that generates a Pdf

I tried this (with and without quotes):

---
title: "\textcolor{blue}{This title is blue}"
output:
  pdf_document:
  latex_engine: xelatex
---

# 1. TITLE 1

## 1.1 Subtitle

which gives this kind of error:

Error: Failed to compile test_font.tex.`

And then I tried this:

---
title: <div class="blue">This title is blue</div>
output:
  pdf_document:
  latex_engine: xelatex
---

# 1. TITLE 1

## 1.1 Subtitle

which doesn't change the color

Any advice? Thanks!

Upvotes: 3

Views: 6740

Answers (3)

Hakuna Patata
Hakuna Patata

Reputation: 61

The easiest way that I've found is to use the <span> html tag. Example below.

---
title: <span style="color:blue">R Notes</span>
output: html_document
---

Hello world!
```{r Hello world}
"hello world"
```

Upvotes: 2

Max Feinberg
Max Feinberg

Reputation: 840

I found a way to change the color of titles of PDFs compiled from RMarkdown files by doing the following. This process does not require any manual modification of intermediate TeX files.

I have a header file that contains LaTeX code to import the color package and define some custom colors, it is named header.tex.

\usepackage{color}
\definecolor{NavyBlue}{RGB}{0,112,192}

The YAML section of my RMarkdown file contains the following lines in the ouput section.

output: 
  pdf_document:
    includes:
      in_header: header.tex
    keep_tex: true
    toc: false

And the title line is the following.

title: \textcolor{NavyBlue}{`r paste0('Report - ', params$Location)`}

The resulting title looks like this.

If you are using TinyTeX you should not need to worry about downloading any packages.

Upvotes: 2

duckmayr
duckmayr

Reputation: 16920

I doubt there is a way to make this work out of the box (after some searching, I was unable to find one, though happy to be proven wrong). I can explain for you though why this doesn't work.

First, to use \textcolor{}{}, you need the color package. So trying to compiling without using that package will fail every time.

So, you may say (naively), let's include the color package:

---
output: pdf_document
header-includes:
   - \usepackage{color}
title: \textcolor{blue}{This text is blue}
---

# 1. TITLE 1

## 1.1 Subtitle

However, you still get a compilation error. So, I examine the .tex file produced, and see the following:

... [output omitted]

\setlength{\droptitle}{-2em}

  \title{\textcolor{blue}{This text is blue}}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{}
    \preauthor{}\postauthor{}
    \date{}
    \predate{}\postdate{}

\usepackage{color}
... [output omitted]

R Markdown will put the header includes in after the title info, which will again, never work. You can manually edit it to the following:

... [output omitted]

\usepackage{color} % Include *before* using \textcolor{}{}

\setlength{\droptitle}{-2em}

  \title{\textcolor{blue}{This text is blue}}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{}
    \preauthor{}\postauthor{}
    \date{}
    \predate{}\postdate{}
... [output omitted]

to successfully compile your document:

enter image description here

but that seems unnecessarily complicated for an R Markdown document, which is supposed to make your life easier.

I might suggest filing an issue at https://github.com/rstudio/rmarkdown , see if that might be behavior they change for future users.

Upvotes: 1

Related Questions