To "comment out" a line, I need to insert a % at the beginning of the line (so that the line will not be compiled).

Is there a way to comment out a large section without having to manually place % in front of each line?

  • Can you explain the sort of situations you want to do this in? If, for example, it's to reduce compile times when writing a long document then you may find it best to use something like the subfiles package. This would mean you don't have to remove the commented out sections before compiling the whole document, but you could still compile individual parts of the document with the proper preamble, and without changing anything except for which file you pass to LaTeX.
    – Edd
    Commented May 10, 2011 at 11:03
  • Related: Multi-line (block) comments
    – Scott H.
    Commented Jan 12, 2013 at 17:41
  • 5
    I think the most comprehensive answer is still this one from the UK TeX FAQ: tex.ac.uk/cgi-bin/texfaq2html?label=conditional Commented Nov 1, 2013 at 14:26
  • I don't use Context, but I think scontents support Context. (however that's a relatively heavy comment, but at least it's built-in) Also iffalse should still work (because it's plain TeX?) ____________________________________________________________________________________________ See also: Multi-line (block) comments in LaTeX - TeX - LaTeX Stack Exchange and the linked answer in its comment.
    – user202729
    Commented Oct 16, 2021 at 10:26

12 Answers 12


You can use \iffalse ... \fi to make (La)TeX not compile everything between it. However, this might not work properly if you have unmatched \ifxxx ... \fi pairs inside them or do something else special with if-switches. It should be fine for normal user text.

There is also the comment package which gives you the comment environment which ignores everything in it verbatim. It allows you to define own environments and to switch them on and off. You would use it by placing \usepackage{comment} in the preamble. Below is an example:

\section{Multi-line comments}}
This is a comment,
a multi-line comment,

  • 28
    make sure to \usepackage{comment} in the preamble Commented Jun 7, 2018 at 2:16
  • 5
    an example of the use of each of these would be good for this answer as it comes up in google
    – baxx
    Commented Apr 29, 2019 at 14:18
  • 1
    is there a way to fix \begin{comment} inside \begin \comment errors?
    – Hao S
    Commented Sep 2, 2019 at 18:27
  • Here is a link to an example usage of the comment package: Just use \begin{comment} and \end{comment} to wrap the commented-out lines. (You must also include the package, of course - \usepackage{comment}.) Commented Dec 31, 2019 at 16:16
  • 2
    Can we see an example of, "[i]t allows you to define own environments and to switch them on and off?"
    – khatchad
    Commented Sep 13, 2021 at 21:45

You can use \iffalse:

One morning, as Gregor Samsa was waking up from anxious dreams, he discovered
that in his bed he had been changed into a monstrous verminous bug. He lay on
his armour-hard back and saw, as he lifted his head up a little, his brown,
arched abdomen divided up into rigid bow-like sections.

Of course, this has to align with other syntactical TeX structures in you document whereas you can use % much more freely. The good news is that you can introduce your own switch to make this optional:

\drafttrue % or \draftfalse

<only shown in draft mode>
<only shown in non-draft mode>

The \else part is optional and you could use \ifdraft ... \fi if you don't need it.

  • 44
    I'm sorry to say, but I saw a bug in your code! :)
    – McGafter
    Commented Mar 31, 2014 at 13:26

The verbatim package provides a comment environment:

    This text will be displayed
    This text will not be displayed.

The Not So Short Introduction to LaTeX2e mentions this option on page 6 and remarks: "Note that this won’t work inside complex environments, like math for example."


Another option is the comment package, which, like verbatim provides a comment environment, but offers the option to define arbitrary "throw away" environments that can selectively be enabled or disabled:


% uncomment to include stuff in standard comment-environment

% define a mysection env which content is excluded

    This text will be displayed
    This text will only be displayed, if \includecomment{comment} was given
    This text will only be displayed, if \includecomment{mysection} was given

Additionally, the package provides some simple hooks into the defined environments. Instead of \includecomment{mysection} one could also use \specialcomment{mysection}{<before code>}{<after code>} to enable a comment section:

% typeset stuff in mycomment with gray text

There are other ways to solve this problem than via (La)TeX.

