[Tex/LaTex] Is it possible to use the LaTeX citation command “\cite{key}” when using knitr in Rstudio

citingmarkdownrstudio

I've been looking for a way to use the \cite{AuthorYear} or \citep{AuthorYear} command on my markdown document on Rstudio instead of the [@AuthorYear], but have not managed to find any solutions here. Does anyone have a tip or resource to look for?

The reason for this is that I need to render word files for others to edit during the review process, but I would like to produce the final document with a LaTeX template for journal submission. Having the citations in this format would make for a much faster document transfer.

So far I have tried this in my markdown header to no avail:

 ---
title: "BEACH Formalisms"
header-includes: 
  \usepackage{mathtools}
  \usepackage{natbib}
output:
  pdf_document: default
  word_document: default
bibliography: library.bib
---

Test:

My citation text \citep{AuthorYear1, AuthorYear2}.

The result:

My citation text (??).

Namely: citations brackets do appear in the text, but the authors are just questions marks.

Do I need to declare a citation format or something else in the header? If so, appreciate your help!?

Thanks!

Best Answer

The "use a different approach" solution. As DG' mentiones one cannot use LaTeX commands in Word output. However, one can use standard RMarkdown syntax and let pandoc handle the translation to LaTeX:

---
title: "BEACH Formalisms"
header-includes: \usepackage{mathtools}
output:
  pdf_document:
    citation_package: natbib
    keep_tex: yes
  word_document: default
bibliography: packages.bib
---

```{r include=FALSE}
# automatically create a bib database for R packages
knitr::write_bib(c(
  .packages(), 'knitr', 'rmarkdown'
), 'packages.bib')
```


My citation text [@R-knitr; @R-base].

With this you will have a .tex file corresponding to your .Rmd file which you can base your journal submission on.

Related Question