Place bibliography before Appendix when using natbib - latex

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

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}

Rmarkdown Latex citation (Harvard style) within footnote

I am writing my thesis in Rmarkdown and Latex with a seperate literature.bib file that contains the bibtex sources. I now have a footnote and I would like to have a citation like in the normal text, but within the footnote. Is there any package to achieve that? When i just try it, like I would have a citation in the normal text, the footnote stops beeing a footnote when i render to pdf. The citation style I use is the Harvard citation style, so only something like this appears and should appear in the footnote:
The rest of information is than provided under the heading References at the end of the document. Thanks in advance! I appreciate any help...!
Here a minimal reproducable example:
--> This is the code for the main .rmd file
---
output:
bookdown::pdf_document2:
toc: no
papersize: a4
geometry: margin = 1in
fontsize: 11pt
bibliography: literatur.bib
---
#TEST
This is just an example #test.\footnote{I would also like to have a citation here in this footnote!!}
\newpage
# References
<div id="refs"></div>
-->This is the code for the literatur.bib file
#online{test,
author = {PACER},
title = {Service Center},
url = {https://pcl.uscourts.gov},
urldate = {2021-09-10}
}
Hope that is helpful?!
If you use markdown syntax for the footnote, you can also insert the citation via markdown syntax:
---
output:
bookdown::pdf_document2:
toc: no
papersize: a4
geometry: margin = 1in
fontsize: 11pt
bibliography: literatur.bib
---
#TEST
This is just an example #test.^[I would also like to have a citation here in this footnote!! See [#test]]
\newpage
# References
<div id="refs"></div>

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

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

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

How can I insert page number to rmarkdown beamer slides?

How can I show page numbers (preferably like 4/10, 5/10 etc.) on an rmarkdown beamer presentation?
In the front matter of the document, you can include a .tex file with extra commands as shown in RStudio's documentation here.
I created a new .tex file that I called "header_pagenrs.tex" which only includes the top 2 lines from #user4281727's answer:
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
Then I referenced this file at the top of my .Rmd file:
---
title: "Things and Stuff"
author: "Me"
output:
beamer_presentation:
includes:
in_header: header_pagenrs.tex
---
If this still gives you errors, you might also be missing some required TeX packages (a separate problem from RStudio and rmarkdown).
Here's another option that worked for me. Didn't need to add a .tex file to my folder. Just included the following (based on above code from #civilstat) at the top of my Markdown doc.
---
title: 'Your Title'
author: "Your Name"
date: "July 4, 1776"
output:
beamer_presentation(keep_tex = TRUE): default
header-includes:
- \setbeamertemplate{navigation symbols}{}
- \setbeamertemplate{footline}[page number]
---
If your beamer version is reasonable up to date (>= v3.48), you can adjust the format of the frame numbers while keeping the footline of your chosen beamer theme unchanged otherwise.
---
output:
beamer_presentation:
theme: "Berlin"
keep_tex: true
header-includes:
- \setbeamertemplate{page number in head/foot}[totalframenumber]
---
test
Try to put the lines below into the template
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
~/Library/R/3.1/library/rmarkdown/rmd/beamer/default.tex

Resources