[Tex/LaTex] Include references to appendix without including appendix

cross-referencinginput

I'm preparing a latex document using the input command. There's a separate file for the appendix called appendix.tex. I wish to add references to theorems in the appendix in the main paper, but I don't want to have the appendix in the final compiled pdf. Is there a way to achieve this (without manually cropping pages from the final pdf)?

To elaborate further: Right now, if I don't include \input{appendix}, I get a lot of ?? (broken references) in my pdf. I wish to replace the ?? with the right theorem numbers without including the appendix.

Best Answer

First, I agree with Christian Hupfer. I would be annoyed reading such a document. Second, providing the appendix as an additional PDF may be a good idea, but it's a bit more complicated then goofynos answer suggests, if the page numbers should be continous across both files. And if there are cross references from the appendix to the main part, it get's even more complicated.

But if you really want a PDF file with cross references going nowhere, there are two ways to achieve this. For both you need to compile your PDF with the appendix attached first to get all references right. Then, after some changes to the main file, you need to compile the file one more time to get rid of the appendix.

Solution 1, with \include:

Here you need to add \includeonly{<files>} to the preamble for the last run. <files> is a comma seperated list of all included files with the exception of those for the appendix.

Note that \include always starts a new page.

\documentclass[a4paper]{article}
\usepackage{lipsum}

% uncomment for last run
% comma seperated list of all files except those for appendix
%\includeonly{main}

\begin{document}
\include{main}

\appendix
\include{appendix}

\end{document}

Solution 2, with \input:

Here you need to comment out the files for the appendix and add \nofiles to the preamble. The latter prevents LaTeX from writing auxiliary files, so the informations about the \labels there are not overwritten.

Note: \input does not start a new page.

\documentclass[a4paper]{article}
\usepackage{lipsum}

% uncomment for last run
%\nofiles

\begin{document}
\input{main}

\appendix
% comment out for last run
\input{appendix}

\end{document}

For completeness, main.tex:

\section{Main text}
\lipsum[1]

Reference to appendix: \ref{lia}

\lipsum[2]

and appendix.tex:

\section{Stuff}
\label{lia}
\lipsum[3]