# Cross references and subfiles

I want to take references between subfiles for a big project, the main structure is for the main files is main.tex:

\documentclass[12pt]{book}
\usepackage{subfiles}
\usepackage{xr}
\externaldocument[I-]{intro/intro}
\externaldocument[A-]{ante/ante}

\begin{document}
\chapter{chap1}
\subfile{intro/intro.tex}
\chapter{chap2}
\subfile{ante/ante.tex}
\end{document}

and subfile 1 intro.tex:

\documentclass[../main.tex]{subfiles}
\begin{document}
... \section{secA}\label{sec:secta}
\end{document}

and subfile 2 ante.tex:

\documentclass[../main.tex]{subfiles}
\begin{document}
... in \ref{I-sec:secta}
\end{document}

but the thing is that I just receive "... in (??)" instead the number of the reference "in (number)" in the output file ante.pdf, I' ve tried change the paths but I still have the ?? in the reference number. Does anyone have a idea how to fix this?, thanks in advance.

• Are you using Sharelatex ? -- How are you compiling your project ? -- Have you seen the error messages in the log ? -- note that \externaldocument[I-]{intro} requires the .aux files. If you do not have the .aux files in the corresponding directories, you will get an error. – Jaime Sep 12 '17 at 0:48
• Hi @Jaime, Thanks for the answer, I am using pdflatex to compile the project, I have a .aux file in each directory, now I was able to obtain a number inside the parenthesis "..in ( )" but the number is wrong, for example I have "(0.1.2)" but the correct section reference is "(1.1.2)" – mors Sep 12 '17 at 16:57
• Do you have a minimal example we can check? – Jaime Sep 12 '17 at 19:43

The compilation of ante.tex will not resolve the cross reference for intro.tex since it has not any information that there is intro.tex at all.

Use \externaldocument[I-]{../intro/intro} in ante.tex again.

\documentclass[../subfilesmain.tex]{subfiles}
\externaldocument[I-]{../intro/intro}
\begin{document}
in \ref{I-sec:secta}
\end{document}

subfilesmain.tex

\documentclass[12pt]{book}
\usepackage{subfiles}
\usepackage{xr}
\externaldocument[I-]{intro/intro}
\externaldocument[A-]{ante/ante}

\begin{document}
\chapter{chap1}
\subfile{intro/intro.tex}
\chapter{chap2}
\subfile{ante/ante.tex}
\end{document}

intro.tex

\documentclass[../subfilesmain.tex]{subfiles}
\begin{document}
... \section{secA}\label{sec:secta}
\end{document}
• Hi, Thanks for your help, with your answer I was able to obtain the number of the references in each individual file and in the "Main" file as well, but I still have the problem with the wrong numbers related with the numeration of the chapters, for example I have something as " ... in (0.3.2)" but I need "... in (2.3.2)", now I have to figure out how to fix this – mors Sep 13 '17 at 22:45
• @mors: The result is 0.1 because the intro file does not have any chapter and so the chapter number is 0. If you want to include the chapter number, the \chapter command should go the intro file etc. as well – user31729 Sep 14 '17 at 8:09