2

For annotating documents, I've defined a command like so:

\DeclareDocumentCommand \commment { o m } {%
    \stepcounter{commentcounter}
    \ifhidecomments
    \else \IfNoValueTF{#1}{%
        \textsf{\textcolor{Red}{\footnotesize[Comment:\;#2]}}%
    }{%
        \textsf{\textcolor{lightgray}{\footnotesize[Comment:\;#2]}}%
    }%
    \fi
}

In the text you can embed a comment by doing \comment{blah blah blah} and it will render like [Comment: blah blah blah] in the text, but in red. If you want the comment to render but in gray instead (to keep it around but indicate it's been addressed), you can do \comment[]{blah blah blah}, i.e. pass an empty optional argument.

Now, what I want is to be able to hide all comments without a trace with a single command (as opposed to manually removing them). I have an if-else statement in my DocumentCommand that doesn't render the comment if \hidecommentstrue is specified in the document. When I used that command, indeed all the comments are hidden. Unfortunately, it can mess with the spacing between words. For instance, if I have word1 \comment{blah} word1 and I do \hidecommentstrue, there's extra space between word1 and word2 that is not there if I actually remove the \comment.

Is there something I can add to my command that would fix this? Or is there no way around removing the comments manually?

Here is a minimum working example:

\documentclass{article}
\usepackage{lipsum}
\usepackage[dvipsnames]{xcolor}

\usepackage{totcount}
\newif\ifhidecomments
\newtotcounter{commentcounter}
\setcounter{commentcounter}{0}
\DeclareDocumentCommand \comment { o m } {%
    \stepcounter{commentcounter}
    \ifhidecomments
    \else \IfNoValueTF{#1}{%
        \textsf{\textcolor{Red}{\footnotesize[Comment:\;#2]}}%
    }{%
        \textsf{\textcolor{lightgray}{\footnotesize[Comment:\;#2]}}%
    }%
    \fi
}

% \hidecommentstrue

\begin{document}

\lipsum[1]
\comment{this is a comment}
\lipsum[1] 

\end{document}
2
  • Please, post a MWE with all code we need to test it. Your code does not compile.
    – Sigur
    Commented May 3, 2023 at 1:34
  • @Sigur Apologies, added one now.
    – rcplusplus
    Commented May 3, 2023 at 4:34

1 Answer 1

1

The problem is a kind of: unexpected space because of not commented end-of-line. you can fix it either using one more percent and \ignorespaces:

\documentclass{article}
\usepackage{lipsum}
\usepackage[dvipsnames]{xcolor}

\usepackage{totcount}
\newif\ifhidecomments
\newtotcounter{commentcounter}
\setcounter{commentcounter}{0}
\DeclareDocumentCommand \comment { o m } {%
    \stepcounter{commentcounter}% <-- percent needed!
    \ifhidecomments
      \ignorespaces
    \else
    \IfNoValueTF{#1}{%
        \textsf{\textcolor{Red}{\footnotesize[Comment:\;#2]}}%
    }{%
        \textsf{\textcolor{lightgray}{\footnotesize[Comment:\;#2]}}%
    }%
    \fi
}

\begin{document}

\lipsum[1]
\comment{this is a comment}
\lipsum[1]

\hidecommentstrue
\lipsum[1]
\comment{this is a comment}
\lipsum[1]

\end{document}

enter image description here

or two more percent and one space:

\documentclass{article}
\usepackage{lipsum}
\usepackage[dvipsnames]{xcolor}

\usepackage{totcount}
\newif\ifhidecomments
\newtotcounter{commentcounter}
\setcounter{commentcounter}{0}
\DeclareDocumentCommand \comment { o m } {%
    \stepcounter{commentcounter}% <-- percent needed!
    \ifhidecomments
    \else
    \IfNoValueTF{#1}{%
        \textsf{\textcolor{Red}{\footnotesize[Comment:\;#2]}}%
    }{%
        \textsf{\textcolor{lightgray}{\footnotesize[Comment:\;#2]}}%
    } % <-- space before percent added
    \fi
}

\begin{document}

\lipsum[1]
\comment{this is a comment}%
\lipsum[1]

\hidecommentstrue
\lipsum[1]
\comment{this is a comment}%
\lipsum[1]

\end{document}

BTW: With the shown definition, if would IMHO make more sense to define, a \comment with star variant, e.g.,

\DeclareDocumentCommand \comment { s m } {%
    \stepcounter{commentcounter}% <-- percent needed!
    \ifhidecomments
    \else
    \IfBooleanTF{#1}{%
        \textsf{\textcolor{lightgray}{\footnotesize[Comment:\;#2]}}%
    }{%
        \textsf{\textcolor{Red}{\footnotesize[Comment:\;#2]}}%
    } % <-- space before percent added
    \fi
}

instead of using testing for an empty optional argument.

2
  • Amazing, it worked like a charm! I decided to go with \ignorespaces (and of course adding the needed % as well), just because it seems clearer when looking over the code what's actually happening.
    – rcplusplus
    Commented May 3, 2023 at 6:03
  • Also using a star seems like a better idea for sure, thanks!
    – rcplusplus
    Commented May 3, 2023 at 6:05

You must log in to answer this question.

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