R markdown - set options for LaTeX packages - r

If I set an output parameter in .Rmd file in following way:
output:
bookdown::pdf_book:
base_format: rticles::elsevier_article
keep_tex: true
in the produced .tex file, endfloat package will be used with option 'nomarkers'. In other words, following line will be included in produced .tex file:
\usepackage[nomarkers]{endfloat}
How can I set options for LaTeX packages used by the output?
In my example, I wish to set endfloat options to 'markers', so that mentioned line in .tex file will looks as follows:
\usepackage[markers]{endfloat}

The simple solution
header-includes:
- \usepackage[markers]{endfloat}
output:
bookdown::pdf_book:
base_format: rticles::elsevier_article
keep_tex: true
works not, because there are package conflicts. The other solution I tried and worked, was to download the package from GitHub https://github.com/rstudio/rticles and change the file template.tex to your needs. This file is located in /inst/rmarkdown/templates/elsevier_article/resources/. For me this worked and the .tex file has the desired output as you expect it.

Related

How to export R Notebook to LaTex (.tex)? [duplicate]

In R, when compiling a markdown document using rmarkdown::render(...), how can I retain the intermediate .tex file that is produced from knitting.
I have tried setting the clean=FALSE argument, but this retains the figures, not the final tex file or auxiliary files, which I need to inspect for debugging purposes.
You can specify it in your YML header with:
output:
pdf_document:
keep_tex: true
---
More options on the rmarkdown site.

Bookdown: Exporting to a word document (Error in files2[[format]] : attempt to select less than one element in get1index)

I am writing my thesis using the bookdown package and the memoir latex class. Everything works relatively fine when I am exporting to pdf or to html but I am unable to export the thesis to a word document...
I get the following mysterious error:
Error in files2[[format]] :
attempt to select less than one element in get1index
It is difficult to provide a reproducible example though, as I am working from my messy dissertation repository.
But here is (a part of) my _output.yml file:
bookdown::pdf_book:
includes:
in_header: latex/preamble.tex # defines style and latex options
before_body: latex/before_body.tex # defines cover page
latex_engine: xelatex # lualatex or xelatex
citation_package: none # needs to be "none" in order to use the csl file
keep_tex: true # keeps the .tex file
dev: "cairo_pdf"
toc: false # deactivates default table of contents
highlight: pygments # code highlighting
pandoc_args: [ "--csl", "./csl/apa6.csl" ] # specifies the csl file to be used
bookdown::word_document2:
pandoc_args: [
"--csl", "./csl/apa6.csl",
"--bibliography", "./bib/packages.bib",
"--bibliography", "./bib/thesis.bib",
#"--reference-doc", "./assets/2018-05-17-aim1-draft.docx",
"--filter", "pandoc-citeproc"
#"--filter", "./assets/fix-apa-ampersands.py"
]
Any idea ?
This is a bug of the bookdown package, which I just fixed on Github. Please try the development version there:
remotes::install_github('rstudio/bookdown')

image file not found from URL in r-markdown

I used to be able to render images in r-markdown using a URL with the following code ![](https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark) but I get a file not found error ! LaTeX Error: File https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark' not found.
Am I missing packages? This code still works on some shiny apps published a few month ago.
Below the a working file r-markdown file:
---
title: "Test"
header-includes:
- \usepackage{graphicx}
output:
pdf_document:
latex_engine: xelatex
number_sections: yes
keep_tex: yes
classoption: article
papersize: A4
fontsize: 10pt
geometry: margin=0.9in
linestretch: 1.15
---
## R Markdown
![](https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark)
The LaTeX graphicx package does not include an http client, it is therefore not able to pull the image from the internet. However, a lot of the conversion work from Markdown to LaTeX is performed by pandoc, which can get this image. One just needs to tell pandoc to store all images locally by passing the --extract-media option. This allows LaTeX to find the images when it is invoked by RMarkdown.
---
output:
pdf_document:
pandoc_args: ["--extract-media", "."]
---
The above will store all images in the same directory as the Rmd file. The files will be named using SHA1 hashes, so you might want to use a separate directory for these files instead.

Is there a way to add chapter bibliographies using bookdown?

