2015-04-01 22 views
9

我正在嘗試創建一個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)放在我的標題上面,那會很棒。

+0

這是否幫助? http://stackoverflow.com/questions/25890939/inserting-logo-into-beamer-presentation-using-r-markdown – tonytonov 2015-04-01 12:56:41

+0

不是我試圖複製它。我正在嘗試PDF而不是Beamer Presentation,所以我不太清楚如何調整代碼以適應。將有一個搜索,看看我是否可以找到PDF類似的東西。 – Dan 2015-04-07 09:33:07

+0

原來我需要重新排列YAML語句,將'includes'放在'pdf_document:'行後面。所以我現在在.tex文件中有'in_header:mystyle.tex'和'{\ includegraphics {tdclogo.png}}'。這會拋出一個錯誤'! LaTeX錯誤:缺少\ begin {document}。即使我把'\ begin {document}'放在那裏,我也會得到更多的錯誤信息。有什麼想法嗎? – Dan 2015-04-07 10:28:05

回答

9

我能解決這個使用乳膠包所有權

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

+0

我得到'!未定義的控制順序。 l.35 \ if @ titlepage'當編織PDF時從Rmd – akhmed 2016-01-19 02:34:49

+1

同樣的錯誤,但改變了'in_header.sty'合併這個解決方案http://tex.stackexchange.com/questions/61051/logo-in-the-first-僅限頁面只需添加'\ logo {\ includegraphics {path}}' – animalito 2017-02-09 19:22:14

5

基於上述解決方案,以下代碼不需要輔助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. 
0

對於投影儀演示,你可以做這樣的:

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" 

的titlegraphic將被置於下方的標題文字

相關問題