28

I am getting started with R markdown and I would like to create a new report having our company image logo.png in the upper right corner of each page.

Is there a way to code this in the YAML section or need this to be done in a R chunk section?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Daniel_D
  • 698
  • 1
  • 7
  • 13

5 Answers5

30

You can use the includes option in the yaml to specify a custom addition to your latex header. The yaml part would look like

---
output: 
    pdf_document:
      keep_tex: true
      includes:
          in_header: header.tex
---

and you need to save a separate file called header.tex with the following defining your company logo like so:

\usepackage{fancyhdr}
\pagestyle{fancy}
\rhead{\includegraphics[width = .05\textwidth]{logo.png}}

Here I used the fancyhdr latex package to add the logo, but there are other potential solutions. See here for more options.

tmpname12345
  • 2,891
  • 18
  • 20
  • Where do I need to save the header.tex file? In the same directory where my rmd. report is saved? – Daniel_D Jan 16 '15 at 14:54
  • 1
    Suppose the path where my logo is stored under: Y:\Risikomanagement\Stress Tests. How should your header.tex code look like? – Daniel_D Jan 16 '15 at 14:58
  • I think you can give it the full path to the logo: `\rhead{\includegraphics[width = .05\textwidth]{Y:\Risikomanagement\Stress Tests\logo.png}}` – tmpname12345 Jan 16 '15 at 15:03
  • 5
    I'd recommend you not to use the full path, especially under Windows. Put everything in the same directory, and always use relative paths everywhere. – Yihui Xie Jan 17 '15 at 06:47
28

Ok, I have found the solution:

---
title:
header-includes: 
   \usepackage{graphicx}
   \usepackage{fancyhdr}
   \pagestyle{fancy}
   \setlength\headheight{28pt}
   \fancyhead[L]{\includegraphics[width=5cm]{GPIM_Logo_300x85.png}}
   \fancyfoot[LE,RO]{GPIM}
output: pdf_document
---
Daniel_D
  • 698
  • 1
  • 7
  • 13
  • 4
    this looks really nice, but it doesn't add the image to the first page for me; anyone know how to force that? – ClaytonJY Feb 24 '17 at 16:26
  • 8
    `header-includes: \usepackage{fancyhdr} \usepackage{graphicx} \usepackage{eurosym} \usepackage{booktabs,xcolor} \pagestyle{fancy} \fancyhf{} \addtolength{\headheight}{1.0cm} \rhead{MainSky Stress Test Report - \today} \lhead{\includegraphics[width=6cm]{path & logo}} \rfoot{Page \thepage} \fancypagestyle{plain}{\pagestyle{fancy}} output: pdf_document` – Daniel_D Feb 27 '17 at 08:35
  • @Daniel_D, I added logo with this method successfully. But today I failed adding logo to the same pdf file I generated previously. Do you have any idea how to solve this issue? – HW-Scientist Apr 10 '18 at 09:19
  • @Daniel_D this is veryhelpful. How would you center the logo? – B. Davis Jan 26 '21 at 22:01
25

I've tried many solutions presented here and in other forums, none of which worked. I've finally came to a solution that worked for me.

---
title: 'Fancy Title Here'
author: "Diego"
date: "today"
output:
  pdf_document:
    toc: yes
header-includes:
    - \usepackage{fancyhdr}
---
\addtolength{\headheight}{1.0cm} % make more space for the header
\pagestyle{fancyplain} % use fancy for all pages except chapter start
\rhead{\includegraphics[height=1.2cm]{C:/Path/to/logo/logo}} % right logo
\renewcommand{\headrulewidth}{0pt} % remove rule below header

I hope that helps someone the same way it helped me.

Diego
  • 251
  • 3
  • 2
  • 2
    Tried this and the logo appears but it appears on each page. I need it only on the first page – Shivi Bhatia Nov 06 '17 at 13:09
  • @Diego, this really helps me a lot, thank you very much. I've been stuck in the logo issue for several days after my previous logo solution became invalid, which I still have no idea why. And finally with your solution I got the logo back! – HW-Scientist Apr 12 '18 at 08:46
  • @Diego: This is really nice solution, but how can I refer to the image with a relative instead of full path? Say I have the image stored in a folder `figs` located in the same folder where the `.Rmd` file is stored. How can I refer to it without adding the full path to the image file in `\includegraphics`? – panman May 01 '18 at 12:36
2

For those using flexdashboardsee this addition to entry preamble text for logos and favicon, although its upper left not right:

http://rmarkdown.rstudio.com/flexdashboard/using.html#logo__favicon

so your start of the .Rmd file looks like this:

---
title: "myappR"
output:
  flexdashboard::flex_dashboard:
    logo: mylogo.png
    favicon: mylogo.png
    theme: bootstrap
runtime: shiny
---

I left my logo in the root directory with a simple name. And:

  • Kept logo height 48 pixels as this plays nicely with the theme,
  • Be careful of spaces and indents and,
  • Do not forget the ending : after flexdashboard.
micstr
  • 5,080
  • 8
  • 48
  • 76
  • How do you adjust the size of this logo? I have one placed using this method, but it's very big and it isn't displaying very well :( – Liliana Pacheco Dec 26 '18 at 18:20
  • 1
    I used a graphics package and resized the image to be 48 pixels high. Take care when scaling so the width also changes proportionately. I used irfanview but you might have others available. – micstr Jan 14 '19 at 11:55
2

I have found a solution to make a logo only on the first page:

\addtolength{\headheight}{1.0cm} % make more space for the header
\fancypagestyle{plain}{} % this is to remove the default plain style for the first page
\thispagestyle{fancy} % use fancy for all pages except chapter start
\fancyhead[L]{\includegraphics[width = 100pt]{ManchesterLogo.png}}
\renewcommand{\headrulewidth}{0pt} % remove rule below header