Inserting title page with image in Rmarkdown to render a pdf document - r

In Rmarkdown, the following sets the context to generate the pdf document:
---
title: "My Report"
author: NDE
output:
pdf_document:
fig_caption: true
toc: true
highlight: kate
---
I want to insert a title page with an image, title before table of contents gets printed. Is there a way I can achieve it?

For me it worked using LaTeX commands \clearpage and \tableofcontents:
---
title: "image before toc"
output: pdf_document
---
\centering
![Caption](folder/image.png)
\raggedright
\clearpage
\tableofcontents
# header 1
lore ipsum
## header 2
lore ipsum
## header 3
lore ipsum
# header 4
If you want the table of contents on the title page, just leave \clearpage command out.
I included \centering and \raggedright commands to center the image on the title page but not the text.
Hope that works for you.

Related

Insert the number of slides up to a final slide in a rmarkdown::beamer_presentation

In a beamer presentation generated with rmarkdown::beamer_presentation, I currently have \insertframenumber/\inserttotalframenumber which shows the current page and the total number of slides in the presentation.
How to insert the number of slides up to a "final slide" in the footer instead of the overall "total number of slides"?
Note: I would like to refrain from having to add {.noframenumbering} to all slides in the appendix.
MWE
Preamble.tex
\setbeamertemplate{footline}{
\leavevmode%
\hfill
\hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
}
Presentation.Rmd
---
title: "Slide counter ends at specific slide"
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
latex_engine: lualatex
toc: false
slide_level: 2
header-includes:
- \input{files_beamer/preamble}
---
## Slide 1
## Slide 2
## Slide Final
==> count up to this slide
``` {=latex}
\insertframeendpage
```
## Additional Slide 1 (not counted)
## Additional Slide 2 (not counted)
If your beamer version is up-to-date, you can use the \setbeamertemplate{page number in head/foot}[appendixframenumber] template. No need for additional packages.
---
title: "Slide counter ends at specific slide"
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
latex_engine: lualatex
toc: false
slide_level: 2
keep_tex: true
header-includes: |
\makeatletter\beamer#ignorenonframefalse\makeatother
\setbeamertemplate{page number in head/foot}[appendixframenumber]
\setbeamertemplate{footline}{%
\leavevmode%
\hfill
\hyperlinkappendixstart{%
\usebeamertemplate{page number in head/foot}%
}
}
---
## Slide 1
## Slide 2
## Slide Final
==> count up to this slide
``` {=latex}
\end{frame}
\appendix
\begin{frame}
\frametitle{Additional Slide 1 (not counted)}
```
## Additional Slide 2 (not counted)
Just add to the YAML-header:
header-includes:
- \usepackage{appendixnumberbeamer}
(See this SO post).
Alternatively one could likely define a new command using something like \inserttotalframenumber - \insertappendixframenumber somewhere before \setbeamertemplate{footline}{...}.

Remove additional blank slides after ToC and insert black slides in rmarkdown::beamer_presentation with custom beamer theme

