Comments in Latex

There are three ways to do comments in Latex.

1. The standard way to do comments puts in % and then everything on the
line after it is commented out:

Say $y = x^2 + \beta$. %Here is a comment.

2. If you put \usepackage{verbatim} at the start of your file, you can
do multiline comments like this:

\begin{comment}
Here is the first line of the comment.
Here is the second line.
Here is the third.
\end{comment}

3.If you put \newcommand{\comments}[1]{} at the start of your file, you
can have the best way of all:

Say $y = x^2 + \beta$.\comments{Here is my comment. }

Note that if you use \usepackage{verbatim}, it creates an odd
command that makes everything after it in the file a comment. Suppose
you write:

\comment{Here is what I wanted to be my comment.} Here is some more
writing for my paper.

Then not only will the words in the brackets be a comment, but all the
words after the brackets and on the next lines and pages too.

8 Responses to “Comments in Latex”

  1. Arvind Says:

    So, I guess latex doesn’t have a native command like JAVA or C that allows one to comment out a block of text without %’ing each line?

  2. Yifeng Zhou Says:

    When \usepackage{verbatim} is used, \comment{} will not work and make all the works after the brackets comments. But, \comments{} still works fine. Regards.

  3. willygarcia Says:

    I’m a teacher and I’m trying to write some class materials so I’d like to have questions
    and answers in the same document. Some sort of “problems book”
    and “teacher’s book” at the same time.

    That is to say I want them in the same .tex project. But with
    with some sort of redefinition (most likely redefining an environment) I
    would like to show (or not) the answers.

    What I have so far is SEARCH AND REPLACE \comment -> \answer
    which is not too great.

  4. watchmath Says:

    Thank you for the tip. I finally use your third method and it works perfectly.

  5. Qrystal Says:

    This is a good overview of the comment types, so thanks for that.

    I’m mostly writing to answer to willygarcia, even though that is a year-old comment now… but in case anyone else is looking, I found the answer to what he wants to do, on another blog:

    http://texblog.wordpress.com/2007/12/31/commenting-in-latex/

    In case that blog goes away, the commenter was Jonathan Dursi of http://www.cita.utoronto.ca/~ljdursi/ and he said the following:

    (AND I QUOTE:)
    I’ve grown quite fond of the comment environment, as it allows you to turn on and turn off the commented text. In particular, I now use it for assignments to generate the version with and without solution sets. I put the answers in section blocks like this

    \begin{answer}
    \end{answer}

    and for the version without the solutions, I just include the line

    \excludecomment{answer}

    To be fancier, and show the answers set off in shaded boxes, I also use the color and framed packages:

    \usepackage{color}
    \usepackage{framed}
    \usepackage{comment}
    \definecolor{shadecolor}{gray}{0.875}

    as comment allows you to, when showing the commented block, set it in any environment:

    \specialcomment{answer}{\begin{shaded}}{\end{shaded}}
    (END OF QUOTE.)

    Hope that helps, and even more so, I hope the formatting works when I paste his codes here! :)

  6. pepe Says:

    @willygarcia:

    You can use if-statements or redefine commands at the start of the document to get different versions of the same document. Eg. embed all answers in a \answer environment and define answer as an empty command or as a command that actually prints the answer…

  7. mike Says:

    Further to @pepe’s and @qrystal’s answers to @willygarcia’s question; see the final example here:

    http://www.troubleshooters.com/linux/lyx/ownlists.htm

    This shows how to do ‘question and answer’ sheets for a multiple choice quiz, again using environments.

  8. CY Lee Says:

    Many thanks for the commenting tips!

    Tip #3 works great. It is especially easy now for me to create two versions of my tex code that show slightly different content, with only a one-line modification, without having to create two separate files.


Bad Behavior has blocked 428 access attempts in the last 7 days.