[Tex/LaTex] How to replace a large block of text by an empty block of the same size

colormacrospage-breakingspacingtext-decorations

I know there exists the \phantom command, but does it work on large blocks of text (containing several paragraphs or even several pages)?

What I want is to have some commands, call them \hide and \show, that hide\show the text that it is typeset, leaving empty space where the text should be. To be more clear, when I put the \hide command, everything that appears after it (until a \show command appears) should be replaced with white empty space.

As a temporary solution, I thought coloring text in white, but this does not always works as expected. Maybe there is a more elegant way of doing this.

Edit:

In the meantime, I thought of one possible alternative solution: using the current font, generate a "blank" font that has the same metrics and use this blank font to "hide" the text. Is it possible?

Note: I've created a separate thread for this new question: Generate a “blank” font using the metrics of another font.

Best Answer

The problem with the \phantom macro is that it places its content into restricted horizontal mode, i.e. in a horizontal box which isn't broken. Therefore it can't include line breaks or paragraphs. Normally you could overcome this issue by placing the content into a minipage environment first, which allows multiple paragraphs. In order to not have to specify the width you can use the similar varwidth environment from the varwidth package instead. However \phantom is unfortunatly not defined as a long macro and therefore you can't have paragraph breaks in it. You would need to box the content yourself first then use \phantom on it or execute the underlying code yourself. The first method is pretty easy using the adjustbox package.

\usepackage{adjustbox}
\usepackage{varwidth}

\newcommand{\Hide}{%
    \adjustbox{varwidth=\linewidth,precode=\phantom}%
}
% Usage: \Hide{<content, can be multiple paragraphs>}
%    or  \Hide\bgroup <content, ...> \egroup

Your idea with \hide and \show is more complicated. It is possible to write some code which does this, but handling page breaks is difficult.

Some basic code which uses a vertical box instead would be:

\documentclass{article}

\newcommand{\hideit}{%
    \begingroup
    \par
    \setbox0\vbox\bgroup
}
\newcommand{\showit}{%
    \egroup
    \setbox1\vbox{}%
    \ht1=\ht0
    \wd1=\wd0
    \dp1=\dp0
    \box1
    \endgroup
}

\begin{document}

This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.

\hideit
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
\showit

This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.
This is a test paragraph.  This is a test paragraph.

\end{document}

However it does not support page breaks and does not add 100% the same height as the normal text, because of the missing line skip between the \vbox and the surrounding paragraphs. But it is very close. Page break support could be added by e.g. checking the height against \pagetotal and \pagegoal.