Something that is good about either of these solutions is that they are independent of particular LaTeX packages and code.

  • 3
    I'd second this; Most text editors have this, and emacs is great for when I'm writing lots of math. For more text-based things, TeXStudio is also good, and has a similar feature.
    – Canageek
    Commented Jul 3, 2012 at 23:03
  • 3
    Actually, <kbd>M-;</kbd> is even better (less keystrokes), and more intelligent (it does different things depending on e.g. whether region is active or not).
    – mbork
    Commented Mar 23, 2013 at 22:05
  • 1
    Worth noting, though, that my editor (Vim with LaTeX-Suite) is the reason I looked for this question despite already knowing how to line-comment en masse, because it screws up the folding. When I use the comment environment, I can fold up the whole commented-out part and only that part. Commented Jun 30, 2013 at 14:25
  • 1
    Also, uncommenting lines commented this way doesn't always work even in editors which support it in theory. It is however pretty useful.
    – Chris H
    Commented Mar 31, 2014 at 13:29
  • 1
    To supplement this answer: In TeXstudio it's Ctrl-T / Ctrl-U to comment and uncomment respectively. Commented Sep 29, 2016 at 12:11

I often paste in plain text such as writing guidelines and comment them out. For me the simplest way is to define a command with an argument which produces no output.


And to comment out text:

Text text text
  • 2
    I'm using same method. Just a detail, in the first code there's a missing backslash. It should be \newcommand{\comm}[1]{} (can't edit it, b/c of silly 6 characters rule).
    – monnef
    Commented Jan 14, 2015 at 9:02
  • 3
    There is a default function for this, which is \typeout{}.
    – Bilbottom
    Commented Jun 9, 2019 at 16:07

In vi or vim you could comment a section (say line 102-345) like this:

[esc] [:]

you are in command mode now! And type:


it means substitute the beginning of the line (^) with a % sign.

  • 8
    I find much more easy to use block-visual-mode insert: notfaq.wordpress.com/2006/07/28/vim-comment-blocks-of-code
    – Rmano
    Commented Apr 10, 2014 at 20:57
  • The second command inserts % in front of the lines 102 till 345... Note that this does not check if the line is already a comment.
    – m13r
    Commented Dec 6, 2016 at 10:44
  • 1
    Usually you DO NOT want to check if the line is already commented, because you can later do 102,345s/^%// and leave any previously commented line commented.
    – gboffi
    Commented Mar 13, 2018 at 9:13
  • vim-commentary package speeds it up and generalizes among different syntax Commented Apr 28 at 7:02

This answer obviously depents of the frontend one is using, but in the case of using Kile:

Commenting multiple lines with Kile

1) Select the lines you wish to comment out

2) Ctrl - D

Uncommenting multiple lines with Kile

1) Select the lines you wish uncomment

2) Ctrl - Shift - D

These shortcuts can be changed from Settings -> Configure Shortcuts...

  • cntrl+/ also works Commented Mar 12, 2023 at 17:13

Anything written after \end{document} will be ignored by the compiler. I often use this to cut a document short when troubleshooting, by inserting a second \end{document} at the appropriate place.

  • 1
    +1: Of course this only works if the section to comment out is from some line x to the end of the document. But if that is the case, this is easy and effective! I frequently use the area after \end{document} to keep larger chunks of "material I do not want to delete, yet"
    – Daniel
    Commented Jun 8, 2017 at 20:27

For the sake of completeness, I will include the shortcuts for the few editors that I know (with the links to their corresponding original answers).

Ctrl + T to comment

Ctrl + U to uncomment

You can customize them

Ctrl + Shift + Alt + to comment

Ctrl + Shift + Alt + to uncomment

Yes, that's four keys (!). However, you can install ToggleComment (a macro) to use Ctrl + Q to toggle between comment and uncomment a block of text.


If using TeXNicCenter, you can highlight the text and

Ctrl + Q to comment the text out

Ctrl + W to uncomment it


Is there way to comment out a large section without having to manually putting a % in front of each line?

Not probably what you are looking for, but another approach to easily hide sections is work with child files. It is a rather simple process. Assuming commons usual shortcuts of commons editors (i.e, excluding vim/emacs) will be some like that:

  1. Select the section to comment
  2. Crtl-X (cut)
  3. Write %\input{filename}
  4. Crtl-N (new document)
  5. Crtl-V (paste)
  6. Crtl-S filename.tex (save the new document).

    Et voilá. Hereafter restore or comment the section again is as simple as remove/add one % before \input. Bonus points: (a)The commented part barely can bothers you while working in the main document and (b) prevent you of accidental deletions on the commented parts (that obviously cannot be detected in the pdf).

You must log in to answer this question.

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