# Is it possible to use cite inside xtable?

If I had a table stored in a data frame:

library(xtable)
tbl <- data.frame(Name=c("A", "B"),Source=c("\\cite{a}", "\\cite{b}"))
tab <- xtable(tbl)
print(tab)


how would I go about producing a tex file that would replace \cite{a} with a bib reference? The final output would look something like:

Name Source
A    [1]
B    [2]


Unfortunately, knitr by default replaces backslashes with $\backslash$. Is it possible to prevent this behaviour?

In the function print.xtable, there is different sanitize function to add markup in the table. You can have a look at the part 4.1 Markup in table in the The xtable Gallery vignette ( vignette("xtableGallery") )

For a .Rmd document, this code works:

---
output: pdf_document
---

{r, results='asis'}
library(xtable)
tbl <- data.frame(Name=c("A", "B"), Source = c("\\cite{a}", "\\cite{b}"))
xtab <- xtable(tbl)
print(xtab, sanitize.text.function = function(x) {x})



output

\begin{table}[ht]
\centering
\begin{tabular}{rll}
\hline
& Name & Source \\
\hline
1 & A & \cite{a} \\
2 & B & \cite{b} \\
\hline
\end{tabular}
\end{table}

• This solution is very elegant and works beautifully! Many thanks! – chao Apr 24 '17 at 12:32