Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

plainTeX newb: troubles with \catcode ... why doesn't this work?

76 views
Skip to first unread message

eds

unread,
Aug 7, 2012, 7:45:33 PM8/7/12
to
I've been trying to write a macro but alas I find that my macro isn't
working. After removing the working TeX, I find that my macro doesn't
work because of the way I'm restoring the catcode of the carriage
return. Below is sample code I put together to illustrate what I'm
doing attempting. Note the code below also changes the catcode of @.
I figured that whatever I needed to do to change and restore the
catcode of @ would be essentially what was needed to change and
restore the end-of-line. That too is incorrect because the code
works for @ but doesn't work for the CR. I can only assume that there
is something about the end-of-line that I don't understand. Can
someone please explain why the sample TeX below doesn't work?

In attempting this macro, I found that I'm also not clear on the
differences between `\^^M and ^^M. If possible, can someone educate
me on the difference?

Thank you in advance
eds.


% set to true to select restoring via the macro
\newif\ifcs
\cstrue
% restores(?) the catcode for the end of line
\edef\restoreCRCC{\catcode`\^^M=\the\catcode`\^^M\relax }
% restores the catcodef for the @ character
\edef\restoreACC{\catcode`@=\the\catcode`@}
% show the definitions
{\tt\string\restoreCRCC\ \meaning\restoreCRCC}\par
{\tt\string\restoreACC \ \meaning\restoreACC }\par

\medskip
CR catcode = \the\catcode`\^^M\par
@ catcode = \the\catcode`@\par
\endlinechar=-1
\catcode`\^^M=12
\catcode`\@=11

\medskip
CR catcode = \the\catcode`\^^M \par
@ catcode = \the\catcode`@\par
\ifcs
\restoreACC
\restoreCRCC
\else
\catcode`\^^M=5
\catcode`\@=12
\fi

\medskip
CR catcode = \the\catcode`\^^M \par
@ catcode = \the\catcode`@\par
\endlinechar=13
\medskip
some text
\end

zappathustra

unread,
Aug 8, 2012, 2:46:09 AM8/8/12
to
eds <edstu...@gmail.com> a écrit:
>
> I've been trying to write a macro but alas I find that my macro isn't
> working. After removing the working TeX, I find that my macro doesn't
> work because of the way I'm restoring the catcode of the carriage
> return. Below is sample code I put together to illustrate what I'm
> doing attempting. Note the code below also changes the catcode of @.
> I figured that whatever I needed to do to change and restore the
> catcode of @ would be essentially what was needed to change and
> restore the end-of-line. That too is incorrect because the code
> works for @ but doesn't work for the CR. I can only assume that there
> is something about the end-of-line that I don't understand. Can
> someone please explain why the sample TeX below doesn't work?

The problem comes from \edef, actually. The line:

\edef\restoreCRCC{\catcode`\^^M=\the\catcode`\^^M\relax }

should be

\edef\restoreCRCC{\catcode`\noexpand\^^M=\the\catcode`\^^M\relax }

The reason is that "`\<char>" denotes a character only in the context of
\catcode; but in an \edef, \catcode being unexpandable, the following
construction isn't interpreted as expected; instead, TeX sees a control
sequence \^^M, which plain TeX defines as:

\def\^^M{\ }

so that a lonely backslash at the end of the line looks and feels like a
control space.

With your code, then, \^^M becomes the control space and when
\restoreCRCC is called it executes:

\catcode`\ =5\relax

thus transforming space into an EOL character. And anything after an EOL
character is discarded by TeX, so that

CR catcode = \the\catcode`\^^M \par

is read as

CR<EOL>catcode<EOL>=<EOL>\the\catcode`\^^M<EOL>\par

and ultimately as a line containing only "CR".

> In attempting this macro, I found that I'm also not clear on the
> differences between `\^^M and ^^M. If possible, can someone educate
> me on the difference?

^^M is a character like any other; `\^^M is an example of the `\<char>
construction used to denote a character code.

Best,
Oaul

Ulrich D i e z

unread,
Aug 8, 2012, 3:33:58 AM8/8/12
to
zappathustra wrote:

> eds <edstu...@gmail.com> a écrit:

