# Censor images and tables

My publication requires a public and non-public version. I have several figures and tables that I'd like to censor by placing a big colored block on top of it. One page suggested using \colorbox. Unfortunately, this doesn't work for pdf files.

All of the images and tables are simple \includegraphics.

Anyone have a clue?

Update @doncherry, this:

\begin{figure}[h!]
\centering
\colorbox{black}{\includegraphics[width=1\textwidth]{\string"Image".pdf}}
\end{figure}


produces:

-
For the nonpublic version say \usepackage[draft]{graphicx} –  egreg Mar 17 '12 at 14:25
Can you show us what you've tried with \colorbox that didn't work? @egreg's suggestion works but one should note that that will blacken all images. –  doncherry Mar 17 '12 at 14:29
@doncherry You can as well put \includegraphics[draft]{mypicture.png}, but it'll mean that you have to change the code manually. –  tohecz Mar 17 '12 at 14:30
I have upgraded the censor package (as of 2/19/13) to be able to redact things like figures and tables (anything stored in a LaTeX box). The upgrade was uploaded to CTAN. –  Steven B. Segletes Feb 19 '13 at 16:38

You can use the option decodearray to produce a black image with a suitable switch. The image below shows the uncensored and censored image respectively.

The MWE is shown below.

\documentclass[a4paper]{article}
\usepackage{graphicx,caption}
\newif\ifcensored
\begin{document}
\centering
\ifcensored
\includegraphics[decodearray=
0.0 0.0
0.0 0.0
0.0 0.0, width=5cm]{amato.jpg}%
\captionof{figure}{censored image}
\else
\includegraphics[width=5cm]{amato.jpg}% image with RGB colors
\captionof{figure}{uncensored}
\fi
\includegraphics[decodearray=
0.0 0.0
0.0 0.0
0.0 0.0, width=5cm]{amato.jpg}%
\captionof{figure}{censored image}
\includegraphics[width=5cm]{amato.jpg}% image with RGB colors
\captionof{figure}{uncensored}
\end{document}


The code works well with .png as well as .jpg images. Details for decodearray can be found in the PDF Reference Manual. As a matter of interest if the values of the pairs have values other than zero one can manipulate the color channels, producing a reddish image for example.

Of course you will need to incorporate a suitable command to replace \includegraphics. One such approach is to define:

\DeclareRobustCommand{\cimage}[3][]{
\ifcensored
\includegraphics[decodearray=
0.0 0.0
0.0 0.0
0.0 0.0, width=5cm]{#2}%
\captionof{figure}{#3(censored image)}
\else
\includegraphics[width=5cm]{#2}% image with RGB colors
\captionof{figure}{#3}
\fi
}


To censor the images you type,

\censoredtrue
\cimage{amato.jpg}{A caption for your figure}


Note that the image data is still included in the pdf and can possibly be extracted, so use with caution or in cases where paper copies only will be circulated.

-
Does this work with non-jpeg images? –  Martin Schröder Mar 17 '12 at 16:21
@MartinSchröder Yes it does. –  Yiannis Lazarides Mar 17 '12 at 16:38
Looks good! Two questions (that you perhaps could answer within your answer): What is decodearray actually intended for? Doing this for every image in a paper might be pretty tedious -- can this be put in a macro, similar to @tohecz's solution? And a third question actually: Is the typo in \ifcencored intentional? –  doncherry Mar 17 '12 at 16:51
@doncherry Thanks! Will put some more info in the morning and fix the typo! –  Yiannis Lazarides Mar 17 '12 at 16:59
@doncherry I noticed the typo as well and thought like "maybe \ifcensored is some LaTeX internal?" :) –  tohecz Mar 17 '12 at 19:07

## Variant One - \includegraphics and \DRMincludegraphics

The following code embeds the egreg's idea in a more sophisticated way. It introduces new command \DRMincludegraphics. If you set \iftrue at the beggining, it will add draft option to the command which hides the graphics. If you set \iffalse, it behaves like the ordinary \includegraphics. You have to put the code after you load graphicx package.

\iftrue
\makeatletter
\def\@DRMincludegraphics[#1]{\includegraphics[#1,draft]}
\def\DRMincludegraphics{\@ifnextchar[%
\@DRMincludegraphics%
{\includegraphics[draft]}%
}
\makeatother
\else
\let\DRMincludegraphics\includegraphics
\fi


## Variant Two - \alwaysincludegraphics and \includegraphics

Works the same way as Variant One, just with the difference that the default command is the one that gets hidden.

\iftrue
\let\alwaysincludegraphics\includegraphics
\makeatletter
\def\bracket@includegraphics[#1]{\alwaysincludegraphics[#1,draft]}
\def\includegraphics{\@ifnextchar[%
\bracket@includegraphics%
{\alwaysincludegraphics[draft]}%
}
\makeatother
\else
\let\alwaysincludegraphics\includegraphics
\fi

-
I guess that's kind of what I was just working on. Now, assuming you want to censor most of the images, but a few not (say, a logo on the title page), could you adapt this so that images included via the normal \includegraphics are censored, but images included via e.g. \alwaysincludegraphics are not censored? –  doncherry Mar 17 '12 at 14:46
I was thinking about that variant as well, gimme a minute. –  tohecz Mar 17 '12 at 15:11

If all images you include with \includegraphics are to be censored, then saying

\usepackage[draft]{graphicx}


will use the real bounding box for the images, but will print instead a box with the file name.

If not all the images are to be censored, I suggest to use for those a different command:

\newcommand{\cincludegraphics}[2][]{%
\includegraphics[draft,#1]{#2}}
%\let\cincludegraphics\includegraphics % Uncomment for "private version"


Use \cincludegraphics just like \includegraphics. For the nonpublic version it's sufficient to uncomment the following line.

This doesn't support \cincludegraphics*, but for that it's equivalent to specify the clip option.

If you also want to hide the file name, you can add (after having loaded graphicx)

\usepackage{etoolbox}
\makeatletter
\patchcmd{\Gin@setfile}{\rlap}{\@gobble}{}{}
\makeatother


In case you prefer to replace the file name with a word, you can say instead

\usepackage{etoolbox}
\makeatletter
\def\CENSORED@IMAGE#1{{\footnotesize Censored}}
\patchcmd{\Gin@setfile}{\rlap}{\CENSORED@IMAGE}{}{}
\makeatother

-