R Markdown转PDF时,有没有办法在.tex文件中保留LaTeX引用键?

3
我正在使用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版本4.0.3pandoc 2.5x86_64-pc-linux-gnu(64位)上。
所有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

你在 YAML 部分中添加了 bibliography 关键字吗?https://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html - Carlos Luis Rivera
@CarlosLuisRivera 是的,我试了一下,pdf(以及.html和.docx)输出看起来很好,引用也正确。只是在.tex文件中没有引用键。这里的引用是纯文本,如(Smith 1995),而不是\cite{smith1995} - harald
您介意编辑帖子并添加YAML的完整规范以及R、其包和Pandoc的版本信息吗? - Carlos Luis Rivera
1
截至2021年2月27日,最新的pandoc版本是2.11.4。你的版本可能太旧了,无法获取LaTeX的引用命令(我认为Pandoc Ver. 2.5使用pandoc-citeproc,已经被弃用,并在从.[R]md生成.tex时标记@mention,如果我理解正确的话...?)。R Markdown Cookbook的一章指导您使用未与RStudio IDE捆绑的Pandoc版本。请问升级Pandoc后问题是否仍然存在? - Carlos Luis Rivera
遗憾的是,问题仍然存在... 我也尝试了xelatex和lualatex,但没有任何区别。 - harald
1个回答

3

我找到了解决方案... 更改引用包即可。默认的pandoc-citeproc好像无法在.tex文件中创建引用键,但是natbib可以。我只需使用以下命令进行更改:

pdf_document:
  keep_tex: yes
  latex_engine: xelatex
  citation_package: natbib

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接