# Ghost environment

I am trying to do the following. I would like a part of my source to be compiled normally by to generate no output. In particlular, I would like references made to the ghosted parts to be produce the same results as if the parts were not ghosted.

I know this sound bizarre and I have seen anything doing something like that.

I would have no trouble with a solution that does not process floats in the ghosted parts.

Here is an idea of the code I would like:

\documentclass{article}
%\usepackage{ghost} % package that would define the ghost environement
\begin{document}
\section{First}
Some text here with a reference \ref{eq:one} and
another reference \ref{se:third}
%\begin{ghost}
\section{Second}
Some text again.
$$\label{eq:one} a = b$$
%\end{ghost}
\section{Third}\label{se:third}
Some final text
\end{document}


that would produce the following output (once the ghost environment works):

• I haven't tried, but \includeonly may solve your problem if you're willing to have the ghost environments in separate files. (You might avoid that with \filecontents.) See the accepted answer to tex.stackexchange.com/questions/87010/… – Ethan Bolker Oct 17 '15 at 1:07
• I think the idea here is conditional compilation. One define new test \ifghost, the environment ghost is defined to do nothing by default and save its contents into \vbox (see Steven B. Segletes' answer below) if the condition \ghosttrue. \ifghost is set to true only in final compilation. – touhami Oct 17 '15 at 17:13

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}.
$$\label{eq:one} y = mx+b$$
\begin{ghost}
\section{Second}\label{se:second}
Some text again.
$$\label{eq:two} a = b$$
$$\label{eq:three} c = d$$
\end{ghost}
\section{Third}\label{se:third}
Some final text to see if next equation number is correct
$$\label{eq:four} e = mc^2$$
\end{document}


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

• what about \pageref? – touhami Oct 17 '15 at 6:12
• @touhami I'm not sure your exact question. I seem to be able to include \pageref calls either inside or outside of the ghost environment, so help me to understand your concern. – Steven B. Segletes Oct 17 '15 at 15:03
• Here the contents of ghost environment is short, suppose that this contents is long (example \lipsum[1-30] after eq:one) in this case \pageref{eq:one} and \pageref{eq:three} shouldn't refer the same page. – touhami Oct 17 '15 at 16:09
• it seems to be extension of http://www.ctan.org/pkg/pagesel package – touhami Oct 17 '15 at 17:06
• please see my comment to the question. – touhami Oct 17 '15 at 18:13

I decided to sum up the differences between Steven's solution and using \include/\includeonly (as suggested by Ethan Bolker) in a separate answer. I didn't really do a thorough comparison with Steven's solution, so if you find anything else, feel free to add to this post.

1. With \include, the ghost part has to be stored in a separate file. As Ethan writes, this can be circumvented using \filecontents.
2. Whenever a file is included, a new page is started. Also, at the end of the included file, a new page is started. This may not always be the desired behaviour, but it also has advantages: The text surrounding the \include will always be typeset in the same manner, no matter whether the file is actually included or not. Also the page counter is adjusted to respect the 'hidden' pages. Therefore, the hidden text is distributed over invisible pages, and it is possible to refer to these hidden pages, as touhami desires in his comment.