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:

%... some other subfiles

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

Some text.... \newpage
    \caption{Some picture}

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

As shown in figure~\ref{ch1-fig:picture} on page~\pageref{ch1-fig:picture} the labelling works.

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.



\hypersetup{colorlinks=true, pdftex}


whatever insteresting

something \hyperref[a]{hyperref link}

whatever boring

something else \label{a} 


Like if you don't comment out the section, then it works fine

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.