Is there a way to keep LaTeX citation keys in .tex file when knitting r-markdown to PDF

Viewed 224

I am writing an article in r-markdown and would like to keep the .tex file for the submission via

title: "My title"
author: "My name"
date: "`r format(Sys.time(), '%d %B, %Y')`"
abstract: "My abstract"
output:
  html_document:
    df_print: paged
  word_document: default
  pdf_document:
    keep_tex: yes
bibliography: bibfile.bib

Everything works fine knitting to PDF and even the .tex file looks good, but the reference keys disappeared. Is there a way to automatically convert the citation keys from the r-markdown file, like [@smith1995] to the .tex file, like \cite{smith1995} and create the corresponding references?

I am running R version 4.0.3 and pandoc 2.5 on x86_64-pc-linux-gnu (64-bit)

All R packages are updated to the most recent version:

 attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] texreg_1.37.5

loaded via a namespace (and not attached):
[1] httr_1.4.2            viridis_0.5.1         tidyr_1.1.2        tidygraph_1.2.0       viridisLite_0.3.0     splines_4.0.3        
[7] foreach_1.5.1         ggraph_2.0.3          network_1.16.1        assertthat_0.2.1      yaml_2.2.1            robustbase_0.93-6    
[13] ggrepel_0.8.2         backports_1.2.1       pillar_1.5.0          lattice_0.20-41       quantreg_5.85         glue_1.4.2           
[19] digest_0.6.27         polyclip_1.10-0       colorspace_1.4-1      htmltools_0.5.1.1     cowplot_1.1.1         Matrix_1.3-2         
[25] plyr_1.8.6            conquer_1.0.2         lpSolve_5.6.15        pkgconfig_2.0.3       broom_0.7.5           SparseM_1.78         
[31] purrr_0.3.4           ergm_3.11.0           scales_1.1.1          tweenr_1.0.1          sna_2.6               MatrixModels_0.4-1   
[37] ggforce_0.3.2         tibble_3.0.6          mgcv_1.8-33           generics_0.1.0        farver_2.0.3          ggplot2_3.3.3        
[43] ellipsis_0.3.1        cli_2.3.1             splitstackshape_1.4.8 magrittr_1.5          crayon_1.3.4          statnet.common_4.4.1 
[49] evaluate_0.14         mcmc_0.9-7            fansi_0.4.1           doParallel_1.0.16     nlme_3.1-152          MASS_7.3-53          
[55] rem_1.3.1             vegan_2.5-6           tools_4.0.3           data.table_1.14.0     lifecycle_0.2.0       matrixStats_0.58.0   
[61] stringr_1.4.0         MCMCpack_1.5-0        trust_0.1-8           munsell_0.5.0         cluster_2.1.0         compiler_4.0.3       
[67] rlang_0.4.10          grid_4.0.3            iterators_1.0.13      rstudioapi_0.11       igraph_1.2.6          rmarkdown_2.7        
[73] gtable_0.3.0          codetools_0.2-18      DBI_1.1.1             reshape2_1.4.4        graphlayouts_0.7.1    rDNA_2.1.18          
[79] R6_2.5.0              gridExtra_2.3         rle_0.9.2             knitr_1.31            dplyr_1.0.4           utf8_1.1.4           
[85] permute_0.9-5         rJava_0.9-13          stringi_1.5.3         parallel_4.0.3        Rcpp_1.0.6            vctrs_0.3.6          
[91] xfun_0.21             DEoptimR_1.0-8        tidyselect_1.1.0      coda_0.19-4
1 Answers

I found the solution... Changing the citation-package worked. The default pandoc-citeproc seems not to create the citation-key in the .tex file, but natbib does. I simply changed it with:

pdf_document:
  keep_tex: yes
  latex_engine: xelatex
  citation_package: natbib

Now the .Rmd citations, like [@Smith1995], are converted to \citep{Smith1995} in the corresponding .tex file. Another option might be biblatex.

Related