Executing TeX commands s.t. (1) produces empty boxes (2) and no other output

EDIT:

I need plain TeX, not LaTeX.

Furthermore, I don't want to use an existing package -- I want to reinvent the wheel. [This is part of a complicated set of TeX macros I have set up; and I need to know how to implement this to implement more complicated things.]

Original Question:

Here is my goal: I want to define a magical command

\def\magic#1...


such that:

\magic{foo bar magic foo}


will do the following:

execute foo bar magic foo, but then instead of producing actual text, it just produces an empty box that has the same width/height as foo bar magic foo.

Motivation:

Writing a quiz, and want to be able to write it in the form of:

\Question{ .... }


Then, in [answer mode], it typesets both the Question & the Answer. In [question mode], the Question part is written; the Answer part is executed -- and an equal amount of spacing is created and put there.

-
Rather than reinventing the wheel, perhaps you might want to check out some of the packages/classes designed to do this sort of thing. Three that I know of are exam, probsoln and exercise. –  Alan Munn Oct 31 '11 at 5:28
If the intent is to ask students to write answers in that space, then you would need more space than the amount taken by TeX to typeset the result. Most people write larger than 10pt! –  Aditya Oct 31 '11 at 15:02

\phantom{argument} leaves the space necessary for argument. You can combine this with an \ifanswer flag for the mode to get the desired behavior. Like Alan Munn, I would suggest to take a look at packages for typesetting exercises, especially exam.

-

As Christian Lindig already posted, \phantom is the way to go. It is defined by both LaTeX and plainTeX.

It works by boxing the argument and then measure its dimension. A second empty box is created in a box register and its dimensions are set explicitly to the ones of the first box. Then the empty box is typeset and the other one is discarded.

To demonstrate this I coded a simple implementation. The original also handles math-mode. You can also create a similar macro which inserts a line instead of an empty space.

\def\phantom#1{%
\begingroup
\setbox0\hbox{#1}%
\setbox1\hbox{}%
\ht1=\ht0
\dp1=\dp0
\wd1=\wd0
\box1
\endgroup
}

\def\phantomrule#1{%
\begingroup
\setbox0\hbox{#1}%
\setbox1\hbox to \wd0{\hrulefill}%
\ht1=\ht0
\dp1=\dp0
\box1
\endgroup
}

Hello world! How are you?

Hello \phantom{world}! How \phantom{are} you?

Hello \phantomrule{world}! How \phantomrule{are} you?

\bye


-