knitr/rmarkdown/Latex: How to cross-reference figures and tables in 2 different pdf files?

别等时光非礼了梦想. 提交于 2019-12-10 19:49:13

问题


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

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. compile the article tex and SuppMat tex a first time using rmarkdown::render()
  2. compile these tex files from the command line in order to keep the corresponding .aux file with their references (missing references won't be resolved at this time)
  3. recompile the 2 tex files from the command line another time where all references should now be resolved

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?

Thank you very much for your help!


回答1:


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

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")

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 :)



来源:https://stackoverflow.com/questions/52531637/knitr-rmarkdown-latex-how-to-cross-reference-figures-and-tables-in-2-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!