1

I have the following files in Overleaf:

main.tex in root directory:

\documentclass[conference]{IEEEtran}
\usepackage{xr}
\usepackage{subfiles}

\begin{document}

\section{Methodology} \label{methodology}
\subfile{section/methodology}

\section{Results and Discussion} \label{discussion}
\subfile{section/discussion}

\end{document}

and 2 subfiles in directory section:

discussion.tex, which as a figure with label fig:myfig:

\documentclass[../main.tex]{subfiles}
\begin{document}

\begin{figure}
\includegraphics[width=0.45\textwidth]{myfig.pdf}
\caption{My Figure.}
\label{fig:myfig}
\end{figure}

\end{document}

Then, in my other subfile methodology.tex, I want to refer to fig:myfig:

\documentclass[../main.tex]{subfiles}
\externaldocument{discussion}

\begin{document}

I want to refer to Figure \ref{fig:myfig}

\end{document}

If I compile main.tex, the figure is referenced just fine. But if I only compile methodology.tex, I got ?? in Figure reference. How do I reference this correctly?

6
  • Does this help? overleaf.com/learn/how-to/…
    – imnothere
    Commented Nov 13, 2019 at 1:03
  • that shows how to reference between 2 independent files. In my case, 2 files are subfiles and linked to a main.tex outside
    – hydradon
    Commented Nov 13, 2019 at 3:16
  • The method works for your example; see overleaf.com/read/bckpkfcfxntb As long as you want to use xr you need the extra helper code and latexmkrc to get the .aux for methodology.tex when you're compiling (latexmk) from discussion.tex as a subfile. The technique actually comes from tex.stackexchange.com/questions/62142/…
    – imnothere
    Commented Nov 13, 2019 at 6:24
  • You have to run LaTeX first on discussion.tex and then on methodology.tex. On my local installation (not Overleaf) your code works fine (except that one has to add \usepackage{graphicx} in main and replace myfig.pdf by example-image.
    – gernot
    Commented Nov 13, 2019 at 14:23
  • 1
    Done. Note that you have to add @gernotsaysReinstateMonica to your comment in order to notify me about your answer. Otherwise I will notice it only if I come back by chance.
    – gernot
    Commented Nov 14, 2019 at 12:47

1 Answer 1

1

On a local TeX installation, the code of the example in the question (almost) works. The required changes have nothing to do with the packages xr and subfiles, but just make the loading of the image work. So if there are problems on Overleaf, they are caused by the specific setting there, not by the packages.

The file structure is as follows:

main.tex
section/discussion.tex
section/methodology.tex

Typesetting main.tex and section/discussion.tex works as usual: run latex twice.

To typeset section/methodology.tex, you have first to run latex once on discussion.tex (this produces discussion.aux, which holds the labels from discussion.tex), and then once on methodology.tex (this run reads discussion.aux):

cd section
pdflatex discussion
pdflatex methodology

Here is the contents of the three files.

% main.tex
\listfiles % to get the list of loaded packages with version information
\documentclass[conference]{IEEEtran}
\usepackage{graphicx}
\usepackage{xr}
\usepackage{subfiles}
\begin{document}
\section{Methodology} \label{methodology}
\subfile{section/methodology}
\section{Results and Discussion} \label{discussion}
\subfile{section/discussion}
\end{document}

% section/discussion.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\begin{figure}
\includegraphics[width=0.45\textwidth]{example-image}
\caption{My Figure.}
\label{fig:myfig}
\end{figure}
\end{document}

% section/methodology.tex
\documentclass[../main.tex]{subfiles}
\externaldocument{discussion}
\begin{document}
I want to refer to Figure \ref{fig:myfig}
\end{document}

For reference, here is the list of loaded packages with information on the versions used. You obtain it by adding \listfiles as first line in main.tex. The list appears at the end of the log output.

 *File List*
IEEEtran.cls    2015/08/26 V1.8b by Michael Shell
  ot1ptm.fd    2001/06/04 font definitions for OT1/ptm.
graphicx.sty    2017/06/01 v1.1a Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2019/11/01 v1.3d Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  pdftex.def    2018/01/08 v1.0l Graphics/color driver for pdftex
      xr.sty    2019/07/22 v5.05 eXternal References (DPC)
subfiles.sty    2019/10/30 v1.5 Multi-file projects (package)
  import.sty    2009/03/23  v 5.1
supp-pdf.mkii
epstopdf-base.sty    2016/05/15 v2.6 Base part for package epstopdf
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
  grfext.sty    2016/05/16 v1.2 Manage graphics extensions (HO)
kvdefinekeys.sty    2016/05/16 v1.4 Define keys (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
kvoptions.sty    2016/05/16 v3.12 Key value format for package options (HO)
kvsetkeys.sty    2016/05/16 v1.17 Key value parser (HO)
etexcmds.sty    2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
ifluatex.sty    2019/10/25 v1.5 ifluatex legacy package. Use iftex instead.
   iftex.sty    2019/11/04 v1.0b TeX engine tests
pdftexcmds.sty    2019/07/25 v0.30 Utility functions of pdfTeX for LuaTeX (HO)
   ifpdf.sty    2019/10/25 v3.4 ifpdf legacy package. Use iftex instead.
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
methodology.tex
discussion.tex
example-image.pdf    Graphic file (type pdf)
 ***********

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .