[comp.text] Command environment for TeX?

kelly@turing.arc.nasa.gov (Jim Kelly) (07/29/88)

Any typesetting program must accept two kinds of input: text to be
output literally, and directives to be interpreted by the program.
One of TeX's major flaws is how the two are distinguished.  TeX takes
the whole document verbatim, except for a few special characters and
words beginning with backslashes.  For most English text, this works
fine, and nobody minds a few backslashes mixed in with the text, but
for defining macros it's a disaster--spaces put in the macro for
legibility appear in the output, and your file is 20% backslashes.

I'd like an environment where the default is reversed--one in which
everything is assumed to be a control sequence unless escaped (maybe
put in an hbox).  Macros could be defined from within the environment
and could contain all the white space legibility demands, without a
single backslash.

Has anyone written or contemplated such an environment?

Jim Kelly (kelly@pluto.arc.nasa.gov)
Jim Kelly
kelly@pluto.arc.nasa.gov
The Government  "We understand you tore that little tag off your mattress..."

mcglk@scott.stat.washington.edu (Ken McGlothlen) (07/29/88)

In the referenced article, kelly@turing.arc.nasa.gov.UUCP (Jim Kelly) writes:
+----------
|  [...]
| One of TeX's major flaws is how the two [commands and text to be typeset]
| are distinguished.  TeX takes the whole document verbatim, except for a
| few special characters and words beginning with backslashes.  For most
| English text, this works fine, and nobody minds a few backslashes mixed
| in with the text, but for defining macros it's a disaster--spaces put in
| the macro for legibility appear in the output, and your file is 20% 
| backslashes.
|
| I'd like an environment where the default is reversed--one in which
| everything is assumed to be a control sequence unless escaped (maybe
| put in an hbox).  Macros could be defined from within the environment
| and could contain all the white space legibility demands, without a
| single backslash.
| [...]
|
| Jim Kelly (kelly@pluto.arc.nasa.gov)
+----------

Whee.  Quite a different approach to an admitted weakness of TeX--it wasn't
really meant for programming.

The way I see it, you're missing several approaches.

For one thing, any escaped symbol (say, \def) uses a non-letter as its
delimiter.  This includes another backslash (\def\bs{...}), or a space,
which, after an escaped token, is ignored.  You might not be able to
write

     \def\ooga{
          \vskip 6pt plus 2pt minus 1pt
          {\bf Ooga \biggle.\giggle:}
          \hfil \break
          \vskip 4pt plus 1pt minus 1pt
     }

because it would have spaces at the following asterisks:

     \def\ooga{*
          \vskip 6pt plus 2pt minus 1pt
          {\bf Ooga \biggle.\giggle:}*
          \hfil \break
          \vskip 4pt plus 1pt minus 1pt
     }

HOWEVER, because whitespace is ignored after a control sequence, you
could use

     \def\ooga{\vskip 6pt plus 2pt minus 1pt
          {\bf Ooga \biggle.\giggle:}\hfil \break
          \vskip 4pt plus 1pt minus 1pt
     }

Admittedly, this is much less readable, but macro readability is not
quite TeX's forte.  Another possibility is defining a null macro for
the end of a line where you don't want a space:

     \def\z{}

     \def\ooga{\z
          \vskip 6pt plus 2pt minus 1pt
          {\bf Ooga \biggle.\giggle:}\z
          \hfil \break
          \vskip 4pt plus 1 pt minus 1pt
     }

If you're really determined to get TeX to ignore those spaces, you can
try changing the \catcode of a space to 9 (null character), and use
something else for a deliberate space (\catcode 10).  That could get
*QUITE* messy, though, and I don't recommend it.

				--Ken McGlothlen
				  mcglk@scott.biostat.washington.edu
				  mcglk@max.acs.washington.edu
				  mcglk@max.bitnet

bts@sas.UUCP (Brian T. Schellenberger) (08/03/88)

An environment convenient for writing code in (this is in LaTeX):

    \global\newif\ifatletter
    \ifcat @a \atletterture \else \atletterfalse \fi
    \makeatletter
    
    \newenvironment {definitions}
       {
       \endgroup    % doesn't really nest
       \ifcat @a \atlettertrue \else \atletterfalse \fi
       \catcode`\@=11 \catcode`\ =9 \catcode`\^^M=9
       }%
       {
       \catcode`\@=12 \catcode`\ =10 \catcode`\^^M=5
       \ifatletter \catcode`\@=\letterC \fi
       \def \@currenvir {definitions}
       \begingroup % doesn't really nest
       }
    
    \ifatletter\else \makeatother \fi

To translate into TeX, ignore the \endgroup, \begingroup, and 
\def\@currenvir; define two seperate macros for the two halves
of the environment; and replace \makeatother and \makeatletter with
the obvious \catcode magic.

Due to the difficulty of getting my computers to easily send small things,
I've typed this in by hand from a copy in front of me, so there may be some
very silly typo in this.

Once you've got such an environment, you can write nicely formatted stuff.
The only problem is that *all* spaces are now ignored; you must remember
to use "\ " or \space for spaces that you really want.  For this reason,
I've defined the little environment for doing things like constructing
error messages:

   \newenvironment {userfeedback}
       {\catcode`\ =10}
       {}

(Actually, I define all my things like this with lines like
"\catcode`\@=\otherC \catcode`\ =\spaceC \catcode`\^^M=\nlC," having
defined the necessary \chardefs for all the catagory codes.  I recommend
this heartily; it makes TeX code *much* easier to read.)
-- 
--Brian,                     __________________________________________________
  the man from              |Brian T. Schellenberger   ...!mcnc!rti!sas!bts
  Babble-On                 |104 Willoughby Lane     work: (919) 467-8000 x7783
____________________________|Cary, NC   27513        home: (919) 469-9389