Add beamer frame options in knitr/rmarkdown - r

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

Related

Rmarkdown beamer_presentation: "missing \item" LaTex error if an R chunk is included and fancybox used

I want to use the LaTeX \ovalbox{} command from the fancybox package in an Rmarkdown beamer_presentation, knitted from within RStudio. However, as soon as I add a simple R chunk, I get an "! LaTeX Error: Something's wrong--perhaps a missing \item."
This is a minimum reproducible example:
---
output:
beamer_presentation
header-includes: |
\usepackage{fancybox}
---
## Slide 1
\ovalbox{an oval box}
```{r}
print("x")
```
I can hand-solve the problem afterwards in the .tex file by moving the \usepackage{fancybox} command before the \usepackage{fancyvrb} command which is automatically inserted during the knitting.
Any suggestions how to avoid this problem in the first place?
Seems the packages are fighting over how to mess with verbatim environments.
Normally one could simply load them in a different order, but of course rmarkdown makes such a simple task a lot more complicate. You can make your document compile using this hack (don't try to use any of the verbatim commands from fancybox, this might explode...):
---
output:
beamer_presentation
header-includes: |
\let\VerbatimEnvironmentsave\VerbatimEnvironment
\usepackage{fancybox}
\let\VerbatimEnvironment\VerbatimEnvironmentsave
---
## Slide 1
\ovalbox{an oval box}
```{r}
print("x")
```

How to include all bibliography in a specific slide in rmarkdown to beamer pdf

I am using a .Rmd file to generate a beamer PDF presentation (which, therefore, accepts LaTeX code).
In the standard R Markdown, the reference goes into the last slide. But I want to specify it in a general slide (or specifically, before the last slide).
This is what I have tried so far:
---
title: "Multivariate Regression Model for Count Data"
output:
beamer_presentation:
includes:
in_header: /home/guilherme/GoogleDrive/Analises/Modelo/extra_settings_2.tex
editor_options:
chunk_output_type: console
bibliography: /home/guilherme/GoogleDrive/Analises/Modelo/referencias_so.bib
csl: /home/guilherme/GoogleDrive/Analises/Modelo/stilo_biblio_so.csl
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(tinytex.verbose = TRUE)
```
## Ref
[#tmb]
[#r]
[#glmmTMB]
[#compoisson]
## Bibliography
\vspace{.5mm}
\setlength{\parskip}{3pt}
\tiny
\printbibliography
\vspace{.5mm}
##
\huge
+ Thank you for your atention.
Content of extra_settings_2.tex:
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[frame number]
% References
\usepackage{biblatex}
\addbibresource{/home/guilherme/GoogleDrive/Analises/Modelo/referencias.bib}
Link for csl and Bib files:
What happens is this:
What I need:
References appear before the last slide.
Font size small, so that the reference will fit in only one frame.
The .csl reference style still applies to my documment.
I am using Tinytex, which is based on TeX Live
It shows me an error after compilation (that I don't know how to handle):
Output created: teste_pres.pdf
Warning message:
LaTeX Warning: Empty bibliography on input line 118.
LaTeX Warning: There were undefined references.
Package biblatex Warning: Please (re)run Biber on the file:
(biblatex) teste_pres
(biblatex) and rerun LaTeX afterwards.
Thank you!!

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

tikzDevice does not use LaTeX preamble when used in RMarkdown document

I want to use tikz as graphics device in RMarkdown and I want it to include the generated LaTeX preamble.
In the past, I already used tikzDevice within knitr documents. The tex file generated by tikzDevice usually included the whole preamble from my knitr/LaTeX document. When I use it with RMarkdown, I get the standard preamble (see below).
RMarkdown file:
---
title: "Title"
author: "Me"
fontsize: 12pt
documentclass: scrartcl
output:
bookdown::pdf_document2:
toc: true
fig_caption: true
keep_tex: true
---
# Introduction
```{r plot, dev="tikz"}
plot(rnorm(50))
``
Beginning of generated tex file (plot-1.tex):
% Created by tikzDevice version 0.12.3 on 2019-06-16 16:09:40
% !TEX encoding = UTF-8 Unicode
\documentclass[10pt]{article}
Desired/expected beginning of plot-1.tex:
% Created by tikzDevice version 0.12.3 on 2019-06-16 16:09:40
% !TEX encoding = UTF-8 Unicode
\documentclass[12pt]{scrartcl}
I'm not sure you really want what you're asking for. The figure will be produced as a separate document containing nothing except the figure, which will be rendered as a PDF. The differences between scrartcl and article shouldn't matter for the figure, they matter for the document as a whole.
But if you really do need that document class, you get it by specifying options(tikzDocumentDeclaration = "\\documentclass[12pt]{scrartcl}") in an R chunk early in your document. When I do that I can see in the source that it worked, but the output looks pretty much the same as it did with the default class. It's also possible to specify this using chunk options, but there's unlikely to be any advantage to doing that.
I think I figured it out:
My problem was that while using RMarkdown the options tikzDocumentDeclaration, tikzLatexPackages ... (nearly all options for tikzDevice) were not set automatically. When you use knitr the options for tikzDevice get set up in the process of splitting up markup and code chunks from the source file. With RMarkdown there is no LaTeX code to extract and use with tikz because pandoc generates it after the graphic is rendered. So one can either define the tikz... options manually or use the chunk option external=FALSE like user2554330 suggested.
Example minimal_knitr.Rnw:
\documentclass[fontsize=12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\begin{document}
<<r plot, dev='tikz', echo=FALSE>>=
plot(rnorm(50))
#
\end{document}

How to create customized chapter heading in a Dynamic Document R and knitr

I´m not a LaTex user...actually started learning something about it as consequence of dynamic document I´m working with for a book document class I´m writing in R and knitr.
I´ve tried to customize the chapter headings, but meanwhile I did not get....I found the way to do this by LaTex (https://tex.stackexchange.com/questions/73421/how-to-create-specific-chapter-style-in-book-documentclass) however I´m not knowing how to address by RMardkown.
Does anybody could help me with that? How can I arrange the below LaTex commands to be triggered by RMarkdown (by .sty file?) or something similar way to customize the chapter heading in output pdf?
\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]
Thanks
Fabio
You can put the stuff you want in the header, except the documentclass line, in a seperate tex file (I use header.tex below). You can then specify in the header of your R-markdown file that you want to include this file in the header. The documentclass van also be set in the header of your R-markdown file. More info on this can be found on the rstudio site.
Below an example:
---
title: "Untitled"
output:
pdf_document:
includes:
in_header: header.tex
documentclass: book
---
\chapter{Introduction}
Section
=======
This is an R Markdown document.
header.tex looks like:
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]

Resources