# Censor minted source code in pdf

I'm using minted in my publication to embed my source code examples with proper highlighting. Unfortunately, they are under non-disclosure agreement (NDA), so I have to blank them out before publishing the paper on the internet. I tried the censor package, more specifically \censorbox, but it breaks my typesetting when I use it on the minted environment.

Can someone give me a simple example of how to remove my source code from the pdf, but keep the layout (replace with blank or black lines)?

begin{listing}[H]
\censorbox{
\begin{minted}{cpp}
// some CPP code ...
\end{minted}
}
\caption[Caption]{Listing Caption}
\label{lst:code}
\end{listing}

• Can you provide a short example of (non)working code? As the author of censor, I doubt it will work with minted, but if I saw some code, I might be able to take a stab. Welcome to the site. – Steven B. Segletes Mar 31 '16 at 11:21
• Thanks for the super quick reply. I added some code to my original post. – Tim Lehr Mar 31 '16 at 11:26
• Could you expand your code to include preamble material, so that we know what packages you are loading (at least those packages necessary to demonstrate this problem)? – Steven B. Segletes Mar 31 '16 at 11:40
• It appears that I am unable to execute minted code on my system (I lack pygmentize) We'll see if someone else can assist. – Steven B. Segletes Mar 31 '16 at 11:56
• Just a suggestion: you might be better off using the multiaudience package; for your "internal" audience you have the code, for the "internet" audience, you could have a box explaining why there's no code to show, rather than a black blob. – Brent.Longborough Mar 31 '16 at 15:53

It is hard to have the minted environment itself as an argument to \censorbox, because it contains verbatim material. At the time where it is passed as an argument, the catcodes are fixed and the verbatimness is basically broken. There are workarounds for this, but I will take another approach, namely saving the output of the minted environment in a box and then using the box. The advantage is that a box always contains completely typeset material and thus doesn't have to deal with immutable catcodes.

# Enclosing in a vbox

Inspired by this answer (Marco Daniel, 2013) I simply enclosed the minted environment in a vbox called \mintedbox. This vbox can then be used with \box\mintedbox, which also works inside of \censorbox.

\documentclass{article}
\usepackage{minted,etoolbox,censor}
\newbox\mintedbox
\BeforeBeginEnvironment{minted}{\setbox\mintedbox=\vbox\bgroup}%
\AfterEndEnvironment{minted}{\egroup}%
\begin{document}
\begin{listing}[H]
\begin{minted}{cpp}
#include <iostream>

// some CPP code ...
int main()
{
std::cout << "Hello World!" << std::cout;
return 0;
}
\end{minted}
\censorbox{\box\mintedbox}
\caption[Caption]{Listing Caption}
\label{lst:code}
\end{listing}
\end{document}


If you dislike this syntax, you can have this as an environment.

\newenvironment{censorenv}%
{\setbox0=\vbox\bgroup}%
{\egroup\censorbox{\box0}}
\begin{censorenv}
\begin{minted}{cpp}
// some CPP code ...
\end{minted}
\end{censorenv}


# Hacking fancyvrb

This is a very hackish solution. I copied the definition of SaveVerbatim and modified it to always save to a box named minted. Then I redeclared Verbatim to call the modified mintedSaveVerbatim instead. Now, when you use \begin{minted}...\end{minted} the content is not printed but saved to a box called minted. You can then place this box using \BUseVerbatim{minted}. This one will then also work in \censorbox.

\documentclass{article}
\usepackage{minted,censor}
\makeatletter
\def\mintedSaveVerbatim{\FV@Environment{}{mintedSaveVerbatim}}
\def\FVB@mintedSaveVerbatim{%
\@bsphack
\begingroup
\FV@UseKeyValues
\def\SaveVerbatim@Name{minted}%
\gdef\FV@TheVerbatim{}%
\def\FV@ProcessLine##1{%
\expandafter\gdef\expandafter\FV@TheVerbatim\expandafter{%
\FV@TheVerbatim\FV@ProcessLine{##1}}}%
\gdef\FV@TheVerbatim{}%
\FV@Scan}
\def\FVE@mintedSaveVerbatim{%
\expandafter\global\expandafter\let
\csname FV@SV@\SaveVerbatim@Name\endcsname\FV@TheVerbatim
\endgroup\@esphack}
\RecustomVerbatimEnvironment{Verbatim}{mintedSaveVerbatim}{}
\begin{document}
\begin{listing}[H]
\begin{minted}{cpp}
#include <iostream>

// some CPP code ...
int main()
{
std::cout << "Hello World!" << std::cout;
return 0;
}
\end{minted}
\censorbox{\BUseVerbatim{minted}}
\caption[Caption]{Listing Caption}
\label{lst:code}
\end{listing}
\end{document}