# \pageref not working properly in main document with subfiles package and xr package

I have a main file setup with the subfiles package and the xr package. I am trying to make the \pageref command work properly for cross-references in the main file, but the page number it displays in the main file is not correct, although it is correct in the subfile. My setup in the main.tex is as follows:

 \documentclass[12pt,a4paper,twoside,openright,final]{memoir}
\usepackage{kpfonts}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{subfiles}
%
\providecommand{\main}{.}
%
\usepackage{xr}
\externaldocument[ch1-]{\main/chap/chapter1/ch1}
\externaldocument[ch2-]{\main/chap/chapter2/ch2}
%
\begin{document}
%... some other subfiles
\subfile{chap/chapter1/ch1.tex}
\subfile{chap/chapter2/ch2.tex}
\end{document}


Now in the file ch1.tex I have the following setup:

\providecommand{\main}{../../}
\documentclass[../../main.tex]{subfiles}
\begin{document}
\chapter{1}
Some text.... \newpage
\begin{figure}[h]
\centering
\includegraphics[width=0.7\textwidth]{picture}
\caption{Some picture}
\label{fig:picture}
\end{figure}
\end{document}


And then in the file ch2.tex I have the following:

\providecommand{\main}{../../}
\documentclass[../../main.tex]{subfiles}
\begin{document}
\chapter{2}
As shown in figure~\ref{ch1-fig:picture} on page~\pageref{ch1-fig:picture} the labelling works.
\end{document}


Now, when I compile the subfiles ch1.tex and ch2.tex, the page numbering associated with \pageref{ch1-fig:picture} gives the correct page in ch2.tex, which is page 2 in the subfile ch1.tex, but when I compile the main file the page-numbering doesn't get updated for some reason, since it should be on page 4 of the main file. I really can't figure out why the main file behaves this way, since the subfiles are able to update accordingly, if the picture gets moved to a different page in the subfile. My setup is partly inspired by the constructions here and here. Any help is much appreciated.

EDIT: It seems like the references to the labels do not work properly either, since in the maindocument, the labels do get updated to the correct numbers, but when trying to make a reference to them later or before, the numbers in the references are wrong.

• As far as I see, the subfiles package does not take care about the labels, which is totally different with a \include approach – user31729 May 22 '16 at 21:14
• Is there any kind of workaround for it? Since I don't want to change all my file setup just for getting the pageref command to work. – Bartuc May 23 '16 at 7:51
• I know of none, but this does not mean there's none! – user31729 May 23 '16 at 10:04

Okay, I found a slight workaround for the problem by simply letting all the labels refer to the main document, i.e. essentially commenting out the \externaldocument[ch1-]{\main/chap/chapter1/ch1}parts etc but keeping an \externaldocument[t-]{\main/main} present, so that the subfiles can find the correct labels in the main document. This makes it so that when cross-referencing things between the subfiles, I just have to make sure the labels are present in the main document by compiling the main document a few times.

I also found that hyperref works in combination with section and subsection. That means if you omit section or subsection, then you are not likely to go to another page.

\documentclass{article}

\usepackage{lipsum}
\usepackage{geometry}

\begin{document}

%\section{something}
whatever insteresting


• You can highlight code in your post using back-ticks. To highlight code-blocks, either indent them by four spaces or use the {} on the gui. – Andrew Sep 22 '16 at 12:12