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

I know this sounds bizarre and I have not 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:

%\usepackage{ghost} % package that would define the ghost environement
Some text here with a reference \ref{eq:one} and 
another reference \ref{se:third}
Some text again.
a = b
Some final text

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

enter image description here

  • 1
    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/… Oct 17, 2015 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, 2015 at 17:13

2 Answers 2


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:


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.

    \expandafter\xdef\csname LBL\romannumeral\value{nlabels}\endcsname{%
         \csname LBL\romannumeral\value{ilabel}\endcsname%
%\vspace*{6in}% Seems to work if I split the page during ghost.
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}.
y = mx+b
Some text again.
a = b
c = d
Some final text to see if next equation number is correct
e = mc^2

enter image description here

For this MWE, the aux file is written as

\@writefile{toc}{\contentsline {section}{\numberline {1}First}{1}}
\@writefile{toc}{\contentsline {section}{\numberline {3}Third}{1}}
  • what about \pageref?
    – touhami
    Oct 17, 2015 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. Oct 17, 2015 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, 2015 at 16:09
  • it seems to be extension of http://www.ctan.org/pkg/pagesel package
    – touhami
    Oct 17, 2015 at 17:06
  • 1
    please see my comment to the question.
    – touhami
    Oct 17, 2015 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.