[Tex/LaTex] Citations in tabular environment not working

citingmarkdownrtables

I'm writing my thesis with Rmarkdown but with a lot of LaTeX mixed in. When I compile the document, all my citations appear just fine, but those in my table appear as [?]. I have tested different types of tables, but even a very simple table does not manage to show the citations:

---
bibliography: library.bib
csl: conservation-biology.csl
output: pdf_document
---

\begin{center}
\begin{tabular}{ c c c }
 cell1 & cell2 & \cite{Saunders2007} \\ 
 cell4 & cell5 & cell6 \\  
 cell7 & cell8 & cell9    
\end{tabular}
\end{center}

I have also tried Rmarkdown's citing format instead of \cite{}: [@Saunders2007], but that shows up unformatted.

Am I missing something that should be done when citing inside tables?

Best Answer

The table is based on LaTeX. Using Rmarkdown inside the table environment is never going to work, as it doesn't recognise Rmarkdown. Using LaTeX citations is logical idea, however, LaTeX citations don't work the same way Rmarkdown citations work. This is an Rmarkdown document and doesn't provide any instructions for LaTeXtables. As you can see, the YAML header provides Rmarkdown citation instructions:

---
bibliography: library.bib
csl: conservation-biology.csl
---

With the bibliography file (.bib) and citation style (.csl) specified. The citations are read by pandoc (that's what knitr uses to convert .rmd files to .md files) and turned into plain text in the .md file. To get a latex/pdf document, the .md file is further converted to a .tex file using pdflatex. The Rmarkdown citation [@Saunders2007] is now written plain text.

It is better to use the LaTeX citation \cite{Saunders2007} instead, as this will be recognized as LaTeX and therefore LaTeX will try to look up the citation. However, no reference library is given for LaTeXcitations. To do this, one needs to write \bibliography{library}{} at the bottom of the document.

However, we want the citation styles to be the same. I'm using conservation-biology.csl here. Unfortunately, latex/bibtex doesn't use .csl files but .bst files (using \bibliographystyle{stylename} at the bottom of the document). As there are way more .csl files than .bst available (check this massive .csl repo), it's unlikely that your specific Rmarkdown citation style can also be found as .bst file.

Of course, you can write your own .bst file, but for the number of citations that are contained in the table it might be easier to just hand write them. To make sure they appear in the references list, use this:

---
nocite: | 
  @Saunders2007
...
Related Question