Compiling latex document in R Markdown - r

I am trying to compile a latex document in R markdown using R studio. These are the header options -
---
header-includes:
- \usepackage{float}
- \usepackage{wrapfig}
- \usepackage{graphicx}
- \usepackage{lipsum}
- \usepackage[fontsize=12pt]{scrextend}
- \usepackage{fontspec}
- \newcommand*{\bibfont}{\footnotesize}
- \PassOptionsToPackage{dvipsnames}{xcolor}
- \usepackage[dvipsnames]{xcolor}
- \usepackage{etoolbox}
- \defbibheading{bibliography}[\textcolor{Blue}\refname]{}
- \usepackage{blindtext}
- \usepackage{needspace}
- \usepackage{mwe}
output:
pdf_document:
latex_engine: xelatex
fig_caption: true
citation_package: biblatex
bibliography: Gibbs.bib
mainfont: ArialMT
geometry: margin=0.70in
---
And this is the error I get -
! Undefined control sequence.
<recently read> \defbibheading
Any help? Thanks!

The problem is that while rmarkdown will automatically load the biblatex package due to your citation_package: biblatex option, it will do so only after it has already processed your header includes. This means you can't directly use a macro like \defbibheading{} from the biblatex package in your header-includes.
As a workaround, you could try
---
header-includes:
- \usepackage{float}
- \usepackage{wrapfig}
- \usepackage{lipsum}
- \usepackage[fontsize=12pt]{scrextend}
- \usepackage{fontspec}
- \PassOptionsToPackage{dvipsnames}{xcolor}
- \usepackage[dvipsnames]{xcolor}
- \usepackage{etoolbox}
- \AtBeginDocument{\renewcommand*{\bibfont}{\footnotesize}\defbibheading{bibliography}[\textcolor{blue}{\refname}]{}}
- \usepackage{blindtext}
- \usepackage{needspace}
- \usepackage{mwe}
output:
pdf_document:
keep_tex: true
latex_engine: xelatex
fig_caption: true
citation_package: biblatex
bibliography: Gibbs.bib
mainfont: ArialMT
geometry: margin=0.70in
---
test

Related

strange symbols generated from rmarkdown when I use Chinese characters in ggplot2

Recently, I encounter a problem in Rmarkdown.
When I add Chinese characters into xlab() and ylab() in ggplot2. The pdf document generated by rmd will display strange ...... symbol. However, I try to search useful something and the result told me I should add dev: cairo_pdf. Ok, after I add the code to YAML, the new error strange symbols occur again. It displays another strange symbols of □□□.
I want to ask for help from someone who may pay attention to my quesiton. Could you help me? Thank you very much. And here my test code:
---
title: "test.Rmd"
author: "author"
documentclass: ctexart
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: no
dev: cairo_pdf
word_document: default
keywords:
header-includes: \usepackage{booktabs} \usepackage{longtable} \usepackage{array} \usepackage{multirow}
\usepackage{wrapfig} \usepackage{float} \floatplacement{figure}{H}
---
library(ggplot2)
ggplot(cars,aes(speed,dist)) +
geom_line()+xlab("速度\nSpeed") + ylab("距离\nDist")
Sorry, I cannot replicate your images. I just altered the header-includes and included the figure in a code chunk. Did you install xelatex and TeX Live?
Here is the code that worked fine for me:
---
title: "test.Rmd"
author: "author"
documentclass: ctexart
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: no
dev: cairo_pdf
word_document: default
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \floatplacement{figure}{H}
---
```{r}
library(ggplot2)
ggplot(cars,aes(speed,dist)) +
geom_line()+xlab("速度\nSpeed") + ylab("距离\nDist")
```
-output

How to change bibliography style in Rmarkdown?

I am trying to change the style of my bibliography in Rmarkdown. I followed this solution but nothing happens in my case.
This is my code:
---
title: "Tilte"
author:
- name: Rollo99
abstract: |
Abstract
keywords: whatever
date: "`r Sys.Date()`"
citation_package: natbib
bibliography: library.bib
biblio-style: abbrvdin
references:
link-citations: yes
linkcolor: blue
output:
rticles::elsevier_article:
number_sections: yes
header-includes:
- \setcitestyle{numbers}
- \usepackage{amsmath}
- \usepackage{hyperref}
- \usepackage{graphicx}
- \usepackage[section]{placeins}
- \usepackage{float}
- \usepackage[font=small, skip=1pt]{caption}
- \usepackage[labelfont=bf]{caption}
- \usepackage{amssymb}
- \usepackage{inputenc}
- \usepackage{placeins}
- \usepackage{blindtext}
- \usepackage{setspace}
- \usepackage[top=3cm, bottom=3cm, left=3cm, right=3cm]{geometry}
- \usepackage{amsmath}
- \usepackage{amssymb}
- \usepackage{mathrsfs}
- \usepackage{longtable}
- \usepackage{graphicx}
- \usepackage{ragged2e}
- \usepackage{csquotes}
- \usepackage[bottom]{footmisc}
- \usepackage{xpatch}
- \setlength\parindent{0pt}
- \usepackage{appendix}
I downloaded and saved abbrvdin.bst in the same repository as the bibliography from here.
What am I doing wrong?
Thanks
The elsevier template does not support this, see the code on github.
You can just use plainmat or elsevier-harv.
A solution is to clone the repository and manipulate the file to your need. In addition to that you can raise a pull request.

