3

I have a report that is 50 pages with a 250 page appendix. I want to create two PDF files, one with and without the appendix. The main chapters of the report reference the appendices, and so I want those links to be preserved.

I'm compiling the report with pdfLaTeX (and Knitr). I tried using the R package pdftools but that breaks my hyperlinks.

Is there a LaTeX based solution to this? I've seen people use pdftk, but I don't have access to this on my computer at work.

Here's an example. I want to compile the entire report to keep the links and references, but don't want the appendix included.

\documentclass{report}

\begin{document}

    \chapter{Report}
    Report body.

    I want to reference \ref{sec:appendix1}

    \appendix

    \chapter{Appendix}
    Here's a really long appendix with a reference \label{sec:appendix1}.

\end{document}
9
  • 2
    You might be looking for \include and \includeonly.
    – user194703
    Commented Feb 8, 2020 at 1:07
  • I am using those, but when you don’t include the appendix, it doesn’t create the references (they show up as ??) unless I’m mistaken Commented Feb 8, 2020 at 2:19
  • 3
    You need to compile it once with \includeonly{main,appendix}, where main.tex contains \chapter{Report} Report body. I want to reference \ref{sec:appendix1} and appendix.tex contains \appendix \chapter{Appendix} Here's a really long appendix with a reference \label{sec:appendix1}.. After the first compilation you can use \includeonly{main} and the references will show (since you have created the aux file in the first run) but the appendix will no longer be compiled nor shown in the resulting pdf.
    – user194703
    Commented Feb 8, 2020 at 2:25
  • Yup, that did the trick. Simple solution. Commented Feb 8, 2020 at 3:35
  • Hi, how to do it on Overleaf?
    – GoingMyWay
    Commented May 28, 2021 at 13:59

2 Answers 2

2

Well, in an comment user user194703 mentioned, that commands \include and \includeonly can do what you want. Let us take a look into details ...

Please see that you need two tex files, for example main.tex

\chapter{Report}
Report body.

I want to reference \ref{sec:appendix1}

and appendix.tex

\appendix
\chapter{Appendix}
Here's a really long appendix with a reference \label{sec:appendix1}.

to be included in your file mwe.tex like this:

\documentclass{report}

\includeonly{main,appendix}% <========================================== first compile run
%\includeonly{main}% <================================================== second and more runs

\begin{document}

\include{main.tex} % <==================================================

\include{appendix.tex}% <===============================================

\end{document}

Command \includeonly{main,appendix} in your preamble includes only the two files main.tex and appendix.tex (there might be more files included in your document) into the compile run with the result that you get the two files main.aux and appendix.aux containing the needed informations for correct references.

Please see that after the first run you see only ?? instead a reference (that is okay! a second run is needed).

For the second (and third etc) run move the %sign to have only command \includeonly{main} called:

\documentclass{report}

%\includeonly{main,appendix}
\includeonly{main}% <================================================== second and more runs

\begin{document}

\include{main.tex} % <==================================================

\include{appendix.tex}% <===============================================

\end{document}

Then you get thw wished result: no appendix printed, but the reference to Appendix A is printed as you can see in the following pdf:

resulting pdf

1
  • Great answer, nonetheless more care is needed for the bibliography, otherwise citations from the appendix will bleed into the bibliography. If one wants the short version: bibtex/biber should be run while the appendix is not included and there is no file appendix.aux.
    – Cryme
    Commented Dec 27, 2022 at 17:51
0

Here is a very simple tutorial for you:

https://www.overleaf.com/learn/how-to/Cross_referencing_with_the_xr_package_in_Overleaf

It solved my problem.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .