Equivalent of \usepackage[round]{natbib} in Rmarkdown yaml - latex

This question is related to Is there a way to keep LaTeX citation keys in .tex file when knitting r-markdown to PDF. Using natbib and xelatex resulted in in-text citations with square parentheses, such as [First et al., 2020] but I need round parentheses: (First et al., 2020). Switching around different latex engines or pdf_document or pdf_document2 does not change this behavior.
I prefer pdf_document2 to accommodate some of my tables better than what pdf_document does. Here is my yaml
title: "abc"
output:
bookdown::pdf_document2:
keep_tex: true
toc: false
latex_engine: lualatex
citation_package: natbib
bibliography: ref.bib
csl: apa-no-ampersand.csl
As stated here https://tex.stackexchange.com/questions/466819/use-round-brackets-instead-of-square-brackets-in-natbib-citations, I can edit my .tex file to set the parenthesis style, upload it to a tex processing program, then download the pdf, but that's quite inefficient.
Any leads to how to render in-text citations in round parentheses straight from R are much appreciated.

Just specify your natbiboptions. If you use BibLaTeX, then you can set biblatexoptions. If you want to specify multiple options, use a list notation with - (two-space indent and hyphen). If you want to comment out an option, use #.
title: "abc"
output:
bookdown::pdf_document2:
keep_tex: true
toc: false
latex_engine: lualatex
citation_package: natbib
bibliography: ref.bib
natbiboptions: round
biblatexoptions:
- sortcites = true
- sorting = nyt
- backend = biber
# - maxcitenames = 2

Related

Latex in document referencing via Varioref is not working in Rmarkdown

I've been using vanilla latex documents for a long time and have recently switched over to Rmarkdown. I'm struggling to get varioref to work, or any in-document referencing for that matter. See below example Rmarkdown file contents. I've tried to make it a minimally reproducible example.
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes: \usepackage{varioref}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}
It seems like the reference is being created with a clickable link, but there is no reference to the section being created..
Rmarkdown producing unnumbered sections has nothing to do with varioref. You can make them numbered like this:
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes:
- \usepackage{varioref}
- \setcounter{secnumdepth}{4}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}

Place bibliography before Appendix when using natbib

