[Tex/LaTex] Censor text spanning multiple lines

expansionline-breakingmacros

I'd like to censor or hide some text while replacing it with the appropriate amount of white-space. The replacement text cannot be searchable. The text to be hidden usually exists in the LR mode. The censor and invisible packages do not seem to work well with text spanning multiple lines.

One solution, I believe, would be to turn each word into an appropriately sized box, \makebox[\wordwidth]{}, and let TeX (or LaTeX) handle the rest since \makebox is treated like a word.

I am very \sloppy, so the problem is hopefully simplified because you don't need to worry about whether a dash will be placed in the middle of a word. (Although, the soul package is capable of handling such situations.)

Best Answer

Here's a quick solution using the customization possibilities of the soul package. More information on how this works can be found in the package documentation, p. 22-25.

\documentclass{article}

\usepackage{soul}
\makeatletter
\DeclareRobustCommand*\censor{%
    \SOUL@setup%
    \def\SOUL@everytoken{\phantom{\the\SOUL@token}}%
    \def\SOUL@everyhyphen{%
        \discretionary{%
            \SOUL@setkern\SOUL@hyphkern%
            \phantom{\SOUL@sethyphenchar}%
        }{}{}%
    }%
    \def\SOUL@everyexhyphen##1{%
        \SOUL@setkern\SOUL@hyphkern%
        \hbox{\phantom{##1}}%
        \discretionary{}{}{%
            \SOUL@setkern\SOUL@charkern%
        }%
    }%
    \SOUL@%
}
\makeatother

\begin{document}
One morning, when Gregor Samsa woke from troubled dreams, he found himself
transformed in his bed into a horrible vermin. He lay on his armour-like back,
and if he lifted his head a little he could see his brown belly, slightly domed
and divided by arches into stiff sections. The bedding was hardly able to cover
it and seemed ready to slide off any moment. His many legs, pitifully thin
compared with the size of the rest of him, waved about helplessly as he looked.
``What's happened to me?'' he thought. It wasn't a dream. His room, a proper
human room although a little too small, lay peacefully between its four familiar
walls. A collection of textile samples lay spread out on the table -- Samsa was
a travelling salesman -- and above it there hung a picture that he had recently
cut out of an illustrated magazine and housed in a nice, gilded frame.

One morning, when Gregor Samsa woke from troubled dreams, he found himself
transformed in his bed into a horrible vermin. He lay on his
\censor{armour-like} back, and if he lifted his head a little he could see his
brown belly, slightly domed and divided by arches into stiff sections. The
bedding was hardly able to cover it and seemed ready to slide off any moment.
His many legs, pitifully thin compared with the size of the rest of him, waved
about helplessly as he looked. \censor{``What's happened to me?''} he thought.
It wasn't a dream. His room, a proper human room although a little too small,
lay peacefully between its four familiar walls. A collection of textile samples
lay spread out on the table -- \censor{Samsa was a travelling salesman} -- and
above it there hung a picture that he had recently cut out of an illustrated
magazine and housed in a nice, gilded frame.
\end{document}

output result

This solution allows to censor multiple lines and also treats hyphenated words correctly (see "armour-like"). As the characters are completely replaced by whitespace using \phantom, the censored text isn't written to the resulting PDF at all and therefore can't be extracted.

Related Question