Include before_body only to some chapters in bookdown - r

Is it possible to include a before_body only to some of the chapters in bookdown?
I tried to write a yaml header for the chapters in which I want to insert the before_body block but nothing showed up
---
bookdown::gitbook:
includes:
before_body: includes/myblock.html
---

Related

R Markdown PDF and fancyhdr, how to add header and footer

I have tried for several days to modify a Rmarkdown PDF file to have both header and footer, also with two logos in the header, However, I just get errors consistently.
the Rmd file with only a yaml header. Only the header is created, with only one logo, and the footer is not created.
I would be grateful if I can get some help with this issue.
---
title: "PDF Outpuf File"
output:
pdf_document:
includes:
in_header: header.tex
keep_tex: true
---
the header.tex with the following text, in a separate file
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{eurosym}
\usepackage{booktabs,xcolor}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[C]{Google Confidential - \url{www.google.com}}
\fancypagestyle{plain}{\pagestyle{fancy}}
\addtolength{\headheight}{2.0cm}
\lhead{Google - Google PD - \today}
\rhead{\includegraphics[width=4cm]{logo1.png}{logo1.png}}

Specify paths to custom theme, preamble and includes in YAML-header of beamer presentation generated with R markdown

In a beamer presentation generated with rmarkdown::beamer_presentation, my aim is to apply a custom theme, which contains a beamerthemeTHEMENAME.sty (with sub files beamercolorthemeTHEMENAME.sty, beamerfontthemeTHEMENAME.sty, beamerinnerthemeTHEMENAME.sty, beamerouterthemeTHEMENAME.sty), as a template.tex and Includes.
Currently I source these files in the YAML-header as follows:
theme: "THEMENAME"
template: template.tex
includes:
in_header: preamble.tex
before_body: before_body.tex
after_body: after_body.tex
To organize the presentation and its files more neatly, I would like to move these files ...
to a subfolder of the presentation directory (short-run solution)
to a general folder such the templates can likewise be sourced by/applied to other presentations (long-term solution).
How would I have to adjust the YAML header in each case to correctly source the above-mentioned files?
Meanwhile, I found an answer which works for the short run.
(For hints regarding a long-term solution, see the answer from #Steven and comments by #Samcarter_is_at_topanswers.xyz.)
Drop all files of the custom theme THEMENAME as well as any includes (e.g., preamble.tex) into a subfolder termed beamer_files within the folder in which the Rmd file of the presentation is located in.
Modify the YAML header and beamerthemeTHEMENAME.sty as shown below.
As per these SO answers (LaTex theme, colon), some LaTex hacks are necessary to apply the LaTex beamer theme smoothly in the rmarkdown::beamer_presentation.
MWE.Rmd
---
# COMMENT out "title" in YAML header: else markdown generates a second title page
# ==> 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
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
% WAS:
% \usecolortheme{THEMENAME}
% \useoutertheme{THEMENAME}
% NOW:
\input{beamer_files/beamercolorthemeTHEMENAME.sty}
\input{beamer_files/beamerouterthemeTHEMENAME.sty}
\mode<all>
preamble.tex
% "title" is commented out in YAML header: else markdown generates a second title page
% if title contains no special characters: feed it straight into LaTex at the end of the YAML header
% if title contains special characters, like ":" or a forced linebreak feed it to LaTex here:
\title[short version]{First line of the title:\par second line of the title}

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

Bookdown book vs chapter Appendix

I am rendering a book in .pdf with bookdown and I want to include several chapters.
_bookdown.yml looks like this:
book_filename: "my-Thesis"
before_chapter_script: "Script1.R" #Script1.R cleans memory (see Script1.R file)
rmd_files: ["index.Rmd", "01-intro.Rmd","02-C-quantity.Rmd","03-C-quality.Rmd","04-fine-roots.Rmd","05-aboveg-biom.Rmd","07-appendix.Rmd","08-definitions.Rmd","09-references.Rmd"]
delete_merged_file: true
_output.yml looks like this:
bookdown::gitbook:
lib_dir: assets
split_by: "rmd"
#config:
#toolbar:
#position: static
bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
latex_engine: xelatex
#citation_package: #harvard #biblatex
I would like to add an Appendix 07-Appendix.Rmd at the end of the book which covers the whole book and shows up as a chapter, not as a subchapter of the "Above-ground..." chapter as shown in the following snapshot. This is also true for the Definitions and Referenceschapters.
Any help is welcome many thanks.
Start a new markdown page with the first line:
Appendix{-}
The {-} removes numbering. I find the rmd_files listing unnecessary. Attaching an image of a page and the resulting contents listing.

R bookdown - custom title page

How to customize title page using bookdown?
I tried using the following code in the YAML header.
includes:
in_header: preamble.tex
before_body: body.tex
The body.tex file was something pretty simple, just for test:
\begin{titlepage}
Hello world
\end{titlepage}
In the LaTeX template <R-Library>/rmarkdown/rmd/latex/default-1.17.0.2.tex we see
\begin{document}
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$
$for(include-before)$
$include-before$
This means that a titlepage is created using \maketitle if a title is defined in the YAML headers. Similar for the abstract. If you remove both these tags from your YAML headers, then the content from the file body.tex will be the first to be processed and you are free to customize your titlepage there.
See the answers to this question for an alternative approach.
I ended up editing the _output.yml file to reference a copy of default-1.17.0.2.tex template in my R project directory using yaml template tag.
bookdown::gitbook:
css: style.css
config:
toc:
before: |
<li>A Minimal Book Example</li>
after: |
<li>Published with bookdown</li>
edit: https://github.com/rstudio/bookdown-demo/edit/master/%s
download: ["pdf", "epub"]
bookdown::pdf_book:
fig_caption: true
number_sections: yes
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
template: template.tex
bookdown::epub_book: default
For some reason i had an error compiling the pdf (! Undefined control sequence...) so I included a latex command \usepackage{graphicx} in template.tex to fix it. Now it is supposed that I am free to customize title page and whatsoever.

Resources