I have a problem using labels together with the subfiles
package. When I'm writing in one section and I try to reference to different section (or for that matter anything within that section) I get an undefined label error. My main.tex
:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\graphicspath{{images/}{../images/}}
\usepackage{subfiles}
\begin{document}
\maketitle
\section{Introduction}
\label{sec:intro}
\subfile{sections/CH1_Introduction}
\section{Theory of Ultrasound Anemometer}
\label{sec:theory}
\subfile{sections/CH2_Theory}
Then, for example, when I write in my 'introduction' section file:
In section \ref{sec:theory} the theory will be discussed ...
Which results in an undefined reference error. Can someone help me? Is the problem of the label command? Or am I doing something else wrong?
Best Answer
This can be done with the package xr. Here is an MWE that makes some assumptions about the layout of the project. In the root folder, under a file called
main.tex
:Then, in a subfolder called
sections
, there are two additional tex files calledCH1_Introduction
:and
CH2_Theory.tex
:compiling
main.tex
than returns:Now assuming that all your sections are called in the main document this should allow you to cite the sections (but not subsections with the current layout) from the introduction.