14

I'm writing a document where some text portions are confidential and would therefore like to create an environment (e.g. confidential) which I can then exclude from the PDF by simply setting a variable or option.

Is there any way of doing this in LaTeX, or better yet, is there a package for this?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
gablin
  • 17,006

3 Answers3

17

You want the comment package. This provides the \includecomment and \excludecomment macros:

\documentclass{article}
\usepackage{comment}
\excludecomment{confidential}
\begin{document}
Some text

\begin{confidential}
Some secret text
\end{confidential}

\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
1

One option is to use newcommand and renewcommand in conjunction:

\documentclass{article}
\newcommand{\confidential}[1]{#1}
\renewcommand{\condidential}[1]{} %Comment out this whole line to surpress exclusion.
\begin{document}
    His name was \confidential{Frank}.
\end{document}

One advantage with this method is that you easily can style the text in different ways, and you can put a standard message to appear whenever something is excluded.

For example:

\newcommand{\confidential}[1]{\textbf{#1}}

makes all the included, confidential information appear in bold.

Further,

\renewcommand{\condidential}[1]{\textbf{CONFIDENTIAL}}

makes all the confidential information, when excluded, replaced with a placeholder which says: CONFIDENTIAL.

You can also easily set up different environments for different needs. For example, if you want to have different levels of confidentiality, you can have a \superConfidential environment in addition to the \confidential environment.

Speldosa
  • 5,077
  • 5
  • 31
  • 40
0

If you find the comment package (as suggested by Joseph Wright's answer) giving you trouble when using its comment environment inside some complex structures like footnotes, use the version package which appears to be a drop-in replacement:

\documentclass{article}
\usepackage{version}
\excludeversion{confidential}
\begin{document}
Some text

\begin{confidential} Some secret text \end{confidential}

\end{document}

It also works inside a footnote:

\documentclass{article}
\usepackage{version}
\excludeversion{confidential}
\begin{document}
Some text

\begin{confidential} Some secret text \end{confidential}

\footnote{A footnote. \begin{confidential} \item Some secret text \end{confidential} }

\end{document}

Thanks to muk.li for sharing this in another post.

Pierre H.
  • 103
tjanez
  • 1,016
  • Notice that description of version on CTAN warns "Although the command syntax is very similar to that of comment, comment.sty is to be preferred to version.sty for documents where significant chunks of text may be excluded". I don't know the rationale though... – Pierre H. Nov 27 '20 at 15:27