# Conflicts between subfiles and caption packages

I am writing a paper that has an online appendix. The paper and appendix reference figures in each other. I'd like to be able to compile each of the documents as a separate .pdf file and maintain the figure referencing. The subfiles package seems to work pretty well for this. But, there is a tiny flaw in that it seems to have difficulties when I also use the caption package in order to modify, for example, how figure numbers appear. In essence, what happens is that if I also load the caption package, then the hyperlinks in my appendix no longer work properly. When I compile the document, I get the following Warning:

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.


Unfortunately, if I re-run the compilation, it breaks other references. Below is a minimal working example with more specific explanation of the procedure I'm trying to follow.

Main.tex

\documentclass{report}
\usepackage{subfiles}
\usepackage{xr-hyper}
\usepackage{hyperref}
\usepackage[notablist,nofiglist,nomarkers]{endfloat}

\usepackage{caption}

\begin{document}

\subfile{Body.tex}

\setcounter{page}{1}
\setcounter{figure}{0}
\setcounter{postfigure}{0}

\renewcommand{\thepostfigure}{\Alph{section}.\arabic{postfigure}}
\renewcommand{\thefigure}{\Alph{section}.\arabic{figure}}

\subfile{Appendix.tex}

\end{document}


Body.tex

\documentclass[Main.tex]{subfiles}
\begin{document}

\section{Section 1}
\label{sec:Section1}

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Body Figure 1}
\label{body_fig1}
\end{figure}

As I show in \autoref{fig1} in \autoref{sec:Online_Appendix} of the appendix ...

\processdelayedfloats

\end{document}


Appendix.tex

\documentclass[Main.tex]{subfiles}
\begin{document}

\pagebreak
\appendix
\section{Online Appendix}
\label{sec:Online_Appendix}

My discussion in \autoref{sec:Section1} is informed by \autoref{fig1} and \autoref{fig2} and \autoref{fig3}.

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Appendix Figure 1}
\label{fig1}
\end{figure}

\pagebreak

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Appendix Figure 2}
\label{fig2}
\end{figure}

\pagebreak

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Appendix Figure 3}
\label{fig3}
\end{figure}

\end{document}


Work Flow

1. I first compile Main.tex.
2. I then comment out the \subfile{Body.tex} and recompile Main.tex. This generates the proper text for the appendix, but the hyperlinks to the figures go to the wrong places in the document when clicked. This is where I get the warning about labels having changed.
3. If I recompile Main.tex again, the hyperlinks to the figures in the appendix now work, but the reference to the section in Body.tex now appears as ??

• All of this works fine if I don't have the caption package enabled.
• If I just try to compile either Body.tex or Appendix.tex separately, the cross references don't work correctly, which is why I use the procedure with the multiple separate compilations of Main.tex.

You are missing the external document parts from https://tex.stackexchange.com/a/378163/36296. If you want a reference in a subfile to another subfile, you have to specify this external document:

\documentclass{report}
\usepackage{subfiles}
\usepackage{xr-hyper}
\usepackage{hyperref}
\usepackage[notablist,nofiglist,nomarkers]{endfloat}
\usepackage{xstring}

\usepackage{caption}

\begin{document}

\subfile{body.tex}

\setcounter{page}{1}
\setcounter{figure}{0}
\setcounter{postfigure}{0}

\renewcommand{\thepostfigure}{\Alph{section}.\arabic{postfigure}}
\renewcommand{\thefigure}{\Alph{section}.\arabic{figure}}

\subfile{appendix.tex}

\end{document}


% !TeX root = body.tex
\documentclass[main.tex]{subfiles}

\IfEq{\jobname}{\detokenize{main}}{}{%
\externaldocument{appendix}
}

\begin{document}

\section{Section 1}
\label{sec:Section1}

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Body Figure 1}
\label{body_fig1}
\end{figure}

As I show in \autoref{fig1} in \autoref{sec:Online_Appendix} of the appendix ...

\processdelayedfloats

\end{document}


% !TeX root = appendix.tex
\documentclass[main.tex]{subfiles}

\IfEq{\jobname}{\detokenize{main}}{}{%
\externaldocument{body}
}

\begin{document}

\pagebreak
\appendix
\section{Online Appendix}
\label{sec:Online_Appendix}

My discussion in \autoref{sec:Section1} is informed by \autoref{fig1} and \autoref{fig2} and \autoref{fig3}.

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Appendix Figure 1}
\label{fig1}
\end{figure}

\pagebreak

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Appendix Figure 2}
\label{fig2}
\end{figure}

\pagebreak

\begin{figure}
\centering\rule{1cm}{1cm}
\caption{Appendix Figure 3}
\label{fig3}
\end{figure}

\end{document}


As the subfiles cyclically depend on each other, compilation order for the first compilation may be a bit tricky, but compiling body, appendix, body, appendix should probably do.

• Perfect, thanks! I also realized the xstring package is essential for this to work. Interestingly, this procedure is successful for me now when I compile in TeXShop, though not when using Sublime LaTeXing. Go figure. But that's hardly a cause for major concern at this point. – aireties Aug 10 '17 at 15:18
• @aireties Just a guess as I neither use TeXShop nor Sublime: Maybe sublime wanted to be too smart and compiled the wrong document? I know this problem from texstudio, that's why I explicitly tell it which document it should compile with % !TeX root = appendix.tex – user36296 Aug 10 '17 at 15:22
• Interesting, yeah, something like that could be up. Just putting that in and of itself into the files wasn't enough to get it to work with Sublime, but it could be something along those lines. – aireties Aug 10 '17 at 15:32
• FWIW, I had to put the \IfEq external document commands inside the \begin{document} to get it to work on KILE – arghdos May 17 '18 at 1:21