{"id":11983,"date":"2024-11-28T16:03:50","date_gmt":"2024-11-28T16:03:50","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=11983"},"modified":"2024-11-28T16:14:38","modified_gmt":"2024-11-28T16:14:38","slug":"cross-referencing-across-latex-documents-in-one-project","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2024\/11\/cross-referencing-across-latex-documents-in-one-project\/","title":{"rendered":"Cross referencing across LaTeX documents in one project"},"content":{"rendered":"\n<p>A common scenario we come across is that we have a main manuscript document and a supplementary information document, each of which have their own sections, tables and figures. The question then becomes &#8211; how do we effectively cross-reference between the documents without having to tediously count all the numbers ourselves every time we make a change and recompile the documents?  <br><\/p>\n\n\n\n<p>The answer: cross referencing! <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>This solution is based on the tex stackexchange thread found here: <a href=\"https:\/\/tex.stackexchange.com\/questions\/62142\/latexmk-with-external-references\">https:\/\/tex.stackexchange.com\/questions\/62142\/latexmk-with-external-references<\/a><\/p>\n\n\n\n<p>Create two *.tex files that will contain the source code for you documents. For example: main.tex for your manuscript and SI.tex for your supplementary information. <br><br>Add the following code to the header of both files. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\usepackage{xr}\n\\makeatletter\n\n\\newcommand*{\\addFileDependency}[1]{\n    \\typeout{(#1)}\n    \\@addtofilelist{#1}\n    \\IfFileExists{#1}{}{\\typeout{No file #1.}}\n}\\makeatother\n\n\\newcommand*{\\myexternaldocument}[1]{%\n    \\externaldocument{#1}%\n    \\addFileDependency{#1.tex}%\n    \\addFileDependency{#1.aux}%\n}\n<\/pre>\n\n\n\n<p>Then, create a new file called &#8220;latexmkrc&#8221; and paste the following code into it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$sub_doc_output = 'output-subdoc';\n\n@sub_doc_options = ();\n\npush @sub_doc_options, '-pdf'; # Use pdflatex for compilation of external documents.\n# Replace '-pdf' by '-pdfdvi', 'pdfxe', or 'pdflua' if needed.\n\npush @file_not_found, '^No file\\\\s*(.+)\\s*$';\n\nadd_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );\nsub makeexternaldocument {\n    if ( $root_filename ne $_[0] )  {\n        my ($base_name, $path) = fileparse( $_[0] );\n        pushd $path;\n        my $return = system \"latexmk\",\n                            @sub_doc_options,\n                            \"-aux-directory=$sub_doc_output\",\n                            \"-output-directory=$sub_doc_output\",\n                            $base_name;\n        if ( ($sub_doc_output ne ' ') &amp;amp;&amp;amp; ($sub_doc_output ne '.') ) {\n\n             rdb_add_generated( \"$sub_doc_output\/$base_name.aux\" );\n             copy \"$sub_doc_output\/$base_name.aux\", \".\";\n        }\n        popd;\n        return $return;\n   }\n}<\/pre>\n\n\n\n<p>Finally, add a command declaring the external document before the \\begin{document} statement in each of the files. So in main.tex add: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\myexternaldocument{SI}<\/pre>\n\n\n\n<p>And to SI.tex add: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\myexternaldocument{main}<\/pre>\n\n\n\n<p>After recompiling you can now reference labels across documents using the usual: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\ref{my_item_label}<\/pre>\n\n\n\n<p>Here is a minimal working example with main.tex: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\documentclass{article}\n\\usepackage{graphicx} \n\n\\usepackage{xr}\n\\makeatletter\n\n\\newcommand*{\\addFileDependency}[1]{\n\\typeout{(#1)}\n\\IfFileExists{#1}{}{\\typeout{No file #1.}}\n}\\makeatother\n\n\\newcommand*{\\myexternaldocument}[1]{%\n\\externaldocument{#1}%\n\\addFileDependency{#1.tex}%\n\\addFileDependency{#1.aux}%\n}\n\n\\myexternaldocument{SI}\n\n\\title{mini cross reffing example}\n\\author{npq15 }\n\\date{November 2024}\n\n\\begin{document}\n\n\\maketitle\n\n\\section{Introduction}\n\n\\begin{figure}\n    \\centering\n   \n    \\caption{Caption}\n    \\label{fig:main}\n\\end{figure}\n\nI am referencing a SI figure: \\ref{fig:SI}\n\n\\end{document}\n<\/pre>\n\n\n\n<p>&#8230; and SI.tex:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"latex\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\documentclass{article}\n\\usepackage{graphicx} % Required for inserting images\n\\usepackage{lipsum}\n\\usepackage{xr}\n\\makeatletter\n\n\\newcommand*{\\addFileDependency}[1]{\n    \\typeout{(#1)}\n    \\@addtofilelist{#1}\n    \\IfFileExists{#1}{}{\\typeout{No file #1.}}\n}\\makeatother\n\n\\newcommand*{\\myexternaldocument}[1]{%\n    \\externaldocument{#1}%\n    \\addFileDependency{#1.tex}%\n    \\addFileDependency{#1.aux}%\n}\n\n\\myexternaldocument{main}\n\n\\title{mini cross reffing example SI}\n\\author{npq15 }\n\\date{November 2024}\n\n\n\\begin{document}\n\n\\maketitle\n\n\\section{SI}\n\n\\begin{figure}\n    \\centering\n    \n    \\caption{SI figure}\n    \\label{fig:SI}\n\\end{figure}\n\nI am referencing a main figure: \\ref{fig:main}\n\n\\end{document}\n<\/pre>\n\n\n\n<p>Happy LaTEXing \ud83d\ude42 <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common scenario we come across is that we have a main manuscript document and a supplementary information document, each of which have their own sections, tables and figures. The question then becomes &#8211; how do we effectively cross-reference between the documents without having to tediously count all the numbers ourselves every time we make [&hellip;]<\/p>\n","protected":false},"author":107,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,361,296,14,463,190,48,15],"tags":[],"ppma_author":[660],"class_list":["post-11983","post","type-post","status-publish","format-standard","hentry","category-code","category-data-science","category-hints-and-tips","category-howto","category-latex","category-public-outreach","category-publication","category-technical"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":660,"user_id":107,"is_guest":0,"slug":"nele","display_name":"Nele Quast","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/3469d0fe5a77ce036cb6cc153e6e8ac8363f0c224abd943442ce53730b4d71ac?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/11983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/107"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=11983"}],"version-history":[{"count":4,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/11983\/revisions"}],"predecessor-version":[{"id":12006,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/11983\/revisions\/12006"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=11983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=11983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=11983"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=11983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}