image file not found from URL in r-markdown - r

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.

Related

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

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: 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')

Inline R code in YAML for rmarkdown doesn't run

I'm trying to run inline R code in the YAML front matter before getting rmarkdown to run the file. However it isn't working for me. Here's an example:
---
title: "**Title**"
classoption: xcolor=dvipsnames
output:
beamer_presentation:
slide_level: 2
pandoc_args: [
"--bibliography", "`r paste('path/to/bib')`"
]
---
<!-- slide 1 -->
## Intro ##
Which throws an error:
pandoc-citeproc: could not find `r paste('path/to/bib')`
This is a simple example, but highlights my main problem. How do I get rmarkdown to run the inline R code in the YAML front matter?
It is a similar problem to these questions:
Manipulate RMarkdown metadata from within R code chunks
YAML current date in rmarkdown
This is how I solved this. I knit from RStudio. Curiously, I had to use one solution for the date and csl fields and a different solution for the bibliography field. !expr did not work in the date or csl lines (for me). And quoted r code didn't work in the bibliography line (for me). I have the bibliography and csl files in a package (inst/docs folder). rmarkdown files, which are not part of that package, use those.
---
title: "Title"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: html_document
bibliography: !expr system.file("docs", "my.bib", package = "MyPackage")
csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
---
# Introduction
Yada yada [#MyRef04].
# References
my.bib is the BibTex file with MyRef04. csl is the style file
This is a situation where one person maintains a package which has data, code, bibliography, etc. Others, potentially unknown to the package writer, install that package from GitHub and write or run rmarkdown files that use the package. The users almost certainly do not use Git or GitHub and I don't want them to have to download any extra files after installing the package from GitHub.
Update: After posting the above, I happened to install markdown from GitHub because I needed something in the development version. With version ‘1.7.5’ of rmarkdown on GitHub you can use r code in the bibliography line:
---
title: "Title"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: html_document
bibliography: '`r system.file("docs", "my.bib", package = "MyPackage")`'
csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
---
To install rmarkdown from GitHub
library(devtools)
install_github("rstudio/rmarkdown")
So I found a round about way of getting what I wanted. Rmarkdown I don't think allows R expressions/commands in the YAML, probably for a good reason. What I ended up doing was putting all the output yaml commands in a file called _output.Ryaml like so:
beamer_presentation:
slide_level: 2
includes:
in_header: "src/preamble.tex"
pandoc_args: [
"--bibliography", "`r paste('path/to/bib')`",
"--variable", "classoption:xcolor=dvipsnames",
"--variable", "fontsize:9pt"
]
Then in the main slides.Rmd file, was something like:
---
title: "**Title**"
author: Luke
---
<!-- slide 1 -->
## Intro ##
Then, I can generate the slides using the R code (which I put into a Makefile):
knitr::knit('_output.Ryaml', '_output.yaml')
rmarkdown::render('slides.Rmd')
unlink('_output.yaml')
Seems to be working well enough. If anyone's got a better idea, let me know!

Knitr with the "--biblatex" option

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}.

Resources