Search code examples
rlatexr-markdownrstudio

Rmarkdown table of contents changes font color


I am trying to use custom colors for my table of contents on a mixed latex R markdown document. I set the table of contents to be webblue color and want my sections and subsections to be a different font color, however, when I change the color to 'niceblue', the entry in the table of contents also change to 'niceblue'. Question: How can I change the colors of my documents sections without altering the color on table of contents? Thanks for any help. See code that I am using below:

---
title: "Age Assignment"
author: "joe"
date: "`r Sys.Date()`"
output:
  pdf_document: default
header-includes:
- \geometry{top=1.3cm, bottom=1.0cm, left=1.8cm, right=1.8cm,footskip=.5cm}
- \usepackage{hyperref}
- \hypersetup{colorlinks = true, linkcolor = webblue, urlcolor = red}
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
- \usepackage{longtable,ctable}
- \usepackage{url}
- \usepackage{relsize}
- \usepackage[compact]{titlesec}%use 'compact' to shrink the whitespace on sections,sub,subsub
- \hypersetup{pdfstartview={XYZ null null 0.75}}%0.75 prints pdf at 75% zoom level
- \usepackage{setspace}
- \newcommand{\subsubsubsection}[1]{\paragraph{#1}\mbox{}\par\vspace{1mm}} %to add a subsubsubsection
- \setcounter{secnumdepth}{4}               %to add a subsubsubsection
- \setcounter{tocdepth}{4}                  %to add a subsubsubsection
- \titlespacing\section{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}%Sections
- \titlespacing\subsection{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}%subsections
- \titlespacing\subsubsection{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}%subsub
- \definecolor{webblue}{rgb}{0, 0, 0.5} % less intense blue
- \definecolor{bole}{rgb}{0.47, 0.27, 0.23}
- \definecolor{brickred}{rgb}{0.8, 0.25, 0.33}
- \definecolor{niceblue}{rgb}{0.0, 0.53, 0.74}
subtitle: Assessment of populations
number_sections: yes
toc: yes
---

```{r setup, include=FALSE} 
knitr::opts_chunk$set(warning = FALSE, message = FALSE,comment="") 
```
\color{bole}The purpose of this document is to......\
\color{black}

# Beginning of document

## This is a test

## And this is another test...

## \color{niceblue}Introduction
\color{black}
The Central Valley blah blah\par

### \color{niceblue} Methods
\color{black}
And this is what happened......

enter image description here


Solution

  • If you use real latex sectioning commands, you can specify both the title as it should appear in text as well as the title for the toc:

    ---
    title: "Age Assignment"
    author: "joe"
    date: "`r Sys.Date()`"
    output:
      pdf_document: 
        keep_tex: true
    header-includes:
    - \geometry{top=1.3cm, bottom=1.0cm, left=1.8cm, right=1.8cm,footskip=.5cm}
    - \usepackage{hyperref}
    - \hypersetup{colorlinks = true, linkcolor = webblue, urlcolor = red}
    - \usepackage{pdflscape}
    - \newcommand{\blandscape}{\begin{landscape}}
    - \newcommand{\elandscape}{\end{landscape}}
    - \usepackage{longtable,ctable}
    - \usepackage{url}
    - \usepackage{relsize}
    - \usepackage[compact]{titlesec}%use 'compact' to shrink the whitespace on sections,sub,subsub
    - \hypersetup{pdfstartview={XYZ null null 0.75}}%0.75 prints pdf at 75% zoom level
    - \usepackage{setspace}
    - \newcommand{\subsubsubsection}[1]{\paragraph{#1}\mbox{}\par\vspace{1mm}} %to add a subsubsubsection
    - \setcounter{secnumdepth}{4}               %to add a subsubsubsection
    - \setcounter{tocdepth}{4}                  %to add a subsubsubsection
    - \titlespacing\section{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}%Sections
    - \titlespacing\subsection{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}%subsections
    - \titlespacing\subsubsection{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt}%subsub
    - \definecolor{webblue}{rgb}{0, 0, 0.5} % less intense blue
    - \definecolor{bole}{rgb}{0.47, 0.27, 0.23}
    - \definecolor{brickred}{rgb}{0.8, 0.25, 0.33}
    - \definecolor{niceblue}{rgb}{0.0, 0.53, 0.74}
    subtitle: Assessment of populations
    number_sections: yes
    toc: yes
    ---
    
    ```{r setup, include=FALSE} 
    knitr::opts_chunk$set(warning = FALSE, message = FALSE,comment="") 
    ```
    \color{bole}The purpose of this document is to......\
    \color{black}
    
    # Beginning of document
    
    ## This is a test
    
    ## And this is another test...
    
    \subsection[Introduction]{\color{niceblue}Introduction}
    \color{black}
    The Central Valley blah blah\par
    
    \subsubsection[Methods]{\color{niceblue} Methods}
    \color{black}
    And this is what happened......
    

    enter image description here