将 r-markdown 编织成 PDF 时,有没有办法将 LaTeX 引用键保留在 .tex 文件中

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

我正在用 r-markdown 写一篇文章,并希望保留 .tex 文件以供通过

提交
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

一切都可以很好地编织成 PDF,甚至 .tex 文件看起来也不错,但参考键消失了。 有没有办法自动将引文键从 r-markdown 文件(如 [@smith1995])转换为 .tex 文件(如 \cite{smith1995})并创建相应的引用?

我 运行 R version 4.0.3pandoc 2.5 x86_64-pc-linux-gnu (64-bit)

所有 R 包都更新到最新版本:

 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

我找到了解决方案...更改引文包有效。默认 pandoc-citeproc 似乎不会在 .tex 文件中创建引用键,但 natbib 会。我只是将其更改为:

pdf_document:
  keep_tex: yes
  latex_engine: xelatex
  citation_package: natbib

现在 .Rmd 引用,如 [@Smith1995],在相应的 .tex 文件中被转换为 \citep{Smith1995}。 另一个选项可能是 biblatex.