# macro that defines a comment

The Wikibooks book on LaTeX mentions the following way of defining a macro for (possibly multi-line) comments:

\newcommand{\comment}[2]{#2}


with the argument that

\newcommand{\comment}[1]{}


can produce unwanted space.

But is this really the best way? Does this macro work in all contexts, even at the very end of a file? What is the best way of defining a comment macro that works in the largest number of circumstances? (Of course there is no way to define a comment macro that works everywhere.)

-
I'm even more convinced that the Wikibooks on LaTeX are not to be recommended. –  egreg Jan 21 '13 at 11:10
@egreg Yeah, there is lots of room for improvement there. –  Lover of Structure Jan 21 '13 at 12:31

Any definition as a command isn't really a comment, You can not comment out % or { for example, and it is not invisible to other commands.

\fbox
%  \ { \hhhh
{aaa}


Boxes aaa as the real comment with % hides all the junk in the line, but any \comment macro will not work in that position, even

\fbox
\comment{xxx}
{aaa}


The argument to \fbox is just \comment and {xxx} and {aaa} are still in the input stream and things go badly wrong.

! Argument of \comment has an extra }.
<inserted text>
\par
l.24      \comment
{xxx}


If you add \ignorespaces to avoid spacing issues then it doesn't expand to nothing so that

\newcommand\comment[1]{\ignorespaces}

\typeout{aaaa\comment{bbb}ccc}


produces

aaaa\ignorespaces ccc


whereas

\newcommand\comment[1]{}

\typeout{aaaa\comment{bbb}ccc}


produces

aaaaccc


Using a trailing #2 to remove space is dangerous unless you know you are in a very restricted context where there will be no {} groups.

\newcommand\comment[2]{#2}
\comment{aaa} {\bfseries bbb} ccc


The ccc will be in bold as the \comment macro will have taken the entire {\bfseries bbb} as #2 but only returned \bfseries bbb without the brace group to the input stream, so the \bfseries is no longer scoped to just bbb.

-
Do aaaa\ignorespaces ccc and aaaaccc produce the same output, or, if not, what is the difference between them? –  Lover of Structure Jan 21 '13 at 12:52
In a message to the user printed on the terminal and in the log the difference between aaaaccc and aaaa\ignorespaces ccc is that the second one has the unwanted characters \ignorespaces  in the middle of the message. If you are not in a message it depends what aaaa really is for example if it is a test with \@ifnextchar (eg looking for an optional argument) then it will see c (or perhaps [ in a real example) in one case but will see \comment in the other. –  David Carlisle Jan 21 '13 at 13:09
Answer taken for the details, though the credit is shared with user @Werner. –  Lover of Structure Jan 27 '13 at 0:30

For me it makes more sense to use

\newcommand{\comment}[1]{\ignorespaces}


since it takes a single argument, gobbles it and ignores any spaces afterwards. Here are some examples of its usage relative to other \comments:

\documentclass{article}
\newcommand{\commentA}[1]{}
\newcommand{\commentB}[2]{#2}
\newcommand{\commentC}[1]{\ignorespaces}
\begin{document}
\commentA{This is a long comment and can extend over multiple lines, etc.} But it won't show. \par
\commentB{This is a long comment and can extend over multiple lines, etc.} But it won't show. \par
\commentC{This is a long comment and can extend over multiple lines, etc.} But it won't show. \par
Something before. \commentA{This is a long comment and can extend over multiple lines, etc.} But it won't show. \par
Something before. \commentB{This is a long comment and can extend over multiple lines, etc.} But it won't show. \par
Something before. \commentC{This is a long comment and can extend over multiple lines, etc.} But it won't show. \par
\end{document}

-
+1 for its straightforwardness; this is what I'll be using. –  Lover of Structure Jan 27 '13 at 0:29