A macro dependent on a parameter

I would like to write a macro privatecomment whose behaviour depends on a global parameter show.

For example, in the .tex, I could write \privatecomment{Here is a comment}. When I set show to true, it prints Here is a comment in .pdf; otherwise, it prints nothing.

Could anyone tell me how to realise that?

You could do

\newif\ifshow
\showtrue
\def\privatecomment#1{\ifshow#1\fi}

but really it's simpler and more efficient to do

\def\showtrue{\def\privatecomment##1{##1}}
\def\showfalse{\def\privatecomment##1{}}
\showtrue

A simple if switch without any frillz.

\documentclass{article}
\begin{document}
\privatecomment{Shows by default}

\privatecomment{Turned off}

\privatecomment{On again.}

\end{document}

A proper way is to use \@bsphack and \@esphack:

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newcommand{\privatecomment}[1]{%
\textcolor{red!50}{#1}%
\else
\@bsphack\@esphack
\fi
}
\makeatother

\begin{document}

This is text \privatecomment{This is a comment}
and the text continues.

\privatecomment{A comment between paragraphs}

Some text again.

This is text \privatecomment{This is a comment}
and the text continues.

\privatecomment{A comment between paragraphs}

Some text again.

\end{document}

• This nicely eliminates whitespace, but not if this \privatvecomment{stuff}! and \showprivatecommentfalse. This results in this !. Is there a workaround for that? – A.Ellett May 21 '15 at 16:19
• @A.Ellett There should be no space before \privatecomment in this case, as the ! immediately follows this. – egreg May 21 '15 at 16:21
• That's not what happened when I tested it. I had to add \unskip. – A.Ellett May 21 '15 at 16:24

In the documents for my classes, I set lots of flags to either show answer or hide answers, to create a slightly different version of the document, for all sorts of purposes. I generally don't like having to go into the document and change something to create these modifications. Instead, I create a directory I call ./.design in which I place various files whose existence (or lack thereof) determines which modifications take effect.

My answer is here is about how to build an interface at the command line to facilitate changing how the file gets compiled. The other answers show you how to implement those changes once they've been made.

So for example, I might have the following files in my ./.design directory:

two_column_doc.false
compile_practice_version.false

That way, at a glance I can see how I've set up the document. I've also created a perl script that allows me to easily toggle or change these settings (for my private purposes not all of them are binary).

For your command, here's an approach to you question following my idea.

First some perl code:

#!/usr/bin/perl
use strict 'vars';
&MAIN(@ARGV);
sub MAIN {
my $fh_true = "./.design/show_comment.true"; my$fh_false = "./.design/show_comment.false";
if ( -e $fh_true ) { system("mv$fh_true $fh_false"); } elsif ( -e$fh_false )
{
system("mv $fh_false$fh_true");
}
else
{
system ("touch \$fh_true");
}
}

The perl code can be called from the command line. It does one of two things, it either toggles the name of the file between ./.design/show_comment.true and ./.design/show_comment.false or, if neither file exists, it creates ./.design/show_comment.true as a default value. All levels of complexity can be added to such a perl script.

And then the document you wish to create can be written as

\documentclass{article}

\makeatletter
\newcommand\privatecomment[1]{%%
\IfFileExists{./.design/show_comment.true}
{#1''}
{\unskip\@bsphack\@esphack}}
\makeatother

\begin{document}

Hello \privatecomment{world} and more stuff.

\end{document}

Here's the result when ./.design/show_comment.true has been created:

There are some problems with this particular definition. When ./.design/show_comment.true does not exist we get the following result: