1

I am having trouble getting cross-referencing to work. I am using the package xr-hyper. I have a 'main' file called main.tex and two subfiles called intro.tex (Introduction) and app.tex (Appendix A). I want to refer to an equation defined in intro.tex in Appendix A.

Either I have made an error in my code or its an issue with my build process. I am very confused about the build process for this. I'm using TeXstudio and I have set up my build process to run PdfLaTeX two times, which I do on main.tex. Is this correct?

I once got the numbers to appear correctly, but I'm not sure how I managed this. But, when I then added a new chapter ahead of intro.tex, the numbers of the references in Appendix A did not update when I re-built the file main.tex.

Any advice would be much appretiated.

The errors I am getting are:

enter image description here

enter image description here

enter image description here

enter image description here

main.tex

\documentclass[12pt,a4paper]{report}
\usepackage{subfiles}
\usepackage[titletoc]{appendix}
\usepackage{xr-hyper}
\usepackage{hyperref}

\begin{document}

\tableofcontents

\subfile{intro.tex}

\begin{appendices}
\subfile{app.tex}
\end{appendices}

\end{document}

intro.tex

\documentclass[main.tex]{subfiles}

\begin{document}

\chapter{Introduction}
\begin{equation}\label{eqn:newton}
F = ma
\end{equation}
Equation~\ref{eqn:newton}

\end{document}

app.tex

\documentclass[main.tex]{subfiles}
\externaldocument[I-]{intro} 

\begin{document}

\chapter{Talking about Equation~\ref{I-eqn:newton}}
Let's refer to Equation~\ref{I-eqn:newton}.

\end{document}
Milo
  • 9,440
  • Did you compile intro before app? – samcarter_is_at_topanswers.xyz Jun 05 '18 at 21:04
  • I didn't realise I had to compile intro and app in the first place, before compiling main.tex – Milo Jun 05 '18 at 21:07
  • Weirdly, when I try to compile to intro and app, TeXstudio instead compiles main. But, if I restart TeXstudio and open intro and app without opening main then I can get TeXstudio to compile intro and app individually. Is this normal behaviour? – Milo Jun 05 '18 at 21:11
  • 3
    Personally I find your system a bit risky. If you changed something between compiling intro and your final document, the reference in the appendix might be wrong. Personally I use the external labels without prefix and only in the subfile, not in the main document. See https://tex.stackexchange.com/a/378163/36296 for my system – samcarter_is_at_topanswers.xyz Jun 05 '18 at 21:12
  • 3
    Yes, texstudio automatically tries to detect the main document, so you have to manually define the root document when compiling subfiles. See the first lines in https://tex.stackexchange.com/a/378163/36296 – samcarter_is_at_topanswers.xyz Jun 05 '18 at 21:13
  • @samcarter Thank you!! Your solution works perfectly! In fact, it seems to work too well :) I have one question. Say I have intro and app saved in individual subfolders. If I write \IfEq{\jobname}{\detokenize{main}}{}{%\externaldocument{intro}} in app.tex this works fine even when intro.tex is saved in a subfolder. Why does this work? I thought I might have to tell it where to find intro. – Milo Jun 05 '18 at 22:28
  • Good question, I never tried that - subfolders just make everything more difficult :) – samcarter_is_at_topanswers.xyz Jun 06 '18 at 08:22

0 Answers0