Search code examples
rlatexr-markdownbookdown

Changing Chapter colors in r bookdown (pdf output)


In the regular bookdown format, every chapter is started with a bold:

Chapter 1

Title of chapter

I am trying to change the color of "Chapter 1" to a deep grey, instead of black. I am completely new to LaTeX, but have pieced some code together based on other Stackoverflow questions, to customize colors. I created a mystyles.sty file which consists of:

\usepackage{titlesec}
\usepackage{xcolor}

\definecolor{battleshipgrey}{rgb}{0.52, 0.52, 0.51}
\titleformat{\thechapter}
{\color{battleshipgrey}\normalfont\Large\bfseries}
{\color{battleshipgrey}\chapter}{1em}{}

My YAML header is:

title: "My Title"
author: "Me"
date: ""
output: pdf_document
bibliography: [bib.bib]
documentclass: book
geometry: left=4cm, right=3cm, top=2.5cm, bottom=2.5cm
link-citations: yes
classoption: openany
biblio-style: apalike
subparagraph: true

And I have an _output.yml with the following code:

bookdown::pdf_book:
  includes:
    in_header: mystyles.sty
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: no
mainfont: Bookman

What am I misspecifying in my mystyles.sty document? Currently, nothing is being changed in color. I have tried specifying:

\titleformat{\chapter}
{\color{battleshipgrey}\normalfont\Large\bfseries}
{\color{battleshipgrey}\thechapter}{1em}{}

But this colours the Chapter number and title grey, but the format of the Chapter title changes to:

1 Title of chapter


Solution

  • ekstroem put me on the right track to use the sectsty package. I tried using \chapternumberfont from the sectsty package, which didn't work. I think Bookdown doesn't set the name for the chapter number to this particular name. I found a workaround that worked, by using setting the entire chapter to grey, and then setting the chapter title to black:

    \usepackage{xcolor}
    \usepackage{sectsty}
    \definecolor{battleshipgrey}{rgb}{0.52, 0.52, 0.51}
    \chapterfont{\color{battleshipgrey}}
    \chaptertitlefont{\color{black}}