98

In tex there is a way to do a comment multiline like in C, C++ /* comment */ or in HTML <!-- comment -->? I'm currently using \ifx

\ifx true false
My multiline comment
that will not be in the
output pdf
\fi

but this may create a problem if I put inside the comment something like \someundefcommand. I wish something like the standard % comment so I can put inside the comment I want.

1

3 Answers 3

91

Use the verbatim package.

\documentclass{article}
\usepackage{verbatim}
\begin{document}
\begin{comment}
some comment
\someundefinedcommand
\end{comment}
a
\end{document}
3
  • 24
    Hmm. Why does verbatim provide this? I would expect to find it in its own package. Did the functionality fall out as a natural by-product of verbatim's implementation? Commented Feb 13, 2012 at 12:24
  • 2
    Is it possible to define the \begin{comment} and \end{comment} environment markers in terms of a character sequences - similarly to how the curly braces are defined? Commented Mar 22, 2017 at 13:51
  • should there be new line after \begin{comment} ?
    – alper
    Commented Sep 29, 2022 at 12:22
58

Instead \ifx true false you may use the shorter

\iffalse
dsaads
fdfdfds
\fi

If you ever want to have to 'activate' your comments later, you may define your own if:

\documentclass{article}

\newif\ifcomment
%\commenttrue # Show comments
\begin{document}

b

\ifcomment
dsaads
fdfdfds
\fi

a
\end{document}

But I would not recommend the \ifcomment. There are packages for this (the already mentioned verbatim or comment or version or versions).

Another interesting approach could be todonotes. Just don't show the todos. If you need them, you may define, where you get them (margin, footnote, own pages...)

24

A similar solution I came up with is to define a \comment command:

\newcommand{\comment}[1]{}

The command makes LaTeX ignore anything inside. This solution has the advantage over the other solutions (for me) of being a command rather than an environment with \begin/\end commands.

Usage:

Text that will be in the final document.
\comment{I am thinking of including this text,
but I don't want it in the compiled document right now.}
More text.

This can be modified à la @knut's answer to have an option for toggling the inclusion of the comments in the final document:

\newif\ifcomment
\commenttrue % Show comments
\newcommand{\comment}[1]{\ifcomment#1\fi}
2
  • 1
    you can also use a delimited macro: \long\def\comment #1\endcomment{}. And as an alternative to using an \if, a companion command \newcommand\showcomments{\let\comment\empty\let\endcomment\empty}. The comment text should not be braced then to not create a group if the comments are to be shown. However, if the need arise to comment a whole block already containing commented out material, then do \comment{...the whole thing...}\endcomment.
    – user4686
    Commented Nov 12, 2013 at 18:49
  • I really like this idea. Because it makes it easier to clean your documents once your ready to submit. Simply comment out the command and see where it crashes. I do this for my todo's
    – magu_
    Commented Apr 8, 2016 at 23:49

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