Comments in LaTeX

As a reminder to myself here’s a bit of LaTeX information on adding comments (that can be turned on/off) in a document. First off use the comment package:

\usepackage{comment}

For comments that enclose stuff that won’t be seen just use the environment:

\begin{comment}
Blah, blah, blah...
\end{comment}

However, if you want to turn comments on or off (so they appear or don’t appear in your final document) you have to place:

\includecomment{comment}, or \excludecomment{comment}

before the \begin{document} command. In fact the “comment” environment name can be anything you want.

You can also have special comments that allow you to change the style of the text within the comment (this is very similar to creating a new environment). So, if I wanted to create a comment environment called notes inside which I wanted all the text to be sans serif and the colour blue I could define:

\specialcomment{notes}{\begingroup\sffamily\color{blue}}{\endgroup}

and within the body of the document use:

\begin{notes}
Here's some stuff that I want to note...
\end{note}

Now, I might want to be able to specify whether to turn the notes on or off when I compile the document. I can do this using the ifthen package and some command line LaTeX e.g. within the document have something like:

\usepackage{comment}
\usepackage{ifthen}
\usepackage{color}

\specialcomment{notes}{\begingroup\sffamily\color{blue}}{\endgroup}

\ifthenelse{\isundefined{\withnotes}}{
  % exclude the comments
  \excludecomment{notes}
}{
  % include the comments
  \includecomment{notes}
}

In this document the \withnotes command is undefined, so by default the notes will be excluded. However, when compiling the document (called, say, mydoc.tex) you can chose to define this command and show the notes e.g.

:~> pdflatex '\newcommand{\withnotes}{}\input{mydoc.tex}'
:~> pdflatex '\newcommand{\withnotes}{}\input{mydoc.tex}'

Or, maybe put this in a Makefile e.g.:

# Make file for mydoc.tex

# default to make document with notes
default: withnotes

withnotes: mydoc.tex
    pdflatex '\newcommand{\withnotes}{}\input{mydoc.tex}'
    pdflatex '\newcommand{\withnotes}{}\input{mydoc.tex}'

withoutnotes: mydoc.tex
    pdflatex mydoc.tex
    pdflatex mydoc.tex

A similar way to do this, but without the easy ability to turn the comments on and off would be to create a new environment like:

\newenvironment{notes}{\sffamily\color{blue}}

5 Replies to “Comments in LaTeX”

  1. Hi Matt,
    When I do:
    \usepackage{comment}
    \usepackage{ifthen}
    \usepackage{color}

    \specialcomment{notes}{\begingroup\sffamily\color{blue}}{\endgroup}

    \newcommand{\withnotes}{}

    \ifthenelse{\isundefined{\withnotes}}{
    % exclude the comments
    \excludecomment{notes}
    }{
    % include the comments
    \includecomment{notes}
    }

    The comment in notes doesn’t appear in blue. If I don’t do the \ifthenelse business and I simply define
    \specialcomment{notes}{\begingroup\sffamily\color{blue}}{\endgroup}
    then the comment does appear in blue.

    Any ideas why?

    S.

  2. I use nearly the identical approach (in fact, I wonder if you borrowed it from me?) in quiz templates so I can quickly generate solution keys. Here’s a template quiz:

    http://hg.tedpavlic.com/courses/osu/ece327/file/tip/quiz_template/weekly_quiz.tex

    What’s great is that you can also change the LaTeX JOBNAME from the command line using latex or pdflatex. So you can have a Makefile (which I do) or similar that will generate a “quiz.pdf” and a “quiz_solution.pdf” simultaneously (or separately, if you wish).

  3. Hi Ted, thanks for your comment. It’s possible that I got this solution off you – I found it by some random Googling, but can’t be sure were it came from.

Leave a Reply