4

I have the following code in my main.tex

\documentclass[a4paper]{report}
\usepackage{xr} 
\usepackage{subfiles} % Best loaded last in the preamble
\begin{document}
\chapter{Sobolev}
\subfile{sobolev}

\chapter{functional} \label{appendix:functional}
\end{document}

and the following in my subfile which is sobolev.tex

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

\begin{document}

The functional analysis terminology may be 
referred to in Appendix \ref{appendix:functional}.

\end{document}

When compiling the subfile, the reference doesn't show up.

A similar question was asked here (Unable to reference figure from one subfile to another subfile when compiling ONLY the subfile). I've tried compiling main followed by compiling sobolev, it still shows the same error.

4
  • Your MWE works fine for me (the reference shows up). Try updating your LaTeX distribution and running your MWE again (first compiling main.tex, then sobolev.tex).
    – schtandard
    Jun 16, 2022 at 12:20
  • As a side note, the documentation states that \externaldocument should go into main.tex, not into sobolev.tex, but both versions work for me.
    – schtandard
    Jun 16, 2022 at 12:21
  • 1
    main.tex is compiled without issues. It doesn't find cross reference if I compile sobolev.tex. I believe it means it doesn't really work.
    – Celdor
    Jun 16, 2022 at 12:34
  • I can confirm it's working now but only if I run both files from command line. Normally, I use Atom with a plugin. I also put all auxiliary and output files in the custom folder: out/. If I run the following line: latexmk -pdflua -auxdir=out/ -outdir=out/ sobolev.tex, assuming I compiled the main.tex before, I get the correct referencing. So in my case, it was a problem with the configuration and the fact LaTeX couldn't find required files.
    – Celdor
    Jun 17, 2022 at 13:09

1 Answer 1

1

Replace \externaldocument{main.tex} by \externaldocument{main}, then it works.

Actually, when running LaTeX on sobolev.tex, the log file contains the complaint:

Package xr Warning: 
No file main.tex.aux
LABELS NOT IMPORTED.
 on input line 2.

Apparently, the xr package just appends .aux to the string provided by \externaldocument and takes this as the filename with the label information.

5
  • Sorry, I commented in a wrong place.
    – Celdor
    Jun 17, 2022 at 13:09
  • When I use main instead of main.tex, sobolev.aux file shows an error saying labels of references are multiply defined. I'm using bibtex.
    – Abhinav
    Jun 21, 2022 at 6:14
  • 1
    @Abhinav This has nothing to do with subfiles but how you use the xr package. In the preamble of your main file, add the lines \usepackage{xr}\usepackage{subfiles}\externaldocument[M-]{\subfix{main}}, and remove \usepackage{xr} and \externaldocument from your subfiles. If you define a label xxx somewhere (via \label{xxx}), then \ref{M-xxx} will give you the value like when compiling the main document. See the documentation of the subfiles package, section 4.2
    – gernot
    Jun 21, 2022 at 9:01
  • 1
    The multiply defined errors are the result of loading main.aux (via \externaldocument) as well as sub.aux. Any label defined in sub.tex will appear in both aux files (hence it seems multiply defined). The optional M- parameter tells LaTeX to prefix the labels loaded from main.aux with M- such that you (and LaTeX) can distinguish the value the label has in the main document (\ref{M-xxx}) and in the subfile (\ref{xxx}`).
    – gernot
    Jun 21, 2022 at 9:11
  • Thanks! that resolved the issue!
    – Abhinav
    Jun 22, 2022 at 11:03

You must log in to answer this question.

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