## Un peu de caviar, s’il vous plaît

There are times when you must hide information from a document without changing its appearance and layout. I’m not talking about plain censorship, I meant like when you have to submit a paper to a journal or a conference anonymously while required to follow stringent layout guidelines. It may suffice to set a background of the same colors as the text, but if it does the thing in print, it doesn’t work so well with PDF where you can still select the text (and I did a blog entry using this behavior a while ago).

There’s a censor package for $\LaTeX$ but it doesn’t play well with whole paragraphs, and it messes out the layout if you select more than just a word or two. This week’s entry proposes a simple $\LaTeX$ macro to do nice (but not weapon-grade) “censoring”.

The main idea for the macro is to process each item individually rather than as a single block. But programming $\LaTeX$ macros is like swimming in a cesspool of cat vomit: nothing is documented, the syntax has six variations for the same thing—all unreadable, of course—and even the knowledgeable guys from #latex (the IRC channel on freenode), despite being as helpful as they can, can’t always help.

At first I thought I could use PGF’s pgffor package that provides a \foreach macro but it requires the list to be coma-separated; which is perfectly fine as long as you don’t intend to use it on a piece of space-separated text, that is, regular text. Also, it may be too general for what we need: it provides iterator and user-specified operations. Let us go for simplicity:

% in the preamble, after \documentclass
% but before \begin{document}
%
\usepackage{ifthen}

\newboolean{caviardee}
\setboolean{caviardee}{true}

\makeatletter
\newlength{\@caviard@width}
\ifthenelse{\boolean{caviardee}}%
{%
\def\caviar#1{\@caviard#1 \@empty}%
}%
{%
\def\caviar#1{#1}%
}

\def\@caviar#1 #2{%
\settowidth{\@caviar@width}{#1}%
\rule{\@caviar@width}{1ex}\space%
\ifx #2\@empty\else
\expandafter\@caviar
\fi
#2%
}


This macro is defined to black out enclosed text if caviardee (caviardée) is true, and to show text normally otherwise. The macro itself uses a first:rest-of-the-list recursive approach, stopping when the rest-f-the-list is \@empty.

And voilà, you can now censoranonymize all the text you want!

*
* *

Ok, let us show a complete, but minimal, example:

\documentclass{article}
\usepackage{ifthen}

\makeatletter
\newlength{\@caviard@width}

\newboolean{caviardee}
\setboolean{caviardee}{true}

\ifthenelse{\boolean{caviardee}}%
{%
\def\caviard#1{\@caviard#1 \@empty}%
}%
{%
\def\caviard#1{#1}%
}

\def\@caviard#1 #2{%
\settowidth{\@caviard@width}{#1}%
\rule{\@caviard@width}{1ex}\space%
\ifx #2\@empty\else
\expandafter\@caviard
\fi
#2%
}

\begin{document}

This text is \caviard{censored, it really is!}.

\end{document}



*
* *

The macro’s name comes from caviarder, the french word for the act of marking parts of a text in black so to make them unreadable.

### 6 Responses to Un peu de caviar, s’il vous plaît

1. Nuzz says:

I’m only a latex user, so forgive me if the following is implicitely clear for anyone knowledgeable, but will this replace the text with boxes on output (regardless of format) or will it just set the background to black?

• Steven Pigeon says:

It walks through text and creates a “ruler” of the height of one 1ex (the height of the letter x, more or less) by the width of the word. The original text is lost. So you can’t just select the region with the pdf viewer and see the text anyway: it will just select a region empty of text.

2. I’m new to using LaTex and macro’s. What’s the usage format?

\newboolean{caviardee}
\setboolean{caviardee}{true}

…and then tried:

\caviar{Text to redact}

…but that didn’t work.

• Steven Pigeon says:

Yes, you are right. My bad. Some pieces were missing, like the actual definition of the boolean variable. I also added a minimal, but complete, example. Thanks for pointing out the bug.

3. Alex Hirzel says:

You never end your \makeatletter with a \makeatother. This is not required but it is good practice as far as I know.

• Steven Pigeon says:

OK, I did not know that. Thanks for the info