Ghost environment

EDITED to actually work (I think).

My approach is to create a ghost environment whereby I stick the ghost material into a box that isn't printed. I first tried it into an lrbox, but that choked on \labels. So I had some success using the bad syntax of \setbox0=\vbox\bgroup...\egroup. I figured that since I wasn't typesetting the material anyway, the bad aspects to the formulation may not be fatal.

This worked well at actuating the sectioning information without actually printing it, so I was halfway to solving the problem. However, executing \label in a box also broke things.

So my solution was to grab the meaning of \label and rather than executing the code in the box, I would just \xdef the code to an indexed macro (\LBLi, \LBLii, \LBLiii) for each invocation of \label. Then, upon exiting the box, I would cycle through a loop executing \LBLi, \LBLii, \LBLiii, successively, to actually execute the \label code outside of the box.

With a little effort, I got it to work. What \label is supposed to do is write the following evaluated line to the aux file:

\newlabel{<label>}{{\@currentlabel}{\thepage}}

so that the actual aux file contains something like \newlabel{eq:one}{{1}{1}}.

My EDIT involved using an \xdef instead of a \gdef in the definitions of \LBLx, with \noexpand preserved on \thepage. The \xdef allowed the proper value of \@currentlabel to be employed.

Here is my MWE, which works for the label numbers. It seems to work for the \thepage, as well, which can be tested in my MWE, by uncommenting the \vspace* line.

RE-EDITED to incorporate \newif\ifghost, per touhami's comment to the question. Setting \ghostfalse near the top of the preamble will cause the compilation to ignore ghost and compile the full document. With \ghosttrue, the ghost sections are omitted, but hopefully accounted for.

\documentclass{article}
\usepackage{ifthen}
\newif\ifghost
\ghosttrue
\newcounter{nlabels}
\newcounter{ilabel}
\ifghost
  \makeatletter
  \def\pwr{\protected@write\@auxout}
  \newenvironment{ghost}{\setcounter{nlabels}{0}%
    \renewcommand\label[1]{\stepcounter{nlabels}%
    \expandafter\xdef\csname LBL\romannumeral\value{nlabels}\endcsname{%
    {}{\string\newlabel{##1}{{\@currentlabel}{\noexpand\thepage}}}}}%
    \par\setbox0=\vbox\bgroup}{\egroup%
      \setcounter{ilabel}{0}%
      \whiledo{\value{ilabel}<\value{nlabels}}{%
       \stepcounter{ilabel}%
       \expandafter\expandafter\expandafter\pwr%
         \csname LBL\romannumeral\value{ilabel}\endcsname%
      }%
  }
  \makeatother
\else
  \newenvironment{ghost}{}{}
\fi
\begin{document}
%\vspace*{6in}% Seems to work if I split the page during ghost.
\section{First}\label{se:first}
Reference to equations \ref{eq:one}, \ref{eq:two}, \ref{eq:three} 
and \ref{eq:four}, and 
another reference to sections \ref{se:first}, \ref{se:second} and \ref{se:third}.
\begin{equation}\label{eq:one}
y = mx+b
\end{equation}
\begin{ghost}
\section{Second}\label{se:second}
Some text again.
\begin{equation}\label{eq:two}
a = b
\end{equation}
\begin{equation}\label{eq:three}
c = d
\end{equation}
\end{ghost}
\section{Third}\label{se:third}
Some final text to see if next equation number is correct
\begin{equation}\label{eq:four}
e = mc^2
\end{equation}
\end{document}

enter image description here

For this MWE, the aux file is written as

\relax
\@writefile{toc}{\contentsline {section}{\numberline {1}First}{1}}
\newlabel{se:first}{{1}{1}}
\newlabel{eq:one}{{1}{1}}
\newlabel{se:second}{{2}{1}}
\newlabel{eq:two}{{2}{1}}
\newlabel{eq:three}{{3}{1}}
\@writefile{toc}{\contentsline {section}{\numberline {3}Third}{1}}
\newlabel{se:third}{{3}{1}}
\newlabel{eq:four}{{4}{1}}