问题
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:
- compile the article tex and SuppMat tex a first time using rmarkdown::render()
- 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)
- 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