I am using plain TeX / pdfTeX.

Is there a way to build a set of macros, say \showOn / \showOff, such that

  1. I can nest them

  2. for a given piece of text, it "looks up the parse tree", find the first \showOn/\showOff parent, and gets displayed if and only if it's on?

For example:

Cat \showOff { dog \showOn {red \showOff {blue} green } superman} wonderwoman

Would result in:

Cat [spacing for dog] red [spacing for blue] green [spacing for superman] wonderwoman

So, I know that I can do the spacing without display part through using phantom - however, is there any way to turn phantom on/off in a nested manner?

Right now, I have:

\showOff {\showOn {foo}}

However, this clearly fails the nesting since \showOff {\showOn {foo}} will NOT output foo.


My fault for not including this earlier. Color changing does not work. The document changes colors all over the place (in pdfTeX).

  • Welcome to TeX.SE. It is always best to compose a MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Commented Nov 18, 2011 at 22:13
  • What do you want in a minimum working example? I have no idea how to create this in the first place. What part of my description is not clear? Commented Nov 18, 2011 at 22:15
  • 1
    @PeterGrill A MWE here would be in plain :-)
    – Joseph Wright
    Commented Nov 18, 2011 at 22:17
  • Is there anything else I can do to make the question easier to answer? Commented Nov 18, 2011 at 22:22
  • @JosephWright: Could add the \def\showOff{} \def\showOn{} and \bye. Not everyone uses plain TeX so it takes time to get to this point (well at least for me). Commented Nov 18, 2011 at 22:23

2 Answers 2


An answer which does not rely on colors and allows verbatim text.

\catcode 64 11
\def\ShowOn@#{\egroup\bgroup\aftergroup\ShowOn@@\let\next= }
\catcode 64 12




How does \phantom work? It stores the text in a box, then creates an empty box with the same width, height and depth. In a similar way, I'll collect the text that should be hidden in box 0, and produce a horizontal space of the same width as that box. All text that is to be displayed is typeset directly.

Let us analyse for example \ShowOff{abc} First, it becomes

\afterassignment\ShowOff@ \setbox\z@\hbox{abc}

TeX remembers \ShowOff@ for use after the next assignment, namely \setbox. The placement of the "after assignment" token in the case of \setbox is a little bit tricky; in our case it ends up after the opening brace following \hbox. Ignore \@inSO@true for the moment. \aftergroup\ShowOff@@ will place \ShowOff@@ as soon as the box that is being build ends. This macro, in turn, inserts a horizontal skip of the size of the box.

Now, \ShowOn cancels this hiding by ending the box of \ShowOff early, namely with the \egroup in \ShowOn@. The horizontal skip is then inserted there. We can then typeset the argument of \ShowOn, and resume hiding afterwards. I could have defined \def\ShowOn@#1{\egroup#1\ShowOff\bgroup}, but this grabs the argument early, forbidding verbatim. Instead, I used \aftergroup again: the argument of \ShowOn comes as a group, {...}. Somehow, we want to insert \aftergroup\ShowOn@@ after the opening brace. Open a brace (\bgroup), and remove the one that the user put (\let\next=).

I still have to explain the @inSO@ switch. If \ShowOn appears outside any \ShowOff command, it should just do nothing to its argument. Otherwise, it acts as described in the previous paragraph.

EDIT: I tried to add support for math mode below (didn't incorporate it above because the macros become ugly).

  \setbox\z@\hbox\bgroup$\m@th #1%
    \let\next= }
\def\ShowOn@#{\egroup\bgroup\aftergroup\ShowOn@@\let\next= }

% Tests.
$abcdefg^h_{ijkl}mnop$ $co^{ffi}n$


abcdefghijklmnop coffin


  • Wow. Nice job using #{, too.
    – Ryan Reich
    Commented Nov 19, 2011 at 4:37
  • Great solution!
    – Boris
    Commented Nov 19, 2011 at 4:43
  • It's 1:22am here. I was about to go to sleep. But this looks too cool to not read and understand. Commented Nov 19, 2011 at 9:23
  • 1
    So I'm reading page 279 of Knuth's TexBook. Your \afterassignment\ShowOff@ \setbox\z@\hbox{abc} becomes: \setbox\z@\hbox{\ShowOff@ abc} <-- is this correct? Commented Nov 19, 2011 at 9:38
  • Two more requests: (1) how can I change the \hskip\wd\z@ so it does both width, height & depth. (2) Right now, if I use \showOff within math mode, it breaks, i.e. $\ShowOff{\sum_{i=1}^n}$ results in compile error for me. How do I fix this? -- Thanks! Commented Nov 19, 2011 at 9:49
\input color
Cat \showOff { dog \showOn {red \showOff {blue} green } superman} wonderwoman


Here is the solution that does not assume the text is black. It assumes, however, that the background is white. Also, any explicit color change inside \showOff makes the text visible.

\input color
\color{green} Cat \showOff { dog \showOn {\textcolor{red}{red}
\showOff {blue} green } superman} wonderwoman 
  • 1
    The OP is using plain TeX, not LaTeX. Commented Nov 18, 2011 at 22:20
  • My fault for not mentioning the document uses pdf's color changing abilities; so toggling to black/white won't work. Commented Nov 18, 2011 at 22:20
  • (Sorry for not mentioning the fact I was using pdftex's color changing abilities earlier.) Commented Nov 18, 2011 at 22:20
  • Clever, but this also produces the green and wonderwoman in white. Commented Nov 18, 2011 at 22:37
  • @TorbjørnT. Thanks - I've changed my answer
    – Boris
    Commented Nov 18, 2011 at 22:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .