# Catcode of \endlinechar when its charcode is less than 1

When \endlinechar is less than 1, how do we access its catcode? The following gives the error shown below:

! Bad character code (-1).
l.34 ...ndlinechar\string=\the\catcode\endlinechar
\relax%
? x

\endlinechar=\m@ne
\edef\restorecatcodes{%
\let\noexpand\restorecatcodes\relax%
\endlinechar\string=\the\endlinechar\relax%
\catcode\the\endlinechar\string=\the\catcode\the\endlinechar\relax%
\catcode\string\noexpand\string=\the\catcode\string\relax%
\catcode\string\noexpand=\string=\the\catcode\string=\relax%
}%


EDIT

I have seen an approach by Michael Downes (in the pcatcode package). It looks like the following:

\begingroup%
\let\e\endlinechar\edef\p{\the\e}\e13\edef\n{\the\catcode\e}%
\xdef\restorecatcodes{%
\let\noexpand\restorecatcodes\relax%
\endlinechar=\p\relax\catcode\number\e=\n\relax%
\catcode\string\noexpand\string=\the\catcode\string\relax%
\catcode\string\noexpand=\string=\the\catcode\string=\relax%
}%
\endgroup%


I think his assumption is that changing the charcode will not affect the catcode.

-
In the code by M. Downes, \n stores the category code of the character ^^M, not of the \endlinechar when \begingroup is executed. –  egreg Oct 28 '12 at 18:41
@egreg: Here is the code by Mike that I referred to: \begingroup\let\e\endlinechar\iffalse\<line end> \fi\chardef\E\e\e13\chardef\n\catcode\e\catcode\e5\relax\relax. I guess we could use the comment sign for \iffalse\<line end>\fi. –  Ahmed Musa Oct 29 '12 at 0:58

In TeX a character code has to be between 0 and 255 to be a valid character (as TeX deals with 8 bit encodings). Setting \endlinechar to -1 means that no endline character is inserted (bit of a supported hack, as Don thought this would be useful I guess). As -1 is not a valid character code there is no \catcode associated with it, which is why you get this error if you try to access it.

## Update to match edit in question

In TeX there are 256 different characters tokens denoted by character codes 0 to 255. Each character has an associated \catcode which is bound to the character code. The three lines

\catcode 98=13
\catcode b=13     % only possible if the char (here "b") has not a special catcode like comment etc
\catcode \b=13    % always safe


are all doing the same, i.e. they set the catcode of character 98 which is ascii "b" to 13 (active).

The \endlinechar holds a number which denotes the character code to be appended at the end of the line (if its between 0 and 255). The catcode of this endline character is the catcode of the character token it points to. So if you do

\endlinechar=98


then at end end of each line a "b" is appended with whatever catcode "b" currently has. In other words, if you temporarily change the \endlinechar, the catcode of the original character is not modified. The only thing that happens is that a different character is appended at the end of the line (or none in case of -1). Thus all you have to do to get back to the old situation is to reset \endlinechar to its previous number.

-
Good explanation. Thanks. The next thing is the workaround. –  Ahmed Musa Oct 28 '12 at 16:45
well, not sure what you want to achieve. if your code is supposed to save current values to be able to restore them later, then perhaps you should put the change to \endlinechar after the definition to store its current meaning not its new meaning ... or am I missing something? –  Frank Mittelbach Oct 28 '12 at 17:35

Just check whether the \endlinechar can have a catcode:

\edef\restorecatcodes{%
\let\noexpand\restorecatcodes\relax%
\endlinechar\string=\the\endlinechar\relax%
\ifnum\endlinechar>-1 \ifnum\endlinechar<256%
\catcode\the\endlinechar\string=\the\catcode\the\endlinechar\relax%
\fi\fi
\catcode\string\noexpand\string=\the\catcode\string\relax%
\catcode\string\noexpand=\string=\the\catcode\string=\relax%
}%

\endlinechar=-1

\restorecatcodes
\showthe\catcode\endlinechar


> 5.
l.16 \showthe\catcode\endlinechar


This shows that the endlinechar is restored to what it was before. If we say

\catcode\^^M=\active

\edef\restorecatcodes{%
\let\noexpand\restorecatcodes\relax%
\endlinechar\string=\the\endlinechar\relax%
\ifnum\endlinechar>-1 \ifnum\endlinechar<256%
\catcode\the\endlinechar\string=\the\catcode\the\endlinechar\relax%
\fi\fi
\catcode\string\noexpand\string=\the\catcode\string\relax%
\catcode\string\noexpand=\string=\the\catcode\string=\relax%
}%

\endlinechar=-1

\restorecatcodes
\showthe\catcode\endlinechar


> 13.

The same if we say \catcode\^^M=5instead of\endlinechar=-1.
Sorry to downvote, egreg, but the \endlinechar itself has no catcode. Catcodes are only attached to the character code, and \endlinechar is not a fixed character code. See Frank's explanation above. –  Bruno Le Floch Oct 28 '12 at 21:24
@BrunoLeFloch \endlinechar is not a character, but just an integer. The \showthe command simply tells what's the catcode of the character with \endlinechar ASCII code; it's used just for showing that \restorecatcodes does the right thing after having set \endlinechar to -1 and restored it to what it was before the setting. Sorry to be fussy, but you are thinking wrong about what \endlinechar is. –  egreg Oct 28 '12 at 21:37