R Markdown - remove left-sided header in pdf output

I am trying to create an automated report in rmarkdown for business partners and I am a bit stuck on how to set up the headers using the Latex package fancyhdr.
The report includes a table of contents set to level 1 headers, but there are sub-sections in the report that are created using level 2 headers.
When I generate the PDF the fancy header has the level 1 header on the right header (which is fine), the message I set in the central header (also fine), and the sub-section (level 2 header) in the left header.
I would like to remove this, but the documentation is very vague as to how to do so - I've just spent a considerable amount of fruitless time trying to get this to work.
Here are the YAML settings at the top of the R Markdown document:
title: "Report Title"
author: "Authors"
date: 'Date'
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 1
header-includes:
- \usepackage{fontspec}
- \setmainfont{Gotham Book}
- \usepackage{booktabs}
- \usepackage[tocflat]{tocstyle}
- \usetocstyle{standard}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Center Message}
- \fancyfoot[CO,CE]{Footer Message}
- \fancyfoot[LE,RO]{\thepage}
---
These are the "left-odd" and "right-even" headers. If you set them to nothing using \fancyhead[LO,RE]{} they will go away:
---
title: "Report Title"
author: "Authors"
date: 'Date'
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 1
header-includes:
- \usepackage{fontspec}
- \usepackage{booktabs}
- \usepackage[tocflat]{tocstyle}
- \usetocstyle{standard}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Center Message}
- \fancyfoot[CO,CE]{Footer Message}
- \fancyfoot[LE,RO]{\thepage}
- \fancyhead[LO,RE]{}
- \usepackage{blindtext}
---
\blinddocument
(I have removed your font and inserted some sample text to show the effect.)

Change bibliographystyle in R Markdown

I want to change the bibliographystyle in R Markdown but nothing I found could help.
I do not want any "and"s in the bibliography (before the last author).
My preferred option was if I could use alphadin (bst-file here) but I could not get it to work.
Here is my YAML so far:
---
output:
pdf_document
bibliography: literatur.bib
biblio-style: alphadin.bst
header-includes:
- \usepackage{graphicx}
- \usepackage{float}
- \usepackage[ngerman]{babel}
- \usepackage{fancyhdr}
- \usepackage{hyperref}
- \pagenumbering{gobble}
- \usepackage{booktabs}
- \usepackage{natbib}
---
The bst-file is in the same directory as the R Markdown file.
If you want to set the bibliography style to use a bst file, you need to force R Markdown to use natbib or biblatex as the citation manager. By default, it will use pandoc to build the citation. This article explains the behaviour more.
Secondly, once you have that working, you need to change the citation style of the file. By default, natbib will use author-year citations, but the bst file you provided does not work with these. So I have change the citation styles to numbers.
Below is a minimal example. It will create a bibliography file test.bib but you need to make sure the alphadin.bst file is in the same directory.
---
output:
pdf_document:
citation_package: natbib
bibliography: test.bib
biblio-style: alphadin
header-includes:
- \setcitestyle{numbers}
---
[#R-rmarkdown]
```{r}
knitr::write_bib(x = "rmarkdown", file = "test.bib")
```
There is another way to set the citation style of natbib: natbiboptions: round in YAML. The combination of citation_package: natbib and natbiboptions: round is equivalent to \usepackage[round]{natbib}. Note that natbiboptions: round comes outside of the output key.
(In the following example, I used biblio-style: apalike but the example should work with any biblio-style.)
---
output:
pdf_document:
citation_package: natbib
bibliography: test.bib
biblio-style: apalike
natbiboptions: round
---
[#R-rmarkdown]
```{r}
knitr::write_bib(x = "rmarkdown", file = "test.bib")
```

part of square root latex equation dissappears in pdf rendering from RStudio's rmarkdown

When I use this code to make a square root in my pdf document (rendered by rmarkdown):
---
title: "Test"
author: "test test"
geometry: margin=1in
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
html_document:
css: tables.css
number_sections: yes
theme: cerulean
toc: yes
toc_depth: 3
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{this is a fancy foot}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{longtable}
- \usepackage{caption}
- \captionsetup{skip=2pt,labelsep=space,justification=justified,singlelinecheck=off}
subtitle: test test test
fontsize: 12pt
---
$$ S_E\text{=}S_x\sqrt{\text{(}\text{1}\text{-}r_{xx}\text{)}} $$
this is the result:
Notice the strange looking square root. Does anyone know what's going wrong here in my code?
I found the solution to this problem here:
https://tex.stackexchange.com/questions/210153/pdftex-font-expansion-error-during-compilation
In the directory C:\Program Files\MiKTeX 2.9\miktex\bin\x64 I ran updmap.exe. After that the problem was solved.

Resources