1

I am looking for LaTeX to print a main paper with working reference/labels (including numerous references to tables and figures in the appendix), but have no printed appendix, and have the whole thing be just one .tex file.

This is a very similar to question to this one, however all the answers there involve using multiple files. The journal I'm submitting this to requires the LaTeX to be a single file.

\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}

Garret C
  • 53
  • 6
  • If you don't want to compile the appendix, and keep everything in one PDF, how are readers going to view it? – Someone Aug 15 '23 at 17:56
  • I would approach this by placing a custom command between the end of the main text and the input for the appendix. Start with the definition of that command being \relax. At the beginning of compilation, check two things: 1) does the .aux file exist? (That's \iffileexists.) and 2) have there been any changes in cross references? That's reported in the log, and I don't know how to test it, but I'm sure someone here does. When both conditions are met, redefine your custom command to be \end{document}. Just an idea. – barbara beeton Aug 15 '23 at 18:31
  • 1
    @Someone Not exactly relevant to solving the problem, but the journal just posts a separate PDF of the appendix (which I've already generated) to their website without doing any typesetting. – Garret C Aug 15 '23 at 21:22
  • The best answer turned out to be like this previous question: https://tex.stackexchange.com/questions/605924/combine-tex-file-with-aux-files-to-produce-a-single-self-contained-tex-file Which is to say, put the .aux file contents into the single .tex file. – Garret C Dec 12 '23 at 22:10

2 Answers2

1
\documentclass{report}

\begin{document}

    \chapter{Report}
    Report body.

    I want to reference \ref{sec:appendix1}

\expandafter\ifx\csname r@sec:appendix1\endcsname\relax\else
\expandafter\stop
\fi

    \appendix

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

\end{document}

The first (and third) run it will typeset everything but give a warning to re-run.

The second (and fourth) run you get no warning and:

enter image description here

David Carlisle
  • 757,742
0

This uses three files with \include and \includeonly. The master file controls which file(s) get printed. The version shown prints both and is used to set up the aux file (references).

\documentclass{report}
\includeonly{body,appendix}
\begin{document}
\include{body}
\include{appendix}
\end{document}

body.tex:

\chapter{Report}
    Report body.
I want to reference \ref{sec:appendix1}

appendix.tex:

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

To print only one file at a time, remove the other from \includeonly. Note that \include will force a new page, so should go before \chapter or \appendix instead of after.

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thanks, but this doesn't meet the requirements. As the title says, it has to all be in one single file. Your solution involves three. – Garret C Aug 24 '23 at 16:34
  • A savebox would increment the counters, but \ref only works if you print it. You could use the tranparent package to make the appendix invisible, but it would take up the same space. – John Kormylo Aug 24 '23 at 20:28