Knitr with the "--biblatex" option - r

I'm writing a document in R Markdown with RStudio. While the default citations/bibliography options work well, when doing a TeX/PDF export I would prefer to have the intermediate .tex source use the \autocite and \autocites commands instead of having the citations as regular text. This can be achieved with pandoc by specifying the --biblatex argument.
However, when I add the pandoc_args: ["--biblatex"] in the YAML front-matter, the citations are not rendered (appear as just the #Author2015 tags, in bold) and the bibliography is not appended to the output. I also get an error in the console:
Error in yaml::yaml.load(front_matter) : Scanner error: while scanning a simple key at line 23, column 1could not find expected ':' at line 24, column 1
Unfortunately, there is nothing significant at line 23 or 24.
Am I doing something wrong or is such functionality not available? Are there any other methods of "knitting" the R Markdown source to obtain hyperlinked citations in the TeX/PDF output?
EDIT: alternatively, is there a way of embedding calls to biber in the markdown file so that it's run on the knitted latex source before rendering to PDF? It would be nice to have that done automatically instead of going to the terminal for every export.
I'm using RStudio version 0.98.1103, RMarkdown 0.5.1 and knitr 1.9. My front matter is:
---
title: Title
author: "Name Surname"
date: "30/03/2015"
output:
pdf_document:
fig_caption: yes
keep_tex: yes
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
pandoc_args: [
"--biblatex"
]
html_document:
fig_caption: yes
number_sections: yes
theme: default
toc: yes
toc_depth: 3
bibliography: refs.bib
csl: harvard1.csl
---

Dealing with the same problem, here's what I have in my YAML header right now:
pandoc_args: ["--biblatex","--latex-engine=xelatex"]
csl: csl/chicago-author-date.csl
header-includes:
- \usepackage[authordate, notes, language=english, backend=biber]{biblatex-chicago}
My best trick right now is to go back to a latex version of what I'm trying to write in .Rmd, then run
pandoc --biblatex 1f.tex -o 1f.md.
Then, I can insert this into the .Rmd. It correctly converted \cite[pg]{citekey}.

Related

How to have `pkgdown` use figure numbers and cross-references in vignettes?

I've created several vignettes for a package, with figures I want to reference in the text.
Using the template for a .Rmd vignette, I can do this by using bookdown::html_document2 as follows in my yaml header:
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
fig_caption: yes
toc: true
Yet, when I build the associated pkgdown site, I don't get figure numbers or cross-references,
done with \#ref(fig:chunk_name).
Is there some magic I can add to my _pkgdown.yml file to have it use the bookdown output format?
Edit: Not sure if this has anything to do with this issue, but my figure chunk labels are of the form topic-figure rather than topic_figure.
E.g.,
```{r, plastic1-HE3D}
#| echo=FALSE,
#| fig.cap="3D HE plot for the plastic MLM"
knitr::include_graphics("fig/plastic-HE3D.png")
```
A solution that seems to work is suggested in https://github.com/r-lib/pkgdown/issues/2201
Essentially, add pkgdown: as_is: true to the yaml headers of vitnettes:
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
fig_caption: yes
toc: true
toc_depth: 2
pkgdown:
as_is: true

YAML Header for R Markdown file - Adding LaTeX packages with options throws errors (sometimes)

Following the R Markdown Cookbook (along with a number of the posts here) and I'm trying to insert a collection of LaTeX packages in the YAML header for my R Markdown file.
When I copy/paste the header from that website into my file:
output:
pdf_document:
extra_dependencies:
caption: ["labelfont={bf}"]
hyperref: ["unicode=true", "breaklinks=true"]
lmodern: null
I get the error: ! LaTeX Error: Option clash for package hyperref. This only happens when I include the options in square brackets - if I remove them, everything works as expected.
In a similar instance, I tried to add the babel package in the same manner. When I include it like this:
extra_dependencies:
babel: ["hebrew", "english"]
I get the error: ! You can't use end-group character }' after \the.`
I am even more confused (if that's possible), because when I include options for the threeparttable package, everything runs without issue.
Here is a file to test:
---
title: "R Notebook"
author:
- Me
date: "Last compiled on `r format(Sys.time(), '%d %B %Y')`"
output:
pdf_document:
highlight: tango
keep_tex: yes
number_sections: yes
toc: yes
fig_caption: yes
extra_dependencies:
cjhebrew: null
babel: ["hebrew", "english"]
---
# Section 1
Test
When I knit this to PDF, I get the error:
! You can't use `end-group character }' after \the.
\everypar #1->\o#everypar {\rl#everypar #1}
The log file is at this repo.
rmarkdown automatically loads the hyperref package. You can't load it again with conflicting options.
You can change its settings with \hypersetup{...}:
---
output:
pdf_document:
keep_tex: true
header-includes:
- \hypersetup{breaklinks=true}
---
test
However are you sure you need all your options and packages?
rmakdown already uses the unicode option, so no need to add it a second time
rmarkdown also automatically loads the lmodern package, no need to load it again
The other problem is the interaction of the babel and the microtype package, which rmarkdown loads by default. With real latex, you could simply avoid loading every package but the kitchen sink and you would never have had this problem, or at least you could add the nopatch option directly to microtype (if you would really need it) ... but rmarkdown makes this unnecessarily complicate.
Here a dirty hack, hoping that no other packages also uses this name for an option:
---
title: "R Notebook"
author:
- Me
date: "Last compiled on `r format(Sys.time(), '%d %B %Y')`"
output:
pdf_document:
highlight: tango
keep_tex: yes
number_sections: yes
toc: yes
fig_caption: yes
extra_dependencies:
cjhebrew: null
babel: ["hebrew", "english"]
classoption: "nopatch"
---
# Section 1
Test

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.

R: Removing the "Chapter" part from the title in bookdown::pdf_book with documentclass: report

consider my header and first section
---
title: "asdf"
author: "asdf"
date: "13 Januar 2018"
documentclass: report
output:
bookdown::pdf_book:
citation_package: biblatex
latex_engine: pdflatex
number_sections: yes
fig_caption: yes
---
# Introduction
Here begins my introduction
I want to remove the automatic "Chapter 1" part, thats sits in front of the actual chapter title (in this case Introduction)
This does not work
header-includes:
\renewcommand{\chaptername}{}
Also using another documentclass does not work, because I need the number of my sections/chapters be represented in the figure captions (e.g Figure 3.1)
What I would like is to end up with a Title 1 Introduction , than 2 Methods and so on.
Should not be to hard, but I can't figure it out.
Thanks
Greg
You can use
subparagraph: true
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
together with
\usepackage{titlesec}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
in preamble.tex (c.f. https://tex.stackexchange.com/questions/10326/how-to-set-the-chapter-style-in-report-class). You have to make sure that the LaTeX package titlesec is installed in your TeX system.
At the time of writing subparagraph: true was needed since the used pandoc LaTeX template redefined \paragraph and \subparagraph in a way interfering with titlesec. This has changed since then. at least with rmarkdown version 2.1 one no longer needs subparagraph: true. thanks #MarkNeal for noticing this!

Resources