I use a custom LaTex beamer theme in an rmarkdown::beamer_presentation.
As per these SO answers (LaTex theme, colon, theme path), I used several modifications of the YAML header and beamerthemeTHEMENAME.sty.
These LaTex hacks are necessary to apply the LaTex Beamer theme smoothly in the rmarkdown::beamer_presentation.
However, currently, a blank slide is inserted by LaTex before and after the table of contents (ToC) slide (see fig 1 below).
How can I get rid of these slides?
Instead, I would ideally have a completely black slide before the ToC and at other instances during the presentation (i.e., to briefly interrupt and "switch off" the presentation in between)
Note: if I use the option toc: true to generate the ToC (instead of using the \tocframedefined in beamerouterthemeTHEMENAME.sty), markdown messes up the vertical alignment of the ToC and it comes without any headline (see fig 2 below).
Fig 1: using Tocframe from LaTex Template to generate ToC (toc:false in YAML header)
Fig 2: using toc:true in YAML header to generate ToC
MWE.Rmd
---
# COMMENT out "title" in YAML header: else markdown generates a second title slide
# ==> if title contains no special characters: feed it straight into LaTex at the end of the YAML header
# ==> if title contains special characters, like ":", feed it in "preamble.tex" sourced in "LaTex Hacks"
subtitle: "Beamer presentation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
# beamer_presentation: default
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
# Source below in "LaTex Hacks", if want theme to be stored in subfolder (else rmarkdown does not find it)
# theme: "THEMENAME"
# Source "includes" below in "LaTex Hacks" if using custom theme with custom title page
# => else, markdown places the includes at inadequate position in LaTex file, which then either does not show the title or throws an error
# includes: ...
latex_engine: xelatex
toc: false # use LaTex TOC (else vertical alignment is messed up and not headline)
slide_level: 2
classoption: aspectratio=169 # fix aspect ratio of presentation (169 => 16:9, 149 => 14:9, default: 4:3)
#
# LaTex Hacks
# --------------------------
compact-title: false # to remove markdown generated title frame
header-includes:
# - \title{Title if no special characters}
- \input{beamer_files/beamerthemeTHEMENAME.sty}
- \input{beamer_files/preamble} # feed title to LaTex in preamble.tex due to ":"
- \def\titlefigure{img/my_bg}
- \AtBeginDocument{\titleframe} # add title frame defined in beamerouterthemeTHEMENAME
- \makeatletter\beamer#ignorenonframefalse\makeatother
---
​```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
​```
<!-- TOC - Table of Contents -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\tocframe
\begin{frame}
```
## Slide with Bullets
<!-- ======================================================== -->
- Bullet 1
- Bullet 2
- Bullet 3
<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```
beamerthemeTHEMENAME.sty
\mode<presentation>
% Tizk, textpos, graphics
\RequirePackage{tikz}
\RequirePackage{textpos}
\RequirePackage{booktabs}
% Beamer settings
\input{beamer_files/beamercolorthemeTHEMENAME.sty}
\input{beamer_files/beamerouterthemeTHEMENAME.sty}
\setbeamertemplate{navigation symbols}{}
\mode<all>
beamerouterthemeTHEMENAME.sty
\mode<presentation>
% Redefine \insertshortinstitute to allow multiple lines
\makeatletter
\renewcommand{\insertshortinstitute}{\beamer#shortinstitute}
\makeatother
% Title frame
\def\titlefigure{img/my_bg}
\newlength\titleboxwidth
\setlength{\titleboxwidth}{0.43\textwidth}
\newcommand{\titleframe}{%
{
\setbeamertemplate{background}{
\begin{tikzpicture}
\useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
\ifx\titlefigure\empty
\fill[titlebgcolor] (6.3mm,6.4mm) rectangle (\the\paperwidth-8.3mm,\the\paperheight-13mm);
\else
\node at (current page.center) [anchor=center,yshift=-3.5mm] {\includegraphics[width=\the\paperwidth-19mm]{\titlefigure}};
\fi
\node at (current page.north east) [anchor=base east, xshift=-8.3mm, yshift=-6.3mm, align=left, inner sep=0mm, font=\fontsize{5.5}{6.6}\selectfont] {
\insertshortinstitute
};
\node at (current page.north west) [anchor=south west, inner sep=0mm, xshift=8.3mm, yshift=-8.6mm] {
\includegraphics[height=3.8mm]{img/my_logo}
};
\end{tikzpicture}
}
\setbeamertemplate{footline}{}
\begin{frame}[noframenumbering]
\begin{tikzpicture}
\useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
\node [
shift={(-11.5mm,-1.8mm)},
fill=titlefgcolor!50!white,
minimum width=0.46\paperwidth,
minimum height=18mm,
anchor=west,
inner sep=4mm
] at (current page.west) {
\hspace*{9mm}
\begin{minipage}{\titleboxwidth}
\raggedright
\usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle\par
\usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\par
\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate
\end{minipage}
};
\end{tikzpicture}
\end{frame}
}
}
% TOC frame
\newcommand{\tocframe}{%
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
}
% Section title frame
\AtBeginSection[]
{
\begin{frame}
\frametitle{Outline}
\large
\tableofcontents[currentsection]
\end{frame}
}
% Frame title
\setbeamertemplate{frametitle}{
\vspace{2mm}
\insertframetitle \\
\usebeamerfont{framesubtitle}\insertframesubtitle
\vspace{2.5mm}
}
% Footline
\setbeamertemplate{footline}{
\leavevmode%
\hyperlink{toc---table-of-contents}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{img/my_logo.png}}
\hfill
\hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
\vspace{3mm}
}
\mode<all>
preamble.tex
% "title" is commented out in YAML header: else markdown generates a second title slide
% if title contains no special characters: feed it straight into LaTex at the end of the YAML header
% if title contains special characters, like ":", feed it to LaTex here:
\title[short version]{First line of the title:\\ second line of the title}
The additional frames are inserted by markdown because it will automatically start unnecessary frames even if none of the content is actually printed to the frame. You can avoid the empty frame between the title and your toc frame by placing the \tocframe after your \titleframe.
for black frames, you can define a new macro, e.g.
\newcommand{\blackframe}{{\setbeamercolor{background canvas}{bg=black}\begin{frame}[plain]\end{frame}}}
---
subtitle: "Beamer presnetation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
author: "Donald Duck"
output:
# beamer_presentation: default
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
# includes:
# in_header: preamble.tex
theme: "THEMENAME"
latex_engine: xelatex
toc: false
slide_level: 2
keep_tex: true
header-includes:
- \newcommand{\blackframe}{{\setbeamercolor{background canvas}{bg=black}\begin{frame}[plain]\end{frame}}}
- \AtBeginDocument{\title{MWE}\titleframe\blackframe\tocframe}
- \AtEndDocument{\begin{closingframe}lalala\end{closingframe}}
- \makeatletter\beamer#ignorenonframefalse\makeatother
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# section
## Slide with Bullets
<!-- ======================================================== -->
- Bullet 1
- Bullet 2
- Bullet 3
``` {=latex}
\end{frame}
\blackframe
\begin{frame}
```
<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```

How to edit footer in RMarkdown using the fancyhdr package

I have created a PDF in RMarkdown with the fancyhdr package in LaTeX. I'm trying to find a solution in which I can insert a horizontal line as a footer (just above the page number), as well as the page number in the right hand corner similar to the image I have attached. Additionally, is there a way in which I can change the color of all the hyperlinks (such as the table of contents) from the default black color to the same color as in the image?
My YAML is as follows
---
title: "Template for a Dynamic Report for Work"
Uni_Logo: Tex/logo.jpg
Author1: "Tangeni Shatiwa"
# ----- Manage headers and footers:
#BottomLFooter: $Title$
#BottomCFooter:
#TopLHeader: \leftmark # Adds section name at topleft. Remove comment to add it.
BottomRFooter: "\\footnotesize Page \\thepage" # Add a '#' before this line to remove footer.
addtoprule: TRUE
addfootrule: TRUE # Use if footers added. Add '#' to remove line.
toc: TRUE
numbersections: TRUE # Should sections (and thus figures and tables) be numbered?
fontsize: 11pt # Set fontsize
linestretch: 1.2 # Set distance between lines.
link-citations: TRUE # This creates dynamic links to the papers in reference list.
linkcolor: blue
output:
pdf_document:
includes:
in_header: "header.tex"
---
and the contents of the header.tex file are as follows
\usepackage{fancyhdr}
\usepackage{titling}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\setlength{\headheight}{50pt}
\lhead{\includegraphics[width = .2\textwidth]{logo.jpg}}
\fancyfoot[R]{\thepage}
\renewcommand{\footrulewidth}{0pt}
\pretitle{%
\begin{center}
\LARGE
\includegraphics[width=4cm,height=6cm]{logo.jpg}\\[\bigskipamount]
}
\posttitle{\end{center}}
TIA!
Here's an example rmarkdown document and header.tex file. I removed the logo lines in header.tex since I don't have your logo. Let me know if this is what you had in mind.
rmarkdown file
---
title: "Template for a Dynamic Report for Work"
Author1: "Tangeni Shatiwa"
BottomRFooter: "\\footnotesize Page \\thepage" # Add a '#' before this line to remove footer.
addtoprule: TRUE
addfootrule: TRUE # Use if footers added. Add '#' to remove line.
toc: TRUE
numbersections: TRUE # Should sections (and thus figures and tables) be numbered?
fontsize: 11pt # Set fontsize
linestretch: 1.2 # Set distance between lines.
link-citations: TRUE # This creates dynamic links to the papers in reference list.
urlcolor: darkred
output:
pdf_document:
includes:
in_header: "header.tex"
---
\newpage
\pagenumbering{arabic}
Some text
[This is a link to Stack Overflow](www.stackoverflow.com)
Some more text
\newpage
Even more text
---
\newpage
\pagenumbering{arabic}
Some text
[This is a link to Stack Overflow](www.stackoverflow.com)
Some more text
\newpage
Even more text
header.tex file
\usepackage{fancyhdr}
\usepackage{titling}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot[L]{This is the left footer}
\fancyfoot[R]{\thepage}
\fancyfoot[C]{}
\setlength{\headheight}{50pt}
%\lhead{\includegraphics[width = .2\textwidth]{logo.jpg}}
\renewcommand{\footrulewidth}{1pt}
\pretitle{%
\begin{center}
\LARGE
%\includegraphics[width=4cm,height=6cm]{logo.jpg}\\[\bigskipamount]
}
\posttitle{\end{center}}
\pagenumbering{gobble}
\usepackage{xcolor}
\definecolor{darkred}{rgb}{0.7,0,0}
PDF output

How can I separate the title page from the table of contents in a RMarkdown Word output?

How can I separate the title page from the table of contents in a RMarkdown Word output? I've seen a lot on here about pdf and html output, but not much about Word. I've tried to use a little bit of LaTex, but it seems that most of that only works with pdf output, and the bit that does work with Word doesn't do what I'm looking for.
---
title: "Sample Title"
subtitle: "Sample Subtitle"
date: "April 8, 2020"
output:
word_document:
toc: yes
---
\newpage
# Header 1
sample text
## Header 2
more sample text
# Another Header 1
loren ipsum
## Another Header 2
foo bar
This just puts a blank page after the table of contents, not before. It feels like there is something simple that I'm missing.
You may consider exploring the officedown package and its block_toc function (together with <!---CHUNK_PAGEBREAK---> inline yaml):
---
title: "Sample Title"
subtitle: "Sample Subtitle"
date: "April 8, 2020"
output:
officedown::rdocx_document
---
<!---CHUNK_PAGEBREAK--->
# Table of Contents
`r officedown::block_toc(level = 5, style = NULL, separator = ";")`
<!---CHUNK_PAGEBREAK--->
# Header 1
sample text
## Header 2
more sample text
# Another Header 1
loren ipsum
## Another Header 2
foo bar
Or even simple, as discussed here:
<!---CHUNK_PAGEBREAK--->
# Table of Contents
<!---BLOCK_TOC--->
<!---CHUNK_PAGEBREAK--->
To place the toc wherever needed without messing with tex files (here with a pdf output),
Turn off automatic toc insertion first in the YAML metadata.
---
title: "myTitle"
date: "`r Sys.Date()`"
output:
pdf_document:
toc: no
number_sections: true
urlcolor: blue
editor_options:
chunk_output_type: console
documentclass: report
---
Then, wherever you want the toc to be in your document, add
```
{=latex}
\setcounter{tocdepth}{4}
\tableofcontents
```
You can then place this toc anywhere using latex macros such as \newpage or \hfill\break for example.
---
title: "myTitle"
date: "`r Sys.Date()`"
output:
pdf_document:
toc: no
number_sections: true
urlcolor: blue
editor_options:
chunk_output_type: console
---
\newpage
```{=latex}
\setcounter{tocdepth}{4}
\tableofcontents
```
\newpage
Note: documentclass: report in the metadata will automatically separate the toc from the title, but won't allow to separate it from the remainder of the document.
Source

can I produce a figure caption in r markdown with eval=false?

If I have the MWE:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
summary(cars)
```
then I do not get a caption. But if I do:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers"}
summary(cars)
```
i.e. remove eval=FALSE then the caption no longer loads.
why I wish to do this?
I want to put example bits of code into my document. the code won't actually work, hence why I want to supress it. Something like
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
for (i in 1:length(c){
#do something
}
```
where I am merely demonstrating a for loop, but not actually running the code.
As far as I know, knitr doesn't support captions for code by default. The easiest way to label your code blocks would be to add an explanation below the box in the markdown.
If you must have captions in the r code, you can use chunk hooks. Here's an example for your case:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
```{r}
library(knitr)
knit_hooks$set(wrapper = function(before, options, envir) {
if (!before) {
sprintf(options$comment)
}
})
```
```{r comment="Figure 1. Some random numbers",wrapper=TRUE,eval=FALSE}
for (i in 1:length(c){
#do something
}
```
We have defined a hook (wrapper), where if we call wrapper=TRUE in any chunk options, the comment argument is printed below.

Resources