1

I have some custom color and I'd like the headers of fancyhdr to be colored differently.

How do you make the different headers \fancyhead[L], \fancyhead[R], \fancyhead[C] have different colorboxes and font colors? How do you make \fancyhead[R] have a different font size?

Thank you.

Here is what I currently have:

---
title: "Untitled"
classoption: landscape
output: 
  pdf_document:
    number_sections: false
    dev: pdf
    keep_tex: false
    toc: yes
header-includes:
- \usepackage{fancyhdr}
- \usepackage{etoolbox}
- \usepackage{color}
- \pagestyle{fancy}
- \definecolor{col1}{RGB}{7, 252, 3}
- \definecolor{col2}{RGB}{7, 200, 3}
- \fancyhead[L]{% 
    \colorbox{col1}\color{blue}{left}%
    }
- \fancyhead[R]{% 
    \colorbox{col2}\color{red}\fontsize{12?}{right}%
    }
- \fancyhead[C]{  %  \colorbox{col2}\color{white}\nouppercase{\leftmark} %   }
- \let\oldheadrule\headrule
- \renewcommand{\headrule}{\color{col2}\oldheadrule}
---




```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```
## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

\newpage

# Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

\newpage

# page 3

You can also embed plots, for example:

```{r pressured, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
1
  • \colorbox{<colour>}{<stuff>} takes two arguments. The first is the <colour>, while the second is the <stuff> contained within the box. Using

    - \fancyhead[L]{% 
        \colorbox{col1}\color{blue}{left}%
      }
    

    mistakenly puts \color as the second argument to \colorbox, which doesn't make sense. You need to use something like

    - \fancyhead[L]{\colorbox{col1}{\color{blue}left}}
    

    Note also the use of a single line rather than breaking a line; writing header content (via header-includes) in Markdown doesn't translate in the same way you'd think.

  • To change the font size, use \fontsize{<size>}{<baseline skip>}\selectfont. In your case, something like \fontsize{12}{14}\selectfont would work.

You may get some warning about the header height being too small. For that, add

- \setlength{\headheigh}{<height>}

to match your needs (where <height> is at least as large as the suggested height).

  • Thank you. What would be the order for the font size and colorbox and color: - \fancyhead[C]{\colorbox{col2}{\color{col1}\fontsize{12}{14}\selectfont\nouppercase{\leftmark}} that gives an error \begin{document} – user3022875 Sep 18 '19 at 0:09
  • @user3022875: That order and bracketing seems fine. Do you have all the other header elements in a single line? – Werner Sep 18 '19 at 0:15
  • Yes it is all in a single line. – user3022875 Sep 18 '19 at 0:16
  • 1
    @user3022875: You need a closing brace. – Werner Sep 18 '19 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.