I have the following document.
---
title: "What comes after the bibliography?"
bibliography: refs.bib
header-includes:
- \usepackage{natbib}
- \setcitestyle{numbers}
- \setcitestyle{square}
output:
pdf_document:
citation_package: natbib
latex_engine: lualatex
keep_tex: yes
---
# A
This document may use knitr[#pKnitr].
# References
<div id="refs"></div>
# Appendix
`1+1` evaluates in R to `r 1+1` (for more information, see [#pRmarkdown])
I used citation() to create the bibtex references in refs.bib.
#InCollection{pKnitr,
booktitle = {{Implementing Reproducible Computational Research}},
editor = {Victoria Stodden and Friedrich Leisch and Roger D. Peng},
title = {knitr: A Comprehensive Tool for Reproducible Research in {R}},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
year = {2014},
note = {ISBN 978-1466561595},
url = {http://www.crcpress.com/product/isbn/9781466561595}
}
#Book{pRmarkdown,
title = {{R Markdown Cookbook}},
author = {Yihui Xie and Christophe Dervieux and Emily Riederer},
publisher = {Chapman and Hall/CRC},
address = {Boca Raton, Florida},
year = {2020},
note = {ISBN 9780367563837},
url = {https://bookdown.org/yihui/rmarkdown-cookbook}
}
I want to place my bibliography before the Appendix. So far I've tried the following, unfortunately with no luck.
Add <div id="refs"></div> (see example above)
Using \AtEndDocument{} - its contents won't transpile to LaTeX
\printbibliography does not work with natbib
\bibliography{refs} kinda works, but I have to manually delete the bibliography macro created by md / Rmd
For the last point, if I have the following in my Rmd file...
<!-- ... -->
# References
\bibliography{refs}
# Appendix
`1+1` evaluates in R to `r 1+1` (for more information, see [#pRmarkdown])
... it produces the following LaTeX file.
% ...
\section{A}\label{a}}
This document may use knitr\citep{pKnitr}.
\hypertarget{references}{%
\section{References}\label{references}}
\bibliography{refs.bib}
\hypertarget{appendix}{%
\section{Appendix}\label{appendix}}
\texttt{1+1} evaluates in R to 2 (for more information, see
\citep{pRmarkdown})
\bibliography{refs.bib}
\end{document}
So \bibliography{refs.bib} is now added twice, causing a Citations multiply defined error.
How can I add the bibliography before my Appendix using natbib?
If you use remove all the interference from markdown, you are more flexible with the placement of the bibliography:
---
title: "What comes after the bibliography?"
header-includes:
- \usepackage{natbib}
- \bibliographystyle{plainnat}
- \setcitestyle{numbers}
- \setcitestyle{square}
output:
pdf_document:
latex_engine: lualatex
keep_tex: yes
---
# A
This document may use knitr \cite{pKnitr}.
\bibliography{refs}
# Appendix
`1+1` evaluates in R to `r 1+1` (for more information, see \cite{pRmarkdown})

bookdown chapter heading not correct

I am using the bookdown to write a book. I've defined the documentclass as book. Everything seems to be working correctly - the Table of Contents, List of Tables and List of Figures along with an Appendix, Bibliography and Index. However, in my index.Rmd file I lead off with a Preface section that is not numbered. So, this first "Preface chapter" is marked with a level-1 heading # Preface {-}. The Preface is listed correctly in the Table of Contents as an unnumbered Chapter. However, on the page following the "Preface chapter" the heading at the top of the page still says "List of Figures". So, somehow the chapter heading is being held over and repeated from the List of Figures and not being updated with the "Preface chapter" heading. This "List of Figures" heading stays the same until I get to the next chapter (technically the second chapter) which is numbered, then the rest of the page headings are correct for the rest of the book.
I'm a bit of a LaTeX newbie, so I'm sure there is some option or setting I'm missing. Do I need to add some special LaTeX command or Pandoc argument to make sure that the page headings are aligned with every chapter - both numbered and unnumbered?
Any suggestions are much appreciated. My relevant YAML settings and TEX commands are listed below.
_output.yml
bookdown::pdf_book:
includes:
in_header: preamble.tex
after_body: after_body.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
preamble.tex
\usepackage{booktabs}
\usepackage{makeidx}
\makeindex
\usepackage[nottoc]{tocbibind}
index.Rmd - relevant section of YAML
documentclass: book
bibliography: [manual.bib, packages.bib]
biblio-style: apalike
link-citations: yes
lot: true
lof: true
Since pandoc passes LaTeX commands through you could use
# Preface {-}
\markboth{Preface}{}
(c.f. https://tex.stackexchange.com/questions/89914/chapter-name-in-the-header-with-chapter)

R Markdown similar feature to "newcommand" in LaTex?

Does R Markdown have a similar construct to LaTex's "newcommand"? I would like to be able to define things like \var to be \mathrm{Var} to avoid the extra typing in math mode. If not, what do people do to reduce repetition in typesetting equations in markdown?
Use \newcommand{\var}{\mathrm{Var}} exactly like you would in LaTeX:
---
title: "Untitled"
author: "An Author"
date: "January 15, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newcommand{\var}{\mathrm{Var}}
## R Markdown
This is an R Markdown document. $\var+2$ 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>.
Note that in order for it to be processed correctly in the output, you'll have to use $...$.
I'm using bookdown and need to have something that works consistently across pdf, html, and docx output. None of the above solutions worked for my case. Here is the hack I settled on:
preamble.tex
\usepackage{amsthm}
\DeclareMathOperator*{\argmin}{argmin}
\newcommand{\var}{\mathrm{Var}}
YAML Header:
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
toc: no
bookdown::word_document2:
reference_docx: template.docx
bookdown::gitbook:
split_by: none
documentclass: article
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
---
<!--- For HTML Only --->
`r if (!knitr:::is_latex_output()) '
$\\DeclareMathOperator*{\\argmin}{argmin}$
$\\newcommand{\\var}{\\mathrm{Var}}$
'`
<!--- For DOCX Only --->
`r if (!knitr:::is_latex_output() & !knitr:::is_html_output()) '
\\DeclareMathOperator*{\\argmin}{argmin}
\\newcommand{\\var}{\\mathrm{Var}}
'`
# Prerequisites
This is a _sample_ book written in **Markdown**.
To get around the requirement of \DeclareMathOperator needing to be in the preamble, use \operatorname:
\newcommand{\Var}{\operatorname{Var}}
$\Var(X)$
(\operatorname handles spacing better than \mathrm)
To use \newcommand properly in HTML output, your LaTeX should be in-line with single $ or in double $$. This applies to environments like \begin{align*} too.
---
title: "Test"
author: "qwr"
date: "January 22, 2019"
output: html_document
---
\newcommand{\Var}{\operatorname{Var}}
$\Var(X)$
$$
\begin{align*}
\Var[Y] &= x \\
&= 3
\end{align*}
$$
I had issues with the above solution when outputting as a beamer presentation, particularly when using the equation mode ($$.$$ rather than $.$). Putting the new commands in a separate file fixed the issue for me.
---
title: Title
author: Author
date: "8/22/2018"
output:
beamer_presentation:
includes:
in_header: preamble.tex
---
Where preamble.tex contains your user defined command(s)
\newcommand{\var}{\mathrm{Var}}
Then you can use the command both inline ($\var$) and in equation mode ($$\var$$)
You can also put other stuff in preamble.tex like frame numbering, etc.

Incorrect conversion from R Markdown to LaTeX

Why does the following R Markdown minimal (non)-working example not compile to PDF?
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- \lfoot{From: K. Grant}
- \cfoot{To: Dean A. Smith}
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
In particular, the problematic conversion happens to -\lfoot{From: K. Grant} and -\cfoot{To: Dean A. Smith}, as seen in the output .tex file:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{The performance of new graduates}
true
true
For some reason, both of these lines are converted to true causing
LaTeX error: Missing \begin{document}
thereby preventing the document from compiling to PDF.
Changing \lfoot and \cfoot to just about anything else seems to lead to them being converted correctly. So what's going on here? I take it that there must be a problem with either knitr or pandoc in the conversion process.
NB: I'm not too familiar with R Markdown, and this is a follow-up question to Headers and footers created in Fancyhead not shown in PDF on TeX.SX on Tom's behalf.
The : character is the problem. pandoc seems to be trying to parse the header-includes content as if it were variables, and : is used to separate variables and values. It compiles if you quote the lines in question (don't forget, then, to escape the leading backslash)
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- "\\lfoot{From: K. Grant}"
- "\\cfoot{To: Dean A. Smith}"
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
Stitching the answer here with trial and error, I found that only by deleting the title key and using YAML pipe-denoted multiline string syntax would it compile:
---
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
header-includes: |
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
---
I recommend using pandoc's raw_attribute feature (enabled by default) to mark the raw LaTeX section as such. This is the most robust solution.
header-includes: |
```{=latex}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
```

Resources