LaTeX Comments: Single-Line, Multi-Line and Block Comments
A LaTeX comment starts with % and runs to the end of the line. That is the whole built-in syntax: there is no multi-line comment in TeX itself. This guide covers the percent sign and its one surprising side effect, the three ways to comment out a block (comment package, verbatim package, \iffalse), editor shortcuts, todo notes in the margin, and how to keep draft-only content out of the final PDF.
Single-Line Comments with %
Everything after % on a line is ignored by the compiler. It can sit on a line of its own or after some code:
% This whole line is a comment.
\section{Results} % the section title is typeset, this note is not
The experiment ran for 12 hours. % TODO: check the exact durationWhy % at the end of a line matters
A comment also removes the line break itself. Normally TeX turns the end of a line into a space; % discards that space together with any leading whitespace on the next line. That is why package code and macro definitions end lines with %: without it, every line break inside the definition would leak a space into the output.
\newcommand{\mylogo}{%
\textbf{Oc}%
\textit{tree}%
}
% Without the three % signs, "Oc tree" would be printed with a space.The same trick joins two words across a line break in running text: super% followed by script on the next line prints “superscript”.
A literal percent sign
To print % in the text, escape it with a backslash. Forgetting this is a classic bug: the rest of the line silently disappears.
Accuracy improved by 12\% over the baseline. % correct
Accuracy improved by 12% over the baseline. % everything after 12 is goneMulti-Line Comments with the comment Package
The comment package provides a comment environment whose body is skipped entirely. The text inside does not have to be valid LaTeX: unbalanced braces, broken commands and half-written environments are all fine.
\usepackage{comment}
\begin{comment}
This paragraph is a first draft { with an unbalanced brace
and a \badcommand that does not exist.
\begin{figure} % never processed
\end{comment}One strict rule: \begin{comment} and \end{comment} must each stand on a line of their own, with no leading spaces and nothing after them. The package reads the file line by line looking for exactly that line, so text \end{comment} or an indented \end{comment} is not recognised and LaTeX stops with Runaway argument? File ended while scanning use of \next.
Named blocks: \includecomment and \excludecomment
The same package lets you define your own environments and switch them on or off from the preamble, which is how many people manage solutions, reviewer-only notes or alternative versions:
\usepackage{comment}
\includecomment{solution} % typeset solution blocks
\excludecomment{note} % drop note blocks
\begin{solution}
The answer is 42.
\end{solution}
\begin{note}
Reviewer 2 asked for a longer proof here.
\end{note}Swap \includecomment for \excludecomment to produce the student version. The begin and end lines follow the same line-of-their-own rule.
The verbatim Package's comment Environment
If you already load verbatim (for \verbatiminput, say), it provides a comment environment too, with the same name and the same effect. Do not load both packages; pick one (loading verbatim then comment fails with Bad space factor at the first \end{comment}).
\usepackage{verbatim}
\begin{comment}
Skipped, just like with the comment package.
\end{comment}Its implementation is a little more forgiving: \end{comment} may be indented. Anything else on that line is dropped with a warning, so the habit of keeping it alone on its line still applies.
\iffalse ... \fi: Commenting Out Without a Package
TeX's conditional primitives give you a block comment with no package at all. Text between \iffalse and \fi is skipped:
\iffalse
This block is not typeset. Braces { do not } need { to balance.
\fiThere is one rule to remember. While skipping, TeX still counts conditionals, so every \if... inside the block must have its own \fi, and the block must not contain a stray \fi or \else: the first unmatched one ends the comment early and the rest is typeset. This bites when you comment out code that itself uses conditionals. Braces, on the other hand, do not have to balance for TeX – but your editor's brace matching and code folding will get confused, which is the practical reason to prefer the comment package for long blocks.
You will also see \if0 ... \fi in older documents. \if compares the next two character tokens, so \if0 followed by a line break compares 0 with a space and is false. The only edge case is \if0 immediately followed, with no space or line break, by 0 or a macro expanding to it; \iffalse has no such edge case.
A macro that swallows its argument
A third no-package option is a command that discards whatever it is given:
\newcommand{\ignore}[1]{}
\ignore{This sentence is read by TeX but never typeset.}Here the braces must balance and \verb cannot appear inside, because TeX reads the argument as normal tokens. Do not call it \comment: that name is used internally by the comment environment.
Commenting Out in Your Editor
For a few lines, the fastest route is the editor's toggle-comment shortcut, which inserts or removes % at the start of every selected line:
- VS Code (with LaTeX Workshop) and Overleaf: Ctrl+/ (Cmd+/ on macOS)
- TeXstudio and Texmaker: Ctrl+T to comment, Ctrl+U to uncomment
- Octree: comments are highlighted in the editor, and you can select a block and ask the AI assistant to comment it out, wrap it in a
commentenvironment, or restore it
Todo Notes in the Margin: todonotes
Comments are invisible in the PDF. For notes that should be seen by co-authors, use the todonotes package, which draws colored boxes in the margin or inline:
\usepackage[colorinlistoftodos]{todonotes}
\listoftodos % optional: a list of all notes, like a table of contents
The results\todo{Add the 2026 data} are summarised in Table~2.
\todo[inline]{Rewrite this section after the review.}
\todo[color=green!30, author=Ada]{Double-check the units.}
\todo[noline]{A note without the connecting line.}
\missingfigure{Plot of accuracy versus training time}\todo{...}– margin note with a line pointing at the text\todo[inline]{...}– a full-width box in the text flow; use this inside floats and other places where margin notes are not allowed\missingfigure{...}– a placeholder box for a figure you still have to make\listoftodos– a list of every note, with page numbers
To remove every note from the final version, load the package with the disable option: \usepackage[disable]{todonotes}. All commands still exist, so the document compiles unchanged. With obeyFinal, notes disappear automatically when the class option final is set.
Notes squeezed or in the wrong place
Margin notes are \marginpars, so they need a wide enough margin. If todonotes prints “The length marginparwidth is less than 2cm” or the boxes overlap the text, set the width before loading the package, and pass the same value as the note width:
\setlength{\marginparwidth}{2.5cm}
\usepackage[textwidth=2.5cm]{todonotes}
% Notes on the wrong side of a two-sided document:
\reversemarginparAlternatives: todo, fixme and changes
The todo package is a lighter cousin: \todo{...} and \todos for the list, without TikZ. The fixme package grades notes by severity (\fxnote, \fxwarning, \fxerror, \fxfatal) and, in its final mode, turns any remaining \fxfatal into a compile error so nothing slips through. The changes package tracks edits rather than notes: \added, \deleted and \replaced show colored markup in drafts and collapse to clean text with \usepackage[final]{changes}.
Notes to the Console: \typeout and \message
Sometimes the note is for whoever runs the compiler, not the reader. \typeout prints a line to the terminal and the .log file; \message is the underlying TeX primitive and does not start a new line.
\typeout{*** Draft build: Table 2 still uses placeholder numbers ***}
\message{compiling chapter 3}Comments Inside tabular and align
% works anywhere, including table rows and multi-line equations. The one thing to watch is position: a comment after & but before \\ swallows the \\, so the next row runs into this one and LaTeX stops with Extra alignment tab has been changed to \cr. Put the comment after the row terminator:
\begin{tabular}{lr}
Item & Value \\ % header row
A & 12 % WRONG: the \\ on this line is now part of the comment \\
B & 7 \\ % this row now collides with the one above
\end{tabular}
\begin{align}
f(x) &= x^2 + 1 \\ % definition
f'(x) &= 2x % derivative - no \\ needed on the last line
\end{align}Commenting out an entire row works the same way: put % at the very start of the line, so that the row's \\ goes with it.
Gotcha: Comments Inside verbatim
Inside verbatim, lstlisting and \verb, every character is printed literally. A % there is not a comment, and a \begin{comment} there is not skipped – both appear in the output. The reverse is also true: a comment environment cannot contain \end{comment} on its own line, because that line ends the comment even inside a listing. If you need to show the comment environment in a code sample, write the closing line with a space in front or break it up.
Draft and Final Versions with Conditionals
When the same source has to produce two documents – a draft with notes and a clean final copy – a single switch in the preamble is cleaner than commenting blocks in and out. Plain TeX's \newif is enough:
\newif\ifdraft
\drafttrue % change to \draftfalse for the final version
\ifdraft
\usepackage[colorinlistoftodos]{todonotes}
\else
\usepackage[disable]{todonotes}
\fi
\begin{document}
\ifdraft
\textbf{DRAFT -- not for distribution}
\fi
...Note that the ifdraft package defines a command with the same name, \ifdraft{draft code}{final code}, keyed to the class option draft; use one or the other, not both. The etoolbox package offers the same idea with less TeX syntax:
\usepackage{etoolbox}
\newtoggle{draft}
\toggletrue{draft} % \togglefalse{draft} for the final version
\iftoggle{draft}{%
\todo[inline]{Still missing: related work.}%
}{}Complete Example
\documentclass{article}
\usepackage{comment}
\setlength{\marginparwidth}{2.5cm}
\usepackage[textwidth=2.5cm, colorinlistoftodos]{todonotes}
\newif\ifdraft
\drafttrue
\excludecomment{reviewernote} % \includecomment{reviewernote} for the referee copy
\newcommand{\unit}[1]{%
\,\mathrm{#1}%
}
\begin{document}
\ifdraft\listoftodos\fi
\section{Method}
We sampled at $44.1\unit{kHz}$ and discarded 3\% of the frames.\todo{Cite the
sampling paper} The remaining frames were aligned by hand.
\begin{reviewernote}
The alignment step was added after the first round of reviews.
\end{reviewernote}
\begin{comment}
Old method, kept for reference:
We sampled at 22 kHz and used automatic alignment { which never
\end{comment}
\begin{tabular}{lr}
Frames & 1200 \\ % before filtering
Kept & 1164 \\ % after filtering
\end{tabular}
\ifdraft
\todo[inline]{Add the comparison with the 2025 baseline.}
\fi
\end{document}Quick Reference
| Task | How |
|---|---|
| Comment to end of line | % text |
| Literal percent sign | \% |
| Suppress the space at a line break | % at the end of the line |
| Block comment (package) | \usepackage{comment}, then \begin{comment} ... \end{comment} on their own lines |
| Block comment (no package) | \iffalse ... \fi (no unmatched \fi inside) |
| Switchable blocks | \includecomment{name} / \excludecomment{name} |
| Margin note | \todo{text} (todonotes) |
| Inline note | \todo[inline]{text} |
| Figure placeholder | \missingfigure{text} |
| List of all notes | \listoftodos |
| Hide all notes | \usepackage[disable]{todonotes} |
| Fix squeezed margin notes | \setlength{\marginparwidth}{2.5cm} before loading todonotes |
| Message on the console | \typeout{text} |
| Draft/final switch | \newif\ifdraft + \drafttrue, or etoolbox \newtoggle{draft} |
Next Steps
With the draft under control, keep building the document:
- LaTeX Hyperlinks – hyperref, \url, \href and clickable cross-references
- LaTeX Footnotes – Footnotes in tables, headings and captions
- LaTeX Tables – booktabs, multicolumn and longtable
- Using Colors in LaTeX – The xcolor names used by todonotes and changes
- Learn LaTeX – Complete beginner tutorial covering all the basics
Free LaTeX Tools
- LaTeX Preview – Check what a commented-out block does with a live PDF preview
- AI LaTeX Generator – Generate LaTeX from a text description
- LaTeX Symbols Reference – Browse math operators and special characters
Related Articles
Degree Symbol: How to Type ° on Keyboard, Mac, iPhone, Word, LaTeX
Best LaTeX Editors for Mac: Complete Comparison Guide 2026
From Crixet to Prism: The Evolution of AI-Powered LaTeX Editing
How to Create a Professional Resume in LaTeX (Complete Guide 2026)
Clean up drafts faster with AI
Octree's AI editor comments out blocks, finds the unescaped percent sign that ate half a paragraph, and compiles your document in seconds.
Try Octree Free →