22

I am attempting to create an rmarkdown document. I have finally figured out a way to approach this, although it has taken quite some time. The last thing I would like to be able to do is to add an image to the title page of my pdf document.

The trouble I have is that my title page is defined by the top section of YAML. Below is the contents of my example.Rmd file. I use the Knit PDF button in RStudio to turn it into a PDF.

---
title: "This is a my document"
author: "Prepared by: Dan Wilson"
date: '`r paste("Date:",Sys.Date())`'
mainfont: Roboto Light
fontsize: 12pt
documentclass: report
output: 
  pdf_document:
    latex_engine: xelatex
    highlight: tango
---
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}
summary(cars)
```

You can also embed plots, for example:

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

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

If anyone has some tips that would allow me to put an image (logo.png) above my title that would be great.

Dan
  • 2,625
  • 5
  • 27
  • 42
  • Does this help? http://stackoverflow.com/questions/25890939/inserting-logo-into-beamer-presentation-using-r-markdown – tonytonov Apr 01 '15 at 12:56
  • not with my attempt at replicating it. I am attempting a PDF rather than Beamer Presentation so am not too clear how to adjust the code to suit. Will have a search to see if I can find a similar thing for PDF. – Dan Apr 07 '15 at 09:33
  • Turns out I needed to reorder the YAML statement, putting the `includes` at straight after the `pdf_document:` line. So I now have `in_header: mystyle.tex` with `{\includegraphics{tdclogo.png}}` in the .tex file. This throws an error `! LaTeX Error: Missing \begin{document}.` Even if I put the `\begin{document}` in there I get more error messages. Any thoughts? – Dan Apr 07 '15 at 10:28
  • I suppose you'll have to define your own template. See here: http://rmarkdown.rstudio.com/developer_document_templates.html. If you are familiar with latex, I suggest digging into https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/latex/default.tex, put your image manually before `\begin{document}`. – tonytonov Apr 07 '15 at 12:40
  • Were you able to solve this? – Megatron Sep 04 '15 at 13:48
  • 1
    I was able to solve it. I am no guru in Markdown or Latex, so my solution may be somewhat ugly, however it does work. I'll post in the next day or two. If I forget just post a reminder. – Dan Sep 06 '15 at 22:19
  • 1
    @Dan Hi Dan, I'm writing the my report using Rmarkdown now, and facing the same problem? Could you post how did you solve the problem? Thanks, I really appreciate – Christopher B. L Oct 02 '15 at 09:44
  • I've just recently created an `R` package that addresses this issue specifically for me. It was a tedious process getting my head around some of the LaTeX stuff, but feel free to fork and make use of it. https://github.com/thedatacollective/templatermd – Dan Nov 02 '17 at 05:19
  • @Dan, So after installing your template rmd, the usage would be something like as described here?: https://github.com/danwwilson/templatermd/blob/master/inst/rmarkdown/templates/tdc_pdf/skeleton/skeleton.Rmd – Mark Neal Mar 14 '20 at 10:13

4 Answers4

25

Based on the previous solution, the following code does not require an auxiliary header.tex file. All contents are contained in the .Rmd file. The LaTeX commands are instead defined in a header-includes block in the YAML header. More info can be found here.

Replace my_graphic.png below with your local graphic file.

---
title: "A title page image should be above me"
header-includes:
- \usepackage{titling}
- \pretitle{\begin{center}\LARGE\includegraphics[width=12cm]{my_graphic.png}\\[\bigskipamount]}
- \posttitle{\end{center}}
output: 
  pdf_document:
    toc: true
---

\newpage

# Section 1

Some text.
Megatron
  • 15,909
  • 12
  • 89
  • 97
21

I was able to solve this using LaTeX package titling

---
title: "Untitled"
author: "Name"
date: "September 19, 2015"
output:
  pdf_document:
    includes:
      in_header: header.tex
---

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}
summary(cars)
```

You can also embed plots, for example:

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

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

Where the header.tex should include the following code:

\usepackage{titling}

\pretitle{%
  \begin{center}
  \LARGE
  \includegraphics[width=4cm,height=6cm]{logo.png}\\[\bigskipamount]
}
\posttitle{\end{center}}

and replace logo.png with the image you would like to use and make sure the file is in the root directory of your Rmd file. You can change image width and height to your needs. For more information on available options go to titling

user3498523
  • 370
  • 4
  • 10
  • I get `! Undefined control sequence. l.35 \if@titlepage` when knitting PDF from Rmd – akhmed Jan 19 '16 at 02:34
  • 1
    Same error but changed the `in_header.sty` to incorporate this solution http://tex.stackexchange.com/questions/61051/logo-in-the-first-page-only Just add the `\logo{\includegraphics{path}}` – animalito Feb 09 '17 at 19:22
  • @user3498523 Is there any way to adjust the position of the logo to be between the title and table of contents using your solution? – Tanga94 Jul 18 '20 at 17:21
8

For a beamer presentation you can do it like this:

title: "Title"
subtitle: "Subtitle"
author: "author"
date: "date"
header-includes:
- \titlegraphic{\centering \includegraphics[width=12cm]{titlepic.png}}
output: 
  beamer_presentation:
    latex_engine: xelatex
    theme: "metropolis"
    highlight: "espresso"
classoption: "aspectratio=169"

The titlegraphic will be placed below your title text

loki
  • 9,816
  • 7
  • 56
  • 82
0

For beamer presentation if you want an image at the bottom you can kind of cheat and add the image where the date line should be. Then if you want to insert date you can add institution (which is before date). the ![] should be tabbed (4 spaces from the far left of the page)

date: |
    ![](mypathtofile/myimage.png){width=3in}
Isaac Zhao
  • 379
  • 2
  • 10