Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 at 17:41

5 Answers

up vote 33 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

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
1  
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 at 22:05

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:

\documentclass{article}
\usepackage{comment}

% uncomment to include stuff in standard comment-environment
%\includecomment{comment}

% define a mysection env which content is excluded
\excludecomment{mysection}

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

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 some comment section:

% typeset stuff in mycomment with gray text
\specialcomment{mysection}{\begingroup\color{gray}}{\endgroup}
share|improve this answer

The verbatim package provides a comment environment:

\documentclass{article}
\usepackage{verbatim}
\begin{document}
    This text will be displayed
\begin{comment}
    This text will not be displayed.
\end{comment}
\end{document}

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

You can use \iffalse:

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

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:

\newif\ifdraft
\drafttrue % or \draftfalse

\ifdraft
<only shown in draft mode>
\else 
<only shown in non-draft mode>
\fi

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

share|improve this answer
3  
+1 for the example. – Skarab Jan 22 '12 at 11:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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