在 rmarkdown pdf 的标题页中添加图像

add image in title page of rmarkdown pdf

我正在尝试创建 rmarkdown 文档。我终于找到了解决这个问题的方法,尽管这花了很长时间。我最不想做的事情是将图像添加到我的 pdf 文档的标题页。

我遇到的麻烦是我的标题页是由 YAML 的顶部定义的。以下是我的 example.Rmd 文件的内容。我使用 RStudio 中的 Knit PDF 按钮将其转换为 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.

如果有人有一些提示可以让我在我的标题上方放一张图片 (logo.png) 那就太好了。

我使用 LaTeX 包标题

解决了这个问题
---
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.

其中 header.tex 应包含以下代码:

\usepackage{titling}

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

并将 logo.png 替换为您要使用的图像,并确保该文件位于 Rmd 文件的根目录中。您可以根据需要更改图像的宽度和高度。有关可用选项的更多信息,请转到 titling

基于前面的方案,下面的代码不需要辅助header.tex文件。所有内容都包含在 .Rmd 文件中。 LaTeX 命令是在 YAML header 的 header-includes 块中定义的。可以找到更多信息 here.

将下面的 my_graphic.png 替换为您的本地图形文件。

---
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.

对于 beamer 演示文稿,您可以这样做:

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"

标题图片将放置在您的标题文本下方