TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question
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 May 10 '11 at 11:03
Related: Multi-line (block) comments – Scott H. Jan 12 '13 at 17:41
I think the most comprehensive answer is still this one from the UK TeX FAQ: tex.ac.uk/cgi-bin/texfaq2html?label=conditional – Blaisorblade Nov 1 '13 at 14:26

10 Answers 10

up vote 145 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
+1 for the example. – Skarab Jan 22 '12 at 11:35
I'm sorry to say, but I saw a bug in your code! :) – McGafter Mar 31 '14 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."

share|improve this answer

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
share|improve this answer

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.

share|improve this answer
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 Jul 3 '12 at 23:03
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 Mar 23 '13 at 22:05
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. – Luke Maurer Jun 30 '13 at 14:25
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 Mar 31 '14 at 13:29

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...

share|improve this answer

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.

share|improve this answer
I find much more easy to use block-visual-mode insert: notfaq.wordpress.com/2006/07/28/vim-comment-blocks-of-code – Rmano Apr 10 '14 at 20:57

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
share|improve this answer
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 Jan 14 '15 at 9:02

If using TeXNiC Center: highlight the text. press ctrl + Q to comment the text out. ctrl + W will uncomment it.

share|improve this answer

For the sake of completeness, I will include the shortcuts for comments on Texmaker (as previously answered in this thread). In short, Ctrl + T to comment and Ctrl + U to uncomment, but of course you can customize them.

share|improve this answer

protected by Stefan Kottwitz Nov 1 '15 at 22:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.