Including entire bibliographic entry in RMarkdown - equivalent of \fullcite? - latex

Is there a way to include an entire bibliographic entry in the main text of my Rmarkdown document? If I were using LaTeX, I'd use \fullcite{Smith2000}. And then it would print, wherever I wanted:
Smith, J. 2000. A nice book. Oxford: Oxford University Press.
Here's a sample .Rmd file
---
title: "hi"
author: "dmt"
date: "30/04/2020"
output: html_document
bibliography: bib.bib
biblio-style: apalike
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r cars}
summary(cars)
```
Here is the citation of a book I am reading:
#Smith2000
But I would like to put the whole reference here like this:
Smith, J. 2000. A nice book. Oxford: Oxford University Press.
How can I do this without writing it out?
And here's a sample .bib file:
#book{Smith2000,
Address = {Oxford},
Author = {Smith, J},
Date-Added = {2020-04-30 17:03:32 +0100},
Date-Modified = {2020-04-30 17:04:03 +0100},
Publisher = {Oxford University Press},
Title = {A nice book},
Year = {2000}}

This works fine for me:
Execute this chunk in your RMD file:
```{r, echo=FALSE}
biblio <- bibtex::read.bib("bib.bib")
```
Then you can use the following line in your script:
`r capture.output(print(biblio["Smith2000"]))`

Related

How do I render Greek characters in code chunks in Rmarkdown to pdf?

I use Rmarkdown to write python code and knit to pdf file for some reason...
I want to use and display greek characters like lambda in my code.
e.g.
def function(X,Y,λ,h):
# do stuff
return w
With default engine, it shows error message as below:
! LaTeX Error: Unicode character λ (U+03BB)
not set up for use with LaTeX.
Error: LaTeX failed to compile report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See report.log for more info.
Execution halted
If I change the latex engine to xelatex or lualatex, it can knit to a pdf but the greek character disappear. Like this:
def function(X,Y, ,h):
# do stuff
return w
Do folks have any idea how to address that? Really appreciate it if someone could help.
Edited: add a minimal reproducible example as asked by the comment.
---
title: "Minimal reproducible example"
date: "2022-11-08"
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
def function(X,Y,λ,h):
# do stuff
return w
```
You'll need to choose a font which does have Greek letters. For example with Arial (using Arial here because many people have it installed, choose a nicer looking font if you have it):
---
title: "Minimal reproducible example"
date: "2022-11-08"
output:
pdf_document:
latex_engine: xelatex
keep_tex: true
header-includes:
- \setmonofont{Arial}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
def function(X,Y,λ,h):
# do stuff
return w
```

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

Rmarkdown: Modify Figure X label to Figure SX in pdf or word output

I am trying to use Rmarkdown to automatically label Figure's caption, but instead of counting Figure 1, I want it to be Figure S1, i.e. just adding S there. An example here and here suggests that this is not possible using pdf output. Ok, I am fine with .doc file, but still my figure caption is not print out? I wonder what could be wrong?
Minimal example for R markdown:
---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
word_document:
fig_caption: yes
---
```{r, include=F}
library(captioner)
figS<- captioner(prefix = "Figure S", auto_space = TRUE, levels = 1, type = NULL, infix = ".")
figS("Figure S1", "Single-crystal X-ray structure of some text (1)", display=FALSE)
```
```{r Xray, fig.cap=figS("This is my figure description"), echo=FALSE}
plot(cars)
```
This prints correctly out Figure S1. But, now my actual figure description is missing?
My desired output is pdf, but if not, I can do with word. Thanks for suggestion how to fix this!
You can use some LaTeX commands under header-includes: in the YAML section, as follows:
---
title: "Untitled"
output: pdf_document
header-includes:
- \renewcommand{\figurename}{Figure S}
- \makeatletter
- \def\fnum#figure{\figurename\thefigure}
- \makeatother
---
```{r, fig.cap="This is my figure description", fig.height=3}
plot(pressure)
```
```{r, fig.cap="Another figure description", fig.height=3}
plot(iris$Sepal.Length, iris$Petal.Width)
```
(The argument fig.height in the R code chunks is not necessary; I used it only to get both plots in the same page and take the screenshot)

How can I use Latex' \newcommand in Rmd?

I would like to make the following work
---
title: "Untitled"
author: "SQC"
date: "21 September 2018"
output: html_document
---
\newcommand{\short}{AreallylongwordIhavetotypefrequently}
# My Test
I would like to write \short which does not work, $\short$ however is close...
Snippets do not work in Rmd plain text (= Rstudio's "Shift", see link below).
But I could not find a solution. It would be great if there is something around!
The following links are helpful, but didn't suggest a solution: pandoc doc, \newcommand in Rmd formula and RStudio snippets.
How about using R instead:
---
title: "Untitled"
author: "SQC"
date: "21 September 2018"
output: html_document
---
```{r, include = FALSE}
short <- "AreallylongwordIhavetotypefrequently"
```
# My Test
I would like to write `r short` instead ...
If you still need to define a \newcommand using LaTeX in Rmarkdown, you can do so by e.g.
Some text with the following equation:
\begin{equation} \newcommand{\matr}[1]{\mathbf{#1}}
\matr{Y} =
\begin{pmatrix}
\matr{y_1} \\
\matr{y_2} \\
\end{pmatrix}
\end{equation}
```{r}
y1 + y2
```
Note, the bm package must be loaded for this to work, e.g., by using this in the YAML header:
---
title: "My R Report"
author: "Me"
date: "2020-01-31"
header-includes:
- \usepackage{bm}
output:
pdf_document:
toc: true
number_sections: true
---

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.

Resources