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}

\relax. At the beginning of compilation, check two things: 1) does the.auxfile 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