Comments in LaTeX source (percent sign, comment environment)

A percent sign % starts a comment: LaTeX ignores everything from it to the end of the line. For several lines, the verbatim package has the comment environment.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{verbatim}

Comment to the end of the line

Everything after % in the same line is dropped, including the line break. A percent sign that should be printed is written \%.

This text is printed. % This note is not.
% A whole line as a comment.
The price rose by 5\%.
Result
The compiled example

Open in the editor

Comment out several lines

The comment environment from the verbatim package skips everything between \begin{comment} and \end{comment}. Put \end{comment} on a line of its own: text after it on the same line is dropped with a warning.

Visible text.
\begin{comment}
None of these lines
appears in the PDF.
\end{comment}
More visible text.
Result
The compiled example

Open in the editor

Without a package

\iffalse skips everything up to the matching \fi. This works without a package, also in the middle of a line.

Visible text.
\iffalse
This part is skipped.
\fi
More visible text.
Result
The compiled example

Open in the editor

Percent sign at the end of a line

A line break in the source counts as a space. A % at the end of the line hides it. In a definition that runs over several lines, this keeps unwanted spaces out of the output.

\newcommand{\important}[1]{%
  \textbf{#1}%
}
A word in \important{bold} without extra spaces.
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages