knitr/rmarkdown/Latex:如何交叉引用2个不同pdf文件中的图形和表格? [英] knitr/rmarkdown/Latex: How to cross-reference figures and tables in 2 different pdf files?

查看:339
本文介绍了knitr/rmarkdown/Latex:如何交叉引用2个不同pdf文件中的图形和表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用rmarkdown在RStudio中完全撰写科学文章和相关的补充材料.

I'm trying to write a scientific article and the associated supplementary materials entirely in RStudio with rmarkdown.

显然,预订是在文件之间进行交叉引用的一种方式( https://stackoverflow.com/a /38884378/576684 ),但我也希望能够引用另一个pdf中的一个pdf中产生的数字.

It seems clear that book down is the way to go to cross-reference between files (https://stackoverflow.com/a/38884378/576684), but I also would like to be able to reference figures produced in one pdf in the other pdf.

尽管我的乳胶随着时间的流逝已经很生锈,但我认为可以通过以下方式实现:

Although my latex has got quite rusty with time, I imagine it could be achieved as follows:

  1. 第一次使用rmarkdown :: render()编译文章tex和SuppMat tex
  2. 从命令行编译这些tex文件,以便将相应的.aux文件与它们的引用保持在一起(丢失的引用目前无法解决)
  3. 另一次从命令行重新编译2 tex文件,现在应该解析所有引用

这是一种合理的方法吗?我可以忽略一些更简单的东西吗? 无论如何,它都需要:

Is it a reasonable way to do it? am I overlooking something simpler? In any case, it requires:

  • a different numbering of figures in each pdf file (covered by https://stackoverflow.com/a/51337664/576684)
  • to prevent rmarkdown from trashing the .aux files (it seems that pandoc doesn't allow this, hence the need to create the aux file using standalone latex)
  • to tell latex to use the additional .aux file if it is found (probably using header-includes: in the YAML header). how can I achieve that?

非常感谢您的帮助!

推荐答案

事实证明,xr包是一种解决方法:

It turns out that the xr package is one way to go: https://texblog.org/2016/08/23/adding-references-from-an-external-file/

所以这可以在R中使用:

so this works from R:

rmarkdown::render("myarticle_ms.Rmd", 
                  bookdown::pdf_book(base_format=rticles::plos_article),
                  clean=FALSE)

rmarkdown::render("myarticle_SM.Rmd", 
                  bookdown::pdf_book(base_format=rticles::plos_article),
                  clean=FALSE)

tinytex::pdflatex("myarticle_ms.tex", clean=FALSE)
tinytex::pdflatex("myarticle_SM.tex", clean=FALSE)

tinytex::pdflatex("myarticle_ms.tex")
tinytex::pdflatex("myarticle_SM.tex")

在myarticle_ms.Rmd的YAML标头中包含以下内容(以及对应的一个SuppMat文件标头):

with the following in the YAML header of myarticle_ms.Rmd (and the corresponding one the SuppMat file header):

header-includes:
  \usepackage{xr} \externaldocument{myarticle_SM}

希望这会使其他一些人的生活更轻松:)

Hope it makes life easier for a few others :)

这篇关于knitr/rmarkdown/Latex:如何交叉引用2个不同pdf文件中的图形和表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