I'd like to do something that is very similar to what is asked for (and answered) here:
Hiding an environment but keeping equation labels and their numbers inside,
but I'd like to go one step further.
I have a number of custom environments that I can show or hide (in the PDF output after compiling) by toggling an ON/OFF option.
I might have a labelled equation inside a certain environment, which I will label with \label{marker} and refer to in another part of the document with \eqref{marker}, outside of that type of environment. When I choose to hide that type of environment, I still want each of these now-hidden equations to be counted by the equation counter, and I can do this, thanks to the above-linked post and response. However, I also want to still be able to reference these hidden equations in other (unhidden) parts of the document, and this is the part I am having trouble with. (At first I thought that the solution with \setbox0\vbox in the above-linked post would work perfectly, but when I copy-paste the code from that post and compile, \eqref{marker} outputs (??) when \label{marker} is inside the hidden environment.)
At the moment, I just have equations inside the optionally-hidden environments, but at some point I may want to do the same with figures and other things one can label and cross-reference with \label{marker} and \ref{marker} or \eqref{marker}. So, an all-in-one (or many-in-one) solution is preferable to one that just deals with equations.
Effectively, I want to hide the visual output, but keep all the "behind the scenes" stuff in the .aux file etc., as if the visual output were not hidden.
I can imagine that my problem may not be well-defined, since changing the visual output will change page numbers and locations of things within the document. I am using hyperref but I don't really care what happens with the links to hidden equations. I don't envisage ever needing \pageref{key} with \label{key} inside a hidden environment.
Perhaps another way of thinking of my goal is this: I essentially want to shrink the output of a type of environment to a point so small that it is invisible to the naked eye, but not to LaTeX.
In fact, in the MWE provided, if I first compile with everything unhidden, then compile with one or both environments hidden, everything looks just as I want it and the data is all in the .aux file. When I compile a second time, the data is removed from the .aux file and the equation references appear as (??).
I tried to research solutions involving \immediate\write somehow, but unfortunately this seems too advanced for me to understand at present. I've also tried to understand
Hide output, but maintain the cross-references
and
hide specific table, keep cross-references and caption in \listoftables,
which seem related, among other posts, but to no avail.
Any help or advice greatly appreciated.
\documentclass[desertEnvironmentOFF, forestEnvironmentON]{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{environ}
\newif\ifdesertEnvironment
\DeclareOption{desertEnvironmentON}{\desertEnvironmenttrue}
\DeclareOption{desertEnvironmentOFF}{\desertEnvironmentfalse}
\newif\ifforestEnvironment
\DeclareOption{forestEnvironmentON}{\forestEnvironmenttrue}
\DeclareOption{forestEnvironmentOFF}{\forestEnvironmentfalse}
\ProcessOptions\relax
\newcounter{environments}
\numberwithin{environments}{section}
\numberwithin{equation}{section}
\ifthenelse{\boolean{desertEnvironment}}
{\NewEnviron{desertEnvironment}[1][]
{\refstepcounter{environments}\vspace{1em}
{{\bfseries Desert \theenvironments.}}
{\itshape \BODY}\vspace{1em}}}
{\NewEnviron{desertEnvironment}[1][]
{\refstepcounter{environments}
\setbox0\vbox{\BODY}
}}
\ifthenelse{\boolean{forestEnvironment}}
{\NewEnviron{forestEnvironment}[1][]
{\refstepcounter{environments}\vspace{1em}
{{\bfseries Forest \theenvironments.}}
{\itshape \BODY}\vspace{1em}}}
{\NewEnviron{forestEnvironment}[1][]
{\refstepcounter{environments}
\setbox0\vbox{\BODY}
}}
\usepackage{setspace}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\begin{document}
Here is some text that is not inside any custom environment. Here is a labelled equation that is not inside any custom environment [should be (0.1)]:
\begin{equation}
\label{eq:fermat}
x^n + y^n = z^n.
\end{equation}
\begin{desertEnvironment}
\label{desert:01}
This is a desert environment. It is labelled. Here is a labelled equation within the environment [should be (0.2)]:
\begin{equation}
\label{eq:desertEinstein}
e = mc^2
\end{equation}
The desert environment ends with this sentence.
\end{desertEnvironment}
\begin{forestEnvironment}
\label{forest:01}
This is a desert environment. It is labelled. Here is a labelled equation within the environment [should be (0.3)]:
\begin{equation}
\label{eq:forestPythagoras}
a^2 + b^2 = c^2.
\end{equation}
The desert environment ends with this sentence.
\end{forestEnvironment}
Another labelled equation that is not inside any environment [should be (0.4)]:
\begin{equation}
\label{eq:euler}
e^{i\pi} + 1 = 0.
\end{equation}
Reference to desert environment: Desert Environment \ref{desert:01} [should be 0.1].
Reference to equation inside it: Einstein \eqref{eq:desertEinstein} [should be (0.2)].
Reference to forest environment: Forest Environment \ref{forest:01} [should be 0.2].
Reference to equation inside it: Pythagoras \eqref{eq:forestPythagoras} [should be (0.3)].
Reference to outside eq'ns: Fermat \eqref{eq:fermat}, Euler \eqref{eq:euler} [should be (0.1), (0.4)].
\end{document}
