How to produce a PDF title page from an R Markdown document - r

I would like to produce a custom title page when I knit my R Markdown document to pdf.
Here are the contents of my R Markdown document:
---
output:
pdf_document:
template: template.tex
---
# abstract
this is just some text
And here are the contents of template.tex:
\begin{document}
\maketitle
\end{document}
When I knit to pdf none of the R Markdown text appears. Only the template does.
Could anyone explain how I could type in R Markdown after using a latex template?

Your R Markdown document seems correct. The issue lies in your template.tex document.
This page shows a simple template.tex example:
\documentclass{article}
\begin{document}
$body$
\end{document}
The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.
However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.
I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.
\documentclass[]{report} % use report class because it contains a title page
\usepackage{hyperref} % load the hyperref package to avoid an undefined
% control sequence error
\title{$title$} % set title to what you passed from the yaml in the R
% Markdown document
\author{} % set author to empty to avoid warning message about
% no author set; use \author{$author$} if you want to
% set author from the yaml like `author: "My Name"`
\date{} % set date to empty to avoid a date on the title page;
% use \date{$date$} to set from yaml `date: 2021-01-08`
\begin{document}
\maketitle
$body$
\end{document}
But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.
I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).
In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:
---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text

Related

RMarkdown with .tex template overriding content

For my RMarkdown document, I would like to use a given Latex template.
Specifically: I try to knit my document to the EMNLP conference template (downloadable zip here).
My mini example below sits in that unzipped folder (containing the .sty and .tex files), so all dependencies work.
---
title: "my sample paper"
author: "A BCD"
date: 'Version: `r format(Sys.time(), "%d %B, %Y")`'
output:
pdf_document:
template: emnlp2021.tex
keep_tex: yes
abstract: "this is an abstract this is an abstract this is an abstract this is an abstract"
---
The body of the paper. The body of the paper. The body of the paper. The body of the paper.
But when knitted, it results in the default pdf that contains the conference instructions (i.e., my RMarkdown content is not shown).
The instructions in the rmarkdown book are great to point it to the .tex but I couldn't find the answer there.
What am I doing wrong?
The template is not a pandoc template. It does not contain the special placeholders that pandoc uses to mark the places where it should actually insert the contents. The EMNLP template is for human processing.
You can take a look at the default latex pandoc template like this:
pandoc -D latex
For example, the placeholder $abstract$ is where the abstract will be placed. Simply replace the fragment after \begin{abstract} in emnlp2021.tex by $abstract$ and compile your Rmarkdown again: the abstract should now be in place. You will have to adapt all other fragments of the template as well!

How to add text in R Markdown when using a LaTeX template? [duplicate]

I would like to produce a custom title page when I knit my R Markdown document to pdf.
Here are the contents of my R Markdown document:
---
output:
pdf_document:
template: template.tex
---
# abstract
this is just some text
And here are the contents of template.tex:
\begin{document}
\maketitle
\end{document}
When I knit to pdf none of the R Markdown text appears. Only the template does.
Could anyone explain how I could type in R Markdown after using a latex template?
Your R Markdown document seems correct. The issue lies in your template.tex document.
This page shows a simple template.tex example:
\documentclass{article}
\begin{document}
$body$
\end{document}
The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.
However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.
I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.
\documentclass[]{report} % use report class because it contains a title page
\usepackage{hyperref} % load the hyperref package to avoid an undefined
% control sequence error
\title{$title$} % set title to what you passed from the yaml in the R
% Markdown document
\author{} % set author to empty to avoid warning message about
% no author set; use \author{$author$} if you want to
% set author from the yaml like `author: "My Name"`
\date{} % set date to empty to avoid a date on the title page;
% use \date{$date$} to set from yaml `date: 2021-01-08`
\begin{document}
\maketitle
$body$
\end{document}
But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.
I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).
In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:
---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text

Render latex text from within r function in rmarkdown?

I am working within RStudio 1.1.383 using rmarkdown. I am trying to render to Latex a string that is created within a knitr chunk so that it appears as latex code within the document.
So far I have experimented with the 'results=' options in the chunk header and find that results='asis' enables it to be rendered once the document is knitted, but I have not been able to find a way to enable the result to be rendered using the preview feature that allows you to run a single chunk and see the results within the .Rmd editor.
Any help on this matter would be appreciated.
A minimal example is included below that should be copied into a .Rmd file before rendering.
Thanks in advance,
Michael
---
title: "Minimal Example"
output: html_document
---
```{r}
str <- "$$\\alpha \\cdot \\beta = \\delta$$"
# this doesn't show in the preview 'run chunk feature'
cat(str, "\n")
# neither does this
writeLines(str)
```

Add sections to beamer presentation using rmarkdown and knitr

I am trying to add section slides to a beamer presentation written in rmarkdown using the latex command \section{}. However, it gets inserted between a \begin{frame} & \end{frame} automatically during the conversion, which causes the compilation to fail. Is there any way to stop this happening so that the section slide can be added without having to manually edit the tex file?
Here is my rmarkdown code:
---
title: "Beamer presentation"
output: beamer_presentation
---
\section{Section one}
which gets converted to:
\title{Beamer presentation}
\begin{document}
\frame{\titlepage}
\begin{frame}
\section{Section one}
\end{frame}
\end{document}
Slides and section slides are both defined by markdown headings, a series of # character at the beggining of a line, the number of # indicating the hierarchical level of the title.
By default [the level that defines frames] is the highest header level in the
hierarchy that is followed immediately by content, and not another
header, somewhere in the document.
All title of higher level than this one will become section titles.
From the rmarkdown documentation ; See also the pandoc documentation on slideshows.
For instance :
# Section title
## Frame title
Frame content
### Subtitle inside a frame

Add beamer frame options in knitr/rmarkdown

I'm trying to add frame numbers to my Beamer presentation written in rmarkdown. However, I would like to suppress the numbers on the title page using the \begin{frame}[plain] option (from the second answer here: https://tex.stackexchange.com/questions/82794/removing-page-number-from-title-frame-without-changing-the-theme). However, when compiling from rmarkdown to tex, the \titlepage already creates a frame environment, so in effect I get a double frame and thus an error.
So when compiling this:
---
output:
beamer_presentation:
includes:
in_header: header.tex
---
\begin{frame}[plain]
\titlepage
\end{frame}
I get this in latex:
\begin{frame{
\begin{frame}
\titlepage
\end{frame}
\end{frame}
In the header.tex I have this:
\let\otp\titlepage
\renewcommand{\titlepage}{\otp\addtocounter{framenumber}{-1}}
So my workaround now is to just use a plain \maketitle in rmarkdown, then compile to .tex, add the [plain] option, then compile to pdf. However, I would like to avoid that intermediate step. Is this possible in rmarkdown?
rmarkdown uses pandoc to convert a Rmd file to a pdf via beamer/latex. pandoc uses templates to control how the conversion goes.
One way to deal with your problem is to :
Download the default beamer template rmarkdown uses and open it.
Change line 137 from this :
\frame{\titlepage}
To this :
\frame[plain]{\titlepage}
Add the path to your modified template in your Rmd file :
---
output:
beamer_presentation:
includes:
in_header: header.tex
template:/path/to/new/template.tex
---
Note that you need to specify the whole path, or store the template where pandoc can find it (~/.pandoc/templates on a linux machine)
Add {.plain} after the title as in:
----
# I'm the title {.plain}
Source: Pandoc User’s Guide

Resources