Search code examples
rr-markdownpdflatex

R Markdown PDF and fancyhdr - how to add special characters and link to footer


I'm trying to create an R Markdown PDF output with a footer containing a logo on the left, another logo on the right, and some text in the center. The center text seems to be working fine when I don't have special characters. So I have two questions:

  1. How do I add special characters (e.g. dash, ampersand, and forward slash) to the footer?
  2. How do I add the website as a hyperlink?

Apologies if my code is only partially reproducible. I don't know how to add the images.


title: "PDF Outpuf File"
output: pdf_document
fontsize: 11pt
header-includes:
-   \usepackage{graphicx}
-   \usepackage{fancyhdr}
-   \pagestyle{fancy}
-   \fancyfoot[L]{\includegraphics[height=2cm]{images/logo1.png}}
-   \fancyfoot[C]{Data source: 2014-2016 data, A & B research center in the School of Something at the University of Someplace. For more information visit this link: www.google.com}
-   \fancyfoot[R]{\includegraphics[height=2cm]{images/logo2.png}}
-   \fancypagestyle{plain}{\pagestyle{fancy}}
---

Solution

  • I'm not entirely sure what you mean by special characters, but I'll try to answer as best I can.

    One word of caution is that your yaml header needs to be structured properly. So, I think that you need to add spaces (or possibly tabs) to the yaml header to ensure that it's parsed as you want it parsed.

    I created a second file, header.tex, with the following text:

    \usepackage{graphicx}
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyfoot[C]{Data source: 2014-2016 data, A \& B research center in the School of Something at the University of Someplace. For more information visit this link: \url{www.google.com}}
    \fancypagestyle{plain}{\pagestyle{fancy}}
    

    I then created a Rmd file with only a yaml header:

    ---
    title: "PDF Outpuf File"
    output: 
      pdf_document:
        includes:
          in_header: header.tex
        keep_tex: true
    ---
    

    Notice that I 'indent' by two spaces each level of the nested yaml field 'output'.

    RStudio has a helpful document here: https://rmarkdown.rstudio.com/pdf_document_format.html#overview

    If by "special character" you mean ampersand, note that Latex requires a "\" to escape the ampersand, because a regular "&" serves another purpose in Latex.

    I hope that this is helpful. If it's not helpful, please point out my misunderstanding.

    You can encode the url as a hyperlink by surrounding it like "\url{google.com}", without quotation marks, as in my example.