If I had a table stored in a data frame:

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

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'}
tbl <- data.frame(Name=c("A", "B"), Source = c("\\cite{a}", "\\cite{b}"))
xtab <- xtable(tbl)
print(xtab, sanitize.text.function = function(x) {x})


 & Name & Source \\ 
 1 & A & \cite{a} \\ 
 2 & B & \cite{b} \\ 
  • This solution is very elegant and works beautifully! Many thanks! – chao Apr 24 '17 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.