【问题标题】:add image in title page of rmarkdown pdf在 rmarkdown pdf 的标题页中添加图像
【发布时间】:2015-06-06 00:04:28
【问题描述】:

我正在尝试创建一个 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),那就太好了。

【问题讨论】:

  • 不是因为我试图复制它。我正在尝试 PDF 而不是 Beamer 演示文稿,所以不太清楚如何调整代码以适应。将进行搜索以查看是否可以找到 PDF 的类似内容。
  • 原来我需要重新排序 YAML 语句,将 includes 放在 pdf_document: 行之后。所以我现在在.tex 文件中有in_header: mystyle.tex{\includegraphics{tdclogo.png}}。这会引发错误! LaTeX Error: Missing \begin{document}. 即使我将\begin{document} 放在那里,我也会收到更多错误消息。有什么想法吗?
  • 我能够解决它。我不是 Markdown 或 Latex 的专家,所以我的解决方案可能有点难看,但它确实有效。我会在接下来的一两天内发布。如果我忘记了,请发布提醒。
  • @Dan Hi Dan,我现在正在使用 Rmarkdown 编写报告,并且面临同样的问题?你能发布你是如何解决这个问题的吗?谢谢,非常感谢

标签: r pdf rstudio r-markdown


【解决方案1】:

基于之前的解决方案,下面的代码不需要辅助header.tex文件。所有内容都包含在.Rmd 文件中。 LaTeX 命令改为在 YAML 标头中的 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.

【讨论】:

    【解决方案2】:

    我能够使用 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

    【讨论】:

    • 从 Rmd 编​​织 PDF 时收到 ! Undefined control sequence. l.35 \if@titlepage
    • 同样的错误,但更改了in_header.sty 以合并此解决方案tex.stackexchange.com/questions/61051/… 只需添加\logo{\includegraphics{path}}
    • @user3498523 有没有办法使用您的解决方案将徽标的位置调整到标题和目录之间?
    【解决方案3】:

    对于 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"
    

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

    【讨论】:

    • 这对我有用,谢谢。我唯一需要改变的是删除titlepic.png之前的反斜杠。
    • 谢谢,我删除了。
    猜你喜欢
    • 2020-02-07
    • 2015-12-05
    • 1970-01-01
    • 2019-09-08
    • 2017-08-25
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多