markdownr-markdown

How to set colour for toc links in R markdown document?


I'm trying to create a R markdown document and use custom colours for the links in the document. This works for external links, but not for the ones in the table of contents. It was my understanding that the linkcolor attribute should take care of that, but it doesn't work for me.

Here's my workflow:

Document:

---
title: "test"
output:
    pdf_document:
      toc: true
linkcolor: red
urlcolor: red
citecolor: red
---

# Foo

## Fooo

foo foo foo [link](https://duckduckgo.com)

# Bar

## Bar Bar

bar bar bar

To render, I'm using rmarkdown::render in R. In Unix systems, you can just run this line (provided you saved the given example as doc.Rmd):

echo "rmarkdown::render('doc.Rmd', output_file='doc.pdf')" | R

Solution

  • Figured it out.

    Using the hyperref package allows all sorts of link customisation.

    Example header:

    ---
    title: "test"
    output:
        pdf_document:
          toc: true
    header-includes:
        - \usepackage {hyperref}
        - \hypersetup {colorlinks = true, linkcolor = red, urlcolor = red}
    ---