I am trying to write my PhD Thesis with bookdown and am mainly using pdf output. I have easily added a bibliography at the end of the document but would rather have a bibliography at the end of each chapter. I have tried adjusting the .tex output with LaTeX packages that allow this but this fights with the bookdoown defaults. Is there a way of adapting the .yaml options to enable this?
For HTML output the default is to use per-chapter bibliographies. For PDF output, I have found it is best to use the LaTeX package biblatex together with biber. Since RStudio does not know about biber, it is best to install a tool like latexmk and configure RStudio to use that via Sys.setenv(RSTUDIO_PDFLATEX = "latexmk"). These programs might have to be installed separately, e.g. on Debian/Ubuntu/...
sudo apt-get install texlive-bibtex-extra biber latexmk
For configuring biblatex the solution provided at https://tex.stackexchange.com/questions/199336/biblatex-reference-both-by-chapter-and-at-the-end-of-the-book is appropriate.
In the end the following settings are necessary in _output.yml:
bookdown::pdf_book:
citation_package: biblatex
In Index.Rmd:
biblio-style: authoryear
biblatexoptions: [refsegment=chapter]
At the end of every chapter:
\printbibliography[segment=\therefsegment,heading=subbibliography]
There is no need to escape this raw LaTeX command, since pandoc ignores such commands for other output formats.
One can see the entire solution at
https://github.com/rstub/bookdown-chapterbib
https://rstub.github.io/bookdown-chapterbib/
https://rstub.github.io/bookdown-chapterbib/bookdown-chapterbib.pdf
Original solution
I managed to get chapter bibliographies with PDF output using the following steps:
Start with a copy of https://github.com/rstudio/bookdown-demo
copy <R-library-path>/rmarkdown/rmd/latex/default-1.17.0.2.tex as book.tex to the working directory
update book.tex to use the LaTeX package bibunits (diff below)
update _output.yml to refer to book.tex as template (diff below)
set YAML options in index.Rmd (diff below)
add code to some Rmd files to write \putbib command (diff below)
After these changes, a PDF file could be produced, but all references where missing, since bookdown does not know about the generated bu?.aux files. After executing bibtex bu1 and bibtex bu2, reproducing the PDF file via bookdown produced a PDF with chapter bibliographies. It is probably best to automate this step with Makefile.
Here the diff between the templates:
$ diff -u /usr/local/lib/R/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex book.tex
--- /usr/local/lib/R/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex 2017-12-11 19:14:54.643867696 +0100
+++ book.tex 2018-01-16 11:43:46.182542634 +0100
## -93,8 +93,11 ##
\fi
$endif$
$if(natbib)$
-\usepackage{natbib}
+\usepackage[$natbiboptions$]{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
+\usepackage{bibunits}
+\defaultbibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
+\defaultbibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
$endif$
$if(biblatex)$
\usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}
## -235,6 +238,7 ##
$endfor$
\begin{document}
+\bibliographyunit[\chapter]
$if(title)$
\maketitle
$endif$
And the diff of the files from bookdown-sample:
$ git diff
diff --git a/01-intro.Rmd b/01-intro.Rmd
index 6b16e73..1a5f9de 100644
--- a/01-intro.Rmd
+++ b/01-intro.Rmd
## -19,3 +19,5 ## knitr::kable(
```
You can write citations, too. For example, we are using the **bookdown** package [#R-bookdown] in this sample book, which was built on top of R Markdown and **knitr** [#xie2015].
+
+`r if (knitr:::is_latex_output()) '\\putbib'`
diff --git a/02-literature.Rmd b/02-literature.Rmd
index 00745d0..983696e 100644
--- a/02-literature.Rmd
+++ b/02-literature.Rmd
## -1,3 +1,6 ##
# Literature
Here is a review of existing methods.
+[#R-knitr]
+
+`r if (knitr:::is_latex_output()) '\\putbib'`
diff --git a/_output.yml b/_output.yml
index 342a1d6..cc8afb1 100644
--- a/_output.yml
+++ b/_output.yml
## -14,4 +14,5 ## bookdown::pdf_book:
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
+ template: book.tex
bookdown::epub_book: default
diff --git a/index.Rmd b/index.Rmd
index 4e21b9d..2fdb813 100644
--- a/index.Rmd
+++ b/index.Rmd
## -7,6 +7,8 ## output: bookdown::gitbook
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
+natbiboptions: sectionbib
+graphics: yes
link-citations: yes
github-repo: rstudio/bookdown-demo
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
For a latex-specific solution see this post, i.e. use the sectionbib option for natbib and load the chapterbib package. You will need to edit The bookdown template to set the required options for natbib and tell bookdown to use your custom template in the yaml. See this post for more info on the bookdown template.
I swear I saw instructions in the bookdown docs for doing this with gitbook format, but I can't seem to find it...
EDIT I went and looked at an old bookdown project of mine. For gitbook output, you specify the following in _output.yml:
bookdown::gitbook:
split_by: chapter
split_bib: yes
Which will (you guessed it) split the bibliography by chapter. I'm actually a bit surprised that bookdown doesn't support the equivalent options for bookdown::pdf_book yaml options, but should be easy enough to do by setting up sectionbib/chapterbib in the LaTeX preamble.

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

Categories

Resources