>> In attempting this macro, I found that I'm also not clear on the
>> differences between `\^^M and ^^M. If possible, can someone educate
>> me on the difference?

>^^M is a character like any other; `\^^M is an example of the `\<char>
>construction used to denote a character code.

^^M is a character-token.
\^^M is a control-sequence-token whose name consists of only
one character, the ^^M-character (ASCII13=Return).

[ Whether that one-letter-control-sequence-token is to be
considered a control-word or a control-symbol depends on
the current catcode of the ^^M-character. If it is 11, then
\^^M is a control-word, otherwise it is a control-symbol.

When reading/tokenizing, then usually spaces behind
control-words are ignored, spaces behind control-symbols
are not ignored.
When unexpanded-writing to console or external file, then
usually a trailing space is attached to a control-word but
is not attached to a control-symbol.]

You can use both single character-tokens and
one-letter-control-sequence-tokens for denoting the
alphabetic-constant of a number while with numbers
the alphabetic-constant-notation is always introduced
by a leading ` .
E.g.,
\number65 (decimal digit notation)
\number`A (alphabetic constant by means of character)
\number`\A (alphabetic constant by means of one letter control-sequence)
\the\catcode65
\the\catcode`A
\the\catcode`\A

In expansion-contexts (e.g., \edef) you need to take care of
expandable control-sequences and expandable
active-characters not getting expanded in a way which
later affects the resuling numeral-value.

In your special case I suggest:

\edef\restoreCRCC{\catcode\number`\^^M=\the\catcode`\^^M }

or

\edef\restoreCRCC{\catcode`\noexpand\^^M=\the\catcode`\^^M }

or

\edef\restoreCRCC{\catcode13=\the\catcode13 }

...


By the way:

\edef\restoreCRCC{\catcode`^^M=\the\catcode`\^^M }

does not work because at reading/tokenizing-time
the first ^^M will be recognized as a character (not a token yet)
of catcode 5 (end of line), not as a control-sequence.

A crucial point in this case is that in any case TeX throws away
any other information that remain on the current line when
it encounters such an end of line character. In this case
anything behind the first ^^M will be dropped yielding that
the definition of \restoreCRCC is not complete.

Another crucial point in this case is that characters of
catcode 5 either yield space-tokens or character-tokens
or are simply dropped depending on the status of TeX'
reading-apparatus.

In the following case you get a space-token for the first ^^M
as TeX is in state M (mid-line) at the time of tokenizing that ^^M:

\edef\restoreCRCC{\catcode`^^M
=\the\catcode`\^^M }
\show\restoreCRCC
\bye

Ulrich

Ulrich D i e z

unread,
Aug 8, 2012, 5:22:58 PM8/8/12
to
I wrote:

> By the way:
>
> \edef\restoreCRCC{\catcode`^^M=\the\catcode`\^^M }
>
> does not work because at reading/tokenizing-time
> the first ^^M will be recognized as a character (not a token yet)
> of catcode 5 (end of line), not as a control-sequence.
>
> A crucial point in this case is that in any case TeX throws away
> any other information that remain on the current line when
> it encounters such an end of line character. In this case
> anything behind the first ^^M will be dropped yielding that
> the definition of \restoreCRCC is not complete.
>
> Another crucial point in this case is that characters of
> catcode 5 either yield space-tokens or character-tokens

Once more I don't know what I was thinking: In the line above
please replace "character" by "\par" :

Another crucial point in this case is that characters of
catcode 5 either yield space-tokens or \par-tokens
--

[Formerly appended fullquote was nuked by morver,
the versatile morphing server.]

Ulrich D i e z

unread,
Aug 8, 2012, 5:39:12 PM8/8/12
to
[I hate morver, the versatile morphing server as it nukes
formerly appended fullquotes when it shouldn't do so.]

I wrote:

> By the way:
>
> \edef\restoreCRCC{\catcode`^^M=\the\catcode`\^^M }
>
> does not work because at reading/tokenizing-time
> the first ^^M will be recognized as a character (not a token yet)
> of catcode 5 (end of line), not as a control-sequence.
>
> A crucial point in this case is that in any case TeX throws away
> any other information that remain on the current line when
> it encounters such an end of line character. In this case
> anything behind the first ^^M will be dropped yielding that
> the definition of \restoreCRCC is not complete.
>
> Another crucial point in this case is that characters of
> catcode 5 either yield space-tokens or character-tokens

Once more I don't know what I was thinking: In the line above
please replace "character" by "\par" :

Another crucial point in this case is that characters of
catcode 5 either yield space-tokens or \par-tokens

eds

unread,
Aug 9, 2012, 3:48:01 PM8/9/12
to
Thanks to everyone who answered. As usual your answers were perfect and very timely. I'm always impressed at how promptly answers are posted and how they are always exactly what I'm looking for.

Thanks a million.

regards,
eds.
0 new messages