TransWikia.com

Ghost environment

TeX - LaTeX Asked by Denis on August 26, 2020

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:

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.
begin{equation}label{eq:one}
a = b
end{equation}
%end{ghost}
section{Third}label{se:third}
Some final text
end{document}

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

enter image description here

2 Answers

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=vboxbgroup...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 newififghost, 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}
newififghost
ghosttrue
newcounter{nlabels}
newcounter{ilabel}
ifghost
  makeatletter
  defpwr{protected@write@auxout}
  newenvironment{ghost}{setcounter{nlabels}{0}%
    renewcommandlabel[1]{stepcounter{nlabels}%
    expandafterxdefcsname LBLromannumeralvalue{nlabels}endcsname{%
    {}{stringnewlabel{##1}{{@currentlabel}{noexpandthepage}}}}}%
    parsetbox0=vboxbgroup}{egroup%
      setcounter{ilabel}{0}%
      whiledo{value{ilabel}<value{nlabels}}{%
       stepcounter{ilabel}%
       expandafterexpandafterexpandafterpwr%
         csname LBLromannumeralvalue{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}}

Correct answer by Steven B. Segletes on August 26, 2020

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.

Answered by jarauh on August 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP