# Not rendering (“commenting”) texts with specific colors

Suppose I have made notes, either inline or a couple of paragraphs, in different colors (e.g., \textcolor{Aquamarine}{...}, \textcolor{cyan}{...}, \textcolor{Orange}{...}, etc.). Now I want to treat all texts in Orange and cyan as comments such that they are removed in the compiled PDF, without having to manually wrap them between \begin{comment} ... \end{comment} (of comment package).

Is there a nice way of doing that, like a command/macro (using \iffalse ... \fi?) that takes a list of colors to be omitted and does the job?

• Are you able to replace all your \textcolor commands with a custom macro (that either outputs colour or nothing depending on a list ) – Chris H Apr 12 at 8:45

My approach would be the following: define you own macro. For example, \myorangetext; then use a global search and replace to change every occurrence of textcolor{Orange} into myorangetext; rinse, repeat.

Now you can do simply this thing:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{xstring}
\newcommand{\EatOneArg}[1]{}
\newcommand{\myorangetext}{\textcolor{Orange}}
\newcommand{\myaquatext}{\textcolor{Aquamarine}}
%
% comment or uncomment these
%
% \renewcommand{\myorangetext}{\EatOneArg}
\renewcommand{\myaquatext}{\EatOneArg}

\begin{document}
And this is my code
\myaquatext{Some text Aquamarine},
some text without color,
\myorangetext{some text in Orange}
\end{document}


Commenting or not each redefinition of your command you can change the appearance of it... and if in the future you want to change the Orange text into for example a footnote you can do it...

\renewcommand{\myaquatext}{\footnote}


The main philosophical difference here is that, defining your own macro, you do not state how to print some text, but what the text IS: this is the fundamental idea about LaTeX: describe semantically, not visually, keep content and visualization as separate as possible.

(So, really, do not name the macros \myorangetext and so on: call them \commentforFred or \commentforProf or whatever they really mean)

• Wow, super elegant! Thanks :) – ManuHaq Apr 12 at 10:42

Depending on the \commentColor you set, and redefining \textcolor, here is a solution.

\documentclass{article}
\usepackage[svgnames]{xcolor}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
\usepackage{xstring}
\let\textcolorold\textcolor
\def\commentColor{Orange}
\RenewDocumentCommand\textcolor{mm}{\IfStrEq{#1}{\commentColor}{}{\textcolorold{#1}{#2}}}

\begin{document}
And this is my code
\textcolor{Aquamarine}{Some text}, \textcolor{cyan}{Not a comment}, \textcolor{Orange}{considered as comment}

\end{document}

• Awesome! There is an issue, though: I cannot do that for multiple comments at the same time. Any possible fix? – ManuHaq Apr 12 at 9:44
• It works, so +1, but it is very danger to change internal commands... – Rmano Apr 12 at 9:53
• @Rmano Sure, it's an ugly fix! It would be better to define a new macro for that, but that's not what OP want (for what I understand…) – NBur Apr 12 at 10:08
• I'm a newbie in TeX, so to me command and macro were indistinguishable. @Rmano's answer is indeed a better solution. – ManuHaq Apr 12 at 10:44

A version which allows you to set the colours to be ignored in a comma-separated list. Of course I absolutely agree with Rmano that you shouldn't use \textcolor directly but rather define your own macro.

I use the kernel macro \in@ to check whether the colour is contained in the list; putting the two commas around the arguments is a safety (taken from https://tex.stackexchange.com/a/260921/82917) against colours sharing part of the name. (I don't know if that would really be an issue but better safe than sorry.)

\documentclass{article}

\usepackage{xcolor}

\newcommand*{\excludedcolors}{}

\makeatletter
\newcommand*{\next}{}% better safe than sorry
\newcommand*{\mycomment}[2]{%
\edef\next{\noexpand\in@{,#1,}{,\excludedcolors,}}\next
\ifin@
\def\next{\unskip}%
\else
\def\next{\textcolor{#1}{#2}}%
\fi
\next
}
\makeatother

\newcommand*{\dummytext}{%
See \mycomment{red}{foo bar}, and \mycomment{blue}{blue text}
but also some \mycomment{teal}{text in teal}.%
}

\begin{document}

\dummytext

\renewcommand*{\excludedcolors}{blue}
\dummytext

\renewcommand*{\excludedcolors}{teal}
\dummytext

\renewcommand*{\excludedcolors}{red,teal}
\dummytext

\end{document}


• Thanks. Sorry, for some reason I missed notification from your answer. I am wondering if your solution would be a better way of doing it or that of @Rmano. – ManuHaq May 4 at 23:19
• @ManuHaq Rmano's and my solutions do slightly different things, so it's somewhat pointless to discuss which one is "better". Of course it's possible to combine the two approaches but the in the end what matters if what you need and how you are going to use them. – campa May 7 at 7:52