3

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?

Thanks in advance.

1
  • 2
    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
    Commented Apr 12, 2021 at 8:45

2 Answers 2

6

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)

1
  • Wow, super elegant! Thanks :)
    – psyguy
    Commented Apr 12, 2021 at 10:42
5

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}
4
  • Awesome! There is an issue, though: I cannot do that for multiple comments at the same time. Any possible fix?
    – psyguy
    Commented Apr 12, 2021 at 9:44
  • 1
    It works, so +1, but it is very danger to change internal commands...
    – Rmano
    Commented Apr 12, 2021 at 9:53
  • 1
    @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
    Commented Apr 12, 2021 at 10:08
  • 1
    I'm a newbie in TeX, so to me command and macro were indistinguishable. @Rmano's answer is indeed a better solution.
    – psyguy
    Commented Apr 12, 2021 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .