[comp.text.tex] How do I comment-out a block of TeX

siemsen@sol.usc.edu (Pete Siemsen) (01/24/91)

TeX or LaTeX can generate errors that are hard to track down, either
because they occur deep in several layers of macros or because the
cause of the error is far from the site of the message, or both.  I
sometimes resort to the brute-force method: chopping big chunks of the
text out and seeing if the error disappears, then doing it again,
until I zero in on the erroneous source.  Knuth even suggests
something like this in Chapter 27 of the TeXbook.  The editing is a
pain, however.

Is there some way to comment out a block of TeX code?  Yes, I can
insert a percent sign at the front of every line, but I want something
simpler, like C's "#if 0" and "#endif" preprocessor directives.  I
tried \ifnum0>1 ... \fi, but TeX barfed with "missing \fi" after 2000
or so lines.
-- 
Pete Siemsen                         Pete Siemsen            siemsen@usc.edu
University of Southern California    645 Ohio Ave. #302      (213) 740-7391 (w)
1020 West Jefferson Blvd.            Long Beach, CA 90814    (213) 433-3059 (h)
Los Angeles, CA 90089-0251

ken-w@is.s.u-tokyo.ac.jp (WAKITA Ken) (01/24/91)

In article <29555@usc> siemsen@sol.usc.edu (Pete Siemsen) writes:

 > Is there some way to comment out a block of TeX code?  Yes, I can
 > insert a percent sign at the front of every line, but I want something
 > simpler, like C's "#if 0" and "#endif" preprocessor directives.  I
 > tried \ifnum0>1 ... \fi, but TeX barfed with "missing \fi" after 2000
 > or so lines.

Yes.  Try the following macro.

\newcommand\comment[1]{}

\comment{
The text you want to comment out.
}

--
WAKITA Ken (ken-w@is.s.u-tokyo.ac.jp)		      Masuda Group.,
						Dept. of Info. Sci.,
						     Univ. of Tokyo.

rouben@math13.math.umbc.edu (Rouben Rostamian) (01/24/91)

In article <29555@usc> siemsen@sol.usc.edu (Pete Siemsen) writes:
>Is there some way to comment out a block of TeX code?  Yes, I can
>insert a percent sign at the front of every line, but I want something
>simpler, like C's "#if 0" and "#endif" preprocessor directives.  I
>tried \ifnum0>1 ... \fi, but TeX barfed with "missing \fi" after 2000
>or so lines.

I do not know off hand how to define an \comment macro in TeX, but
I recall a discussion of this issue in TeXHaX or someplace like that,
and that there were some people who offered solutions which I did not
save.

I cannot imagine why you should want to comment-out 2000 lines of 
text out of a file.  Two thousand lines of text should never be
placed in one file to begin with.

I have two suggestion regarding your query:

    1. The longer a TeX file, the more unwieldy it gets, not
only for debugging, but for editing, TeXing, updating, revising,
spell-checking, and so on.  I always advise beginners to break up
their TeX files into as many logically separate components as
possible.  For instance, when writing a journal article, it is a good
idea to place each section, not matter how short, into a separate
file.  Then have a master file \input the individual sections for
processing.  The same method applies to chapters and subsections of
books and monographs.  After all, "modular programming" is the
standard practice in writing programs, and a TeX documents is not
much different from a program.

Once you have modularized your text, you can comment and
uncomment selected \input commands in the master file to process
individual sections.

    2.  If you are on a machine with a C compiler, you may use C
preprocessor's #ifdef construct to comment out sections of a TeX
document.  Just enclose the selected text within an "#ifdef DEBUG"
and "#endif", and run your TeX document through the C preprocessor
before feeding it to TeX.  The C preprocesor is accessed differently
depending on the particular machine you are on.  Consult the manual.
In ultrix, you may do:

/lib/ccp -P -DDEBUG file.tex >file_new.tex

End of suggestions.
-----------------------------------------------------------------------
\begin{wishlist}
I wish that the unix implementation of TeX would accepts files form
standard in, as most other unix utilities do.  In that case I could
by-pass the creation of the intermediate file_new.tex in the example
above and do:

/lib/ccp -P -DDEBUG file.tex |latex

Alas, ...
\end{wishlist}

--
Rouben Rostamian                          Telephone: (301) 455-2458
Department of Mathematics and Statistics  e-mail:
University of Maryland Baltimore County   bitnet: rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.              internet: rouben@math9.math.umbc.edu

kgk@cs.brown.edu (Keiji Kanazawa) (01/24/91)

In article <4881@umbc3.UMBC.EDU> rouben@math13.math.umbc.edu (Rouben Rostamian) writes:

   I cannot imagine why you should want to comment-out 2000 lines of 
   text out of a file.  Two thousand lines of text should never be
   placed in one file to begin with.

Just for the record, tex.web is 24858 lines.  Does DEK split it?

:-)

--

Keiji Kanazawa
kgk@cs.brown.edu

kaja@daimi.aau.dk (Kaja P. Christiansen) (01/24/91)

siemsen@sol.usc.edu (Pete Siemsen) writes:

>Is there some way to comment out a block of TeX code?

We have an option style called "comment". It's not very
sophisticated (works on a paragraph, but not in the middle
of it), but you may find it usefull. Entering "yes" after
prompt will comment-out; entering anything else will not.

% Document style option "comment"
%	usage:	\documentstyle[...,comment,...]{...}
%		\begin{document}
%		 ...
%		 \BeginComment
%		 text
%		 \EndComment
%		 ...
% Preserves or discards text between \BeginComment...\EndComment
% according to user's wishes.
%
% Kaja Christiansen / 23 Nov 1988

\typein[\comment]{Enter yes to comment out}
\def\@comment{yes}
\gdef\BeginComment#1\EndComment{\ifx\comment\@comment\else#1\fi}

--
Kaja P. Christiansen                     Computer Science Department
(kpchristiansen@daimi.aau.dk)            Aarhus University 
Phone: +45 86 12 71 88                   Ny Munkegade 116,
Fax:   +45 86 13 57 25                   DK-8000 Aarhus C., Denmark 

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (01/25/91)

siemsen@sol.usc.edu (Pete Siemsen) writes:

>Is there some way to comment out a block of TeX code?  Yes, I can

Here is something I wrote, and posted on both usenet and TeXhax
a while ago. It works with plain TeX and LaTeX, and does not
suffer from all of the drawbacks of simpler approaches.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Comment.sty   version 1.0 2 November 1990
% to be used with plain TeX or LaTeX
%
% Author
%    Victor Eijkhout
%    Center for Supercomputing Research and Development
%    University of Illinois at Urbana-Champaign
%    305 Talbot Lab
%    104 South Wright Street
%    Urbana, Illinois 61801, USA
%
%    eijkhout@csrd.uiuc.edu
%
% Usage: all text included in between 
%    \comment ... \endcomment
% or \begin{comment} ... \end{comment}
% is discarded. The closing command should appear on a line
% of its own. No starting spaces, nothing after it.
% This environment should work with arbitrary amounts
% of comment. 
%
% Basic approach: take every line in verbatim mode as macro
% argument, then don't do nothing with.

\def\makeinnocent#1{\catcode`#1=12 }

\def\comment{\begingroup
    \let\do\makeinnocent \dospecials 
    \makeinnocent\^^L % and whatever other special cases
    \endlinechar`\^^M \catcode`\^^M=12 \xcomment}
{\catcode`\^^M=12 \endlinechar=-1 %
 \gdef\xcomment#1^^M{\def\test{#1}
      \ifx\test\plainendcommenttest
          \let\next\endgroup
      \else\ifx\test\lalaendcommenttest
          \def\next{\endgroup\end{comment}}
      \else
          \let\next\xcomment
      \fi \fi \next}
}

{\escapechar=-1
 \xdef\plainendcommenttest{\string\\endcomment}
 \xdef\lalaendcommenttest{\string\\end\string\{comment\string\}}
}

\endinput

Share and enjoy!

Victor.

krab@iesd.auc.dk (Kresten Krab Thorup) (01/25/91)

Here's the comment environment from TeXhax-72,1990

/Kresten

-------forwarded message follows this line---------

Date: Fri, 02 Nov 90 21:25:51 -0600
From: eijkhout@csrd.uiuc.edu
Subject: comment environment
Keywords: comment environment

Over the last few weeks I have seen two of three requests
for a comment environment on the net. Time for action.

Probably everyone knows why \def\comment#1\endcomment{}
does not work: unbalanced braces, buffer overflow, outer macros, ...

I saw one quite ingenious solution of someone who make almost every character
into comment. Cute. It will take quite a while before you run
into buffer overflow then.

Here is another solution: every line is read as verbatim text, and
just thrown away. No buffer problems. Never.
These macros can be used with plain TeX and LateX, and probably any
other macro package. For plain TeX write
\comment 
 ...
\endcomment
For LaTeX write
\begin{comment}
 ...
\end{comment}
In both cases put the closing command on a line of its own, and nothing
before or after it.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Comment.sty   version 1.0 2 November 1990
% to be used with plain TeX or LaTeX
%
% Author
%    Victor Eijkhout
%    Center for Supercomputing Research and Development
%    University of Illinois at Urbana-Champaign
%    305 Talbot Lab
%    104 South Wright Street
%    Urbana, Illinois 61801, USA
%
%    eijkhout@csrd.uiuc.edu
%
% Usage: all text included in between
%    \comment ... \endcomment
% or \begin{comment} ... \end{comment}
% is discarded. The closing command should appear on a line
% of its own. No starting spaces, nothing after it.
% This environment should work with arbitrary amounts
% of comment.
%
% Basic approach: take every line in verbatim mode as macro
% argument, then don't do nothing with.

\def\makeinnocent#1{\catcode`#1=12 }

\def\comment{\begingroup
    \let\do\makeinnocent \dospecials
    \makeinnocent\^^L % and whatever other special cases
    \endlinechar`\^^M \catcode`\^^M=12 \xcomment}
{\catcode`\^^M=12 \endlinechar=-1 %
 \gdef\xcomment#1^^M{\def\test{#1}
      \ifx\test\plainendcommenttest
          \let\next\endgroup
      \else\ifx\test\lalaendcommenttest
          \def\next{\endgroup\end{comment}}
      \else
          \let\next\xcomment
      \fi \fi \next}
}

{\escapechar=-1
 \xdef\plainendcommenttest{\string\\endcomment}
 \xdef\lalaendcommenttest{\string\\end\string\{comment\string\}}
}

\endinput

Victor Eijkhout                     phone: +1 217 244-0047
 Center for Supercomputing Research and Development
 University of Illinois at Urbana-Champaign
 305 Talbot laboratory
 104 South Wright street
 Urbana, Illinois 61801-2932, USA

home: 2503 W. Springfield Av, Apt. K-4, Champaign 61821, USA

--

        (  .                     
      (. )  )                    
       ) ( (                     
    .________.                   <krab@iesd.auc.dk>
    | |    __|                   
    |       ))                   Kresten Krab Thorup
    | |    //|                   Institute for electronic systems
`---`-`------'---'               Dept. of mathemathics and computer science
Coffee coffee ... (C) 1991       Aalborg University - Denmark, EC

hanche@imf.unit.no (Harald Hanche-Olsen) (01/25/91)

In article <29555@usc> siemsen@sol.usc.edu (Pete Siemsen) writes:

   Is there some way to comment out a block of TeX code?  Yes, I can
   insert a percent sign at the front of every line, but I want something
   simpler, like C's "#if 0" and "#endif" preprocessor directives.  I
   tried \ifnum0>1 ... \fi, but TeX barfed with "missing \fi" after 2000
   or so lines.

Many solutions have already been suggested, but let me just point out
that the verbatim.sty file by Rainer Sch\"opf also contains a comment
environment.  This file is distributed with the Mittelbach-Sch\"opf
font selection scheme and amslatex, I believe.

- Harald Hanche-Olsen <hanche@imf.unit.no>
  Division of Mathematical Sciences
  The Norwegian Institute of Technology
  N-7034 Trondheim, NORWAY

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (01/25/91)

As quoted from <KEN-W.91Jan24110833@silver.is.s.u-tokyo.ac.jp> by ken-w@is.s.u-tokyo.ac.jp (WAKITA Ken):
+---------------
| In article <29555@usc> siemsen@sol.usc.edu (Pete Siemsen) writes:
|  > Is there some way to comment out a block of TeX code?  Yes, I can
|  > insert a percent sign at the front of every line, but I want something
|  > simpler, like C's "#if 0" and "#endif" preprocessor directives.  I
| 
| Yes.  Try the following macro.
| \newcommand\comment[1]{}
+---------------

This will blow up after far fewer than 2000 lines... macro arguments are
not expected to be large, so there isn't much memory set aside for them.

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

walvdrk@pttrnl.nl (01/25/91)

 > Is there some way to comment out a block of TeX code?  Yes, I can
> insert a percent sign at the front of every line, but I want something
> simpler, like C's "#if 0" and "#endif" preprocessor directives.  I
> tried \ifnum0>1 ... \fi, but TeX barfed with "missing \fi" after 2000
> or so lines.

I use:
\iffalse
Piece of text to be made invisible ...
\fi

Kees  van der Wal				  e-mail: JC_vdWal@pttrnl.nl
----------------------------------------------------------------------------
PTT Research Neher Laboratories
P.O. Box 421
2260 AK  Leidschendam  The Netherlands		       Phone: +31 70 3326295
============================================================================
Alt. addressing modes:  WALVDRK@HLSDNL5  or  WALVDRK%HLSDNL5@CUNYVM.cuny.edu

xiaofei@acsu.buffalo.edu (Xiaofei Wang) (02/08/91)

How do I comment-out a block of TeX?

I know many solutions have been posted. But I would like to push the 
question to another direction: eplain. I think one of the eplain macros
will solve the problem. Something like \list which I used successfully
to print a FORTRAN program.

-- 
xiaofei@acsu.buffalo.edu / rutgers!ub!xiaofei / v118raqa@ubvms.bitnet