add image in title page of rmarkdown pdf

2020-02-04 20:24发布

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.

3条回答
够拽才男人
2楼-- · 2020-02-04 20:58

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.
查看更多
迷人小祖宗
3楼-- · 2020-02-04 21:12

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

查看更多
萌系小妹纸
4楼-- · 2020-02-04 21:16

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

查看更多
登录 后发表回答