I am running into a rather weird error that apparently originates from the combination of \include
, \let\clearpage\relax
and a referenced label after a page break.
In this particular example, the label mylabel
defined in the listing cannot be referenced anymore when the reference and the label are close enough to a page break.
The MWE consists of two files, the main-file and an included file.
I am using the \let\clearpage\relax
command to avoid page breaks after every \include
.
The error does not occur if the file is \input
instead of \include
d.
\documentclass[11pt,twoside]{book} %
\usepackage[utf8]{inputenc} % Enoding
\usepackage[english]{babel} % Language
\usepackage{listings}
\usepackage{forloop}
\begin{document}
\begingroup
\let\clearpage\relax
\include{includedfile} % changing include for input would work
\endgroup
\end{document}
Included file:
\newcounter{ct}
\forloop{ct}{1}{\value{ct} < 40}%
{%
.\\
}
Listing \ref{mylabel}.
\begin{lstlisting}[caption=somecaption, label=mylabel]
some code;
\end{lstlisting}
Running this example will have an output where the label cannot be referenced (even after multiple runs):
Now, one solution is to use \input
as mentioned earlier but I am rather interested in why this happens.
Does the \clearpage
command fulfil an important function for references to work?
And can the error be avoided without removing \let\clearpage\relax
while using \include
?
EDIT: I just found this question with the same issue (though it does not have a definite answer).