TransWikia.com

Commenting out large sections

TeX - LaTeX Asked by user5419 on February 20, 2021

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?

12 Answers

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:

usepackage{comment}
begin{document}
section{Multi-line comments}}
begin{comment}
This is a comment,
a multi-line comment,
indeed.
end{comment}
end{document}

Correct answer by Martin Scharrer on February 20, 2021

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:

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

Answered by Christian Lindig on February 20, 2021

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

Answered by doncherry on February 20, 2021

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 includecomment{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 a comment section:

% typeset stuff in mycomment with gray text
specialcomment{mysection}{begingroupcolor{gray}}{endgroup}

Answered by Daniel on February 20, 2021

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.

Answered by N.N. on February 20, 2021

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

Answered by np8 on February 20, 2021

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.

newcommand{comm}[1]{}

And to comment out text:

comm{
Text text text
}

Answered by kstoc on February 20, 2021

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

[esc] [:]

you are in command mode now! And type:

102,345s/^/%/

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

Answered by Robert Nagtegaal on February 20, 2021

If using TeXNicCenter, you can highlight the text and

Ctrl + Q to comment the text out

Ctrl + W to uncomment it

Answered by Justin on February 20, 2021

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.

Answered by Arturo Moncada-Torres on February 20, 2021

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.

Answered by lblb on February 20, 2021

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

Answered by Fran on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP