Problem: you’re working on a paper in LaTex using Overleaf and you have two separate *.tex files — one for your manuscript, and a second for your supplementary information — and you want to be able to call ‘Supplementary Figure 1’ from the main text.

two separate *.tex documents

If you were working only within one *.tex file, this would be super easy. You’d just reference your figure and tables using the \ref command:

Thinner cortex within a region encompassing portions of the right cingulate gyrus (isthmus), precuneus, and lingual gyrus was associated with higher visual reliance scores (Fig. \ref{fig:Bal_CT_Result}; Table \ref{table:CT_Bal}).

This compiles nicely, correctly naming these as Fig. 3 and Table 3, and updating these figure and table numbers as the text is updated (e.g., to Fig. 2 and Table 2 if I remove previous figures and tables):

screenshot of compiled PDF, which correctly names Fig. 3 and Table 3

However, if you also want to name Supplemental Figure 1 in the main text (which I’ve labeled “Supp1”), which is defined in the Supplemental_Info.tex file and not the Manuscript_Text.tex file, without additional code, you run into some problems. This call:

Thinner cortex within a region encompassing portions of the right cingulate gyrus (isthmus), precuneus, and lingual gyrus was associated with higher visual reliance scores (Fig. \ref{fig:Bal_CT_Result}; Table \ref{table:CT_Bal}; Supplemental Fig. \ref{fig:Supp1}).

…compiles to question marks:

oh no, the dreaded question marks!

While I could’ve just manually numbered my 3 supplemental figures and moved on with my life, I chose the rabbit hole. I wanted to set up my LaTex documents so that numbering (and renumbering) would happen automatically — to make it easier should I ever have a larger number of supplemental figures to reference, or a document early in the writing stages where supplemental figures and tables were bound to be reordered, removed, added, etc.

Solution: after a deep LaTex forum dive, the solution turned out to be relatively painless:

The code within the text remains the same:

Supplemental Fig. \ref{fig:Supp1}

First, you need to add one extra file to your workspace called latexmkrc that contains the following code. Note: don’t save this as a *.tex file, just remove the file extension completely.

add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );

sub makeexternaldocument {
    if (!($root_filename eq $_[0]))
    {
        system( "latexmk -cd -pdf \"$_[0]\"" );
    }
}

Next, you just need to add the following to the header section of your document, changing the call to “Supplemental_Info” in \myexternaldocument{Supplemental_Info} to whatever your supplemental information tex file is called:

% Edit to ref between main text & supplemental material
\usepackage{xr}
\makeatletter
\newcommand*{\addFileDependency}[1]{% argument=file name and extension
  \typeout{(#1)}
  \@addtofilelist{#1}
  \IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother

\newcommand*{\myexternaldocument}[1]{%
    \externaldocument{#1}%
    \addFileDependency{#1.tex}%
    \addFileDependency{#1.aux}%
}

\myexternaldocument{Manuscript_Text}

This works, yay!

fix works; yay!

If you’d like to also be able to call ‘main text’ figures and/or tables in the supplemental file, add the same code to the header of your supplemental information file, but change \myexternaldocument{Supplemental_Info} to call the main text file instead (e.g., \myexternaldocument{Manuscript_Text}).

% Edit to ref between main text & supplemental material
\usepackage{xr}
\makeatletter
\newcommand*{\addFileDependency}[1]{% argument=file name and extension
  \typeout{(#1)}
  \@addtofilelist{#1}
  \IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother

\newcommand*{\myexternaldocument}[1]{%
    \externaldocument{#1}%
    \addFileDependency{#1.tex}%
    \addFileDependency{#1.aux}%
}

\myexternaldocument{Supplemental_Info}

Caveat: This worked for me in two different instances: when using the elsarticle LaTex template, and when using the frontiers LaTex template and implementing both via Overleaf. I’m not sure how these fixes would play with other templates or with other LaTex builds.

Note: A different help post said that you also need to add an additional file to your workspace called latexmkrc that contains the following code; however, this was not necessary for me to get the above fix to work.

Archives