add image in title page of rmarkdown pdf - r

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.

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.

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

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

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}

Related

header on first page and others

a header is created for pages 2+ with fancyhdr but how can you make the same header also appear on the first page?
here is the rmarkdown:
---
title: "Untitled"
classoption: landscape
output:
pdf_document:
number_sections: false
dev: pdf
keep_tex: false
toc: yes
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[C]{center text}
- \fancyhead[R]{right text}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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 cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Include \thispagestyle{fancy} in the beginning of your document (e.g. just after the yaml header) to make the first page fancy.
Edit to address your comment
If you want to have different center text in the first page you can have use if when you define \fancyhead
\fancyhead[C]{\ifnum\value{page}>1 center text \else \fi}

R Markdown PDF Change Font & Color for Headers

I need to update the color and font of the header (only header) in a R Markdown PDF file. I have found recourses on how to do this for the whole document, but can't find an answer for changing the headers only.
Thank you kindly!
---
title: "Untitled"
output: pdf_document
---
Simplified version of the solution provided by Grada Gukovic:
You can add simple LaTeX statements to your document via the YAML header header-includes, e.g.:
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{sectsty}
- \allsectionsfont{\color{cyan}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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>.
Result:
This is most useful for small additions like the one seen here. If you want to add more than a few lines of LaTeX code it is often easier to save them to an external file, say preamble.tex and include that via
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
Other possible places are before_body and after_body, c.f. ?rmarkdown::includes.
There is no option to do this in rmarkdown::pdf_document. You can do this by modifying the .tex template being used using the sectsty package for latex.
For example the following changes the color of all headers to cyan:
Download the default latex template from here:
tex template
Open the template in Notepad and add the following lines on an appropriate place in the document preamble(I have them as lines nr. 200 and 201):
\usepackage{sectsty}
\allsectionsfont{\color{cyan}}
Save the modified file with the extension .tex (my file is called "Cyansections.tex") and put it in R's working directory.
Modify the header of the .rmd document:
---
title: "Untitled"
output:
pdf_document:
template: Cyansections.tex
---
If you want a different color or font consult this answer
and sectsty's manual Especially section 4 of the manual for chanhing fonts

Rmarkdown beamer: How to left justify the title page

How is it possible to align the title page of my Rmarkdown beamer presentation to the LEFT instead of the default center?
Example , default is center:
---
title: "Untitled"
author: "S SS"
date: "2/4/2018"
output: beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
This is an R Markdown presentation. 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 switch to a left aligned title page by using
\setbeamertemplate{title page}[default][left]
To use this in rmarkdown, place this line in a file called preamble.tex and use
---
title: "Untitled"
author: "S SS"
date: "2/4/2018"
output:
beamer_presentation:
includes:
in_header: preamble.tex
---
test
Edit:
with an up-to-date version of markdown, you can also directly do
---
title: "Untitled"
author: "S SS"
date: "2/4/2018"
output:
beamer_presentation
header-includes:
- \setbeamertemplate{title page}[default][left]
---
test
Here's a hack until someone comes along with a better answer: We'll dispense with a standard beamer title slide and just format the first slide as if it were a title slide:
---
output: beamer_presentation
header-includes:
- \usepackage{color}
---
\color{blue}
\LARGE{\textbf{This is the title}}
\color{black}
\vspace{0.2cm}
\large{Author Name}
\large{February 4, 2018}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
This is an R Markdown presentation. 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.

How to remove compact title from R markdown to latex conversion?

I wrote my own titlepage and it is loaded via an include in the R-markdown file. However, this conflicts with the pandoc title. I am trying to find settings in the R markdown yaml header such that pandoc does not insert the following code snipped into the tex-file.
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{}
\pretitle{\vspace{\droptitle}}
\posttitle{}
\author{}
\preauthor{}\postauthor{}
\date{}
\predate{}\postdate{}
There is no clear indication in the pandoc documents or the r markdown guidelines how to disable the title generation. Any help would be appreciated.
Update: In particular, I am looking for solutions that allow me to keep creating my title page with the \maketitle command. That is why I focussed on this particular code snipped that I want to get rid of.
I also use my own title page with rmarkdown documents for latex/pdf outputs. To remove the title, you can add the following command in a text file called with in_header :
\AtBeginDocument{\let\maketitle\relax}
A reproductible example with the header.tex file built directly within the Rmd document:
---
title: "RMarkdown No title Test"
author: "StatnMap"
date: "July 30, 2017"
output:
pdf_document:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r rm_title_page, echo=FALSE}
head <- cat('
\\AtBeginDocument{\\let\\maketitle\\relax}
', file = "header.tex")
```
# Title 1
**Some text**
# Title 2
**Some text**
Using compact-title: false in the YAML works.
---
title: "This title is not compact"
author: "Test"
date: "2019 May 10"
output: pdf_document
compact-title: false
---
I had the same problem today. Here's what I did. (Maybe I'll update the solution when I come up with something better.)
The solution is dumb but useful. I can't set an arbitrary space between the lines now, because I used \newline.
---
title: "\\huge My Smart Title"
author: "\\newline \\Large My Smart Author"
date: "\\newline \\Large 2018-12-25"
output:
pdf_document:
includes:
in_header: preamble.tex
latex_engine: xelatex
---
Below are the outputs before and after the solution.
BEFORE:
AFTER:
Note:
You may be confused about the different sizes of the "author" and the "date" in the two pictures above, if you don't know that the fontsize of the "author" and the "date" is \large instead of \Large by default.
END

How to add table of contents in Rmarkdown?

I am using RStudio for writing markdown documents and want to add Table of Contents (TOC) at top of the documents so that the user could click the relevant section for reading. There were some relevant examples on rpubs but now I can't seem to find them. Please note that I don't use pandoc and am quite new to Rmd & knitr. Is there any way to add TOCs without using pandoc? If using pandoc is must then which functions are relevant?
EDIT
Here's a small sample page:
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
Header 1
---------------
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>.
## Header 2
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)
```
### Header 3
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I tried running this in RStudio v 0.98.864 and it worked! but sadly it didn't work on 0.98.501 and 0.98.507. I am working on my thesis in 0.98.501 and after updating RStudio, some of my analyses didn't work. So, I reverted back to 0.98.501.
What should I do now? I really want TOCs but without harming the outputs of other analyses.
The syntax is
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.
Syntax with more options:
---
title: "Planets"
author: "Manoj Kumar"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
toc: true # table of content true
toc_depth: 3 # upto three depths of headings (specified by #, ## and ###)
number_sections: true ## if you want number sections at each table header
theme: united # many options for theme, this one is my favorite.
highlight: tango # specifies the syntax highlighting style
css: my.css # you can add your custom css, should be in same folder
---
If you are using pdf_document, you might want to add table of contents in a new page, which toc: true does not allow. It puts the table of contents right after the document title, author and date--because it is in yaml.
If you want to have it in a new page, you have to use some latex language. Here is what I did.
---
title: \vspace{3.5in}"Title"
author: "Name"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: true
number_sections: true
---
\newpage # adds new page after title
\tableofcontents # adds table of contents
\listoffigures
\listoftables
\newpage
So, after yaml (the chunk between ---), I added a new page using \newpage, then a table of contents using \tableofcontents, a list of figures using \listoffigures, a list of tables \listoftables, and a new page before everything else.
Note, \vspace{3in} in the title adds vertical space of 3 inch from the top before printing yaml (title, etc.).
Read more here: https://www.sharelatex.com/learn/Table_of_contents

Resources