# How to emulate a newline without a <return> char?

This works:

a^^M%
^^M%
b


But a real newline is still needed after %. If I remove the newlines and use

a^^M^^Mb


The result is just a letter a. It seems that, everything after the first ^^M is ignored because ^^M is the end of line!

Is there a way to change the catcodes or set \endlinechar or do something else, so that I can write one line but it works like several lines of input? There's no actual usage, I'm just curious about it.

-
Could you give as a little bit more context (no, not ConTeXt) for this? Are you not talking about a verbatim environment, right? There is AFAIK, no way to make TeX think one source line is several lines. –  Martin Scharrer Apr 16 '12 at 12:59
One similar thing with the real end-of-line in the source file which bugged me is that the line break at the end of a line line \begin{myverbatimenv} is already inserted before the environment code is executed, so changing \endlinechar in it doesn't affect this end-of-line but all others in the environment. It's catcode seems however not been assigned yet, so you can still make it active. It would be interesting to learn how exactly TeX processes the input lines. –  Martin Scharrer Apr 16 '12 at 13:04
@Martin: Yes, I was just trying some useless tricks and found that changing catcodes and \endlinechar not work. Then the question raised. –  Leo Liu Apr 16 '12 at 13:16

The answer is no. When TeX finds an end-of-record signal (as determined by the operating system) it throws away whatever remains on the input line and inserts the (character codecategory code) pair determined by the value of \endlinechar into the input stream and reads it.

The value of \endlinechar is usually 13 (^^M) which (usually) corresponds to a character with category code 5. This enables the process by which TeX can recognize an empty line, inserting a \par token.

An explicit category code 5 character in the input stream has the precedence over the end-of-record signal; so, when TeX finds

a^^Mb


and ^^M has category code 5, the b is thrown away. If ^^M has another category code, it is read and digested or stored as such.

All you can do is to write multiple records in a file:

\newlinechar\^^J
\newwrite\linewrite
\immediate\openout\linewrite=lines.dat
\immediate\write\linewrite{a^^Jb^^c}
\immediate\closeout\linewrite
\input lines.dat


or, with an e-TeX engine such as pdftex,

\newlinechar\^^J
\scantokens{a^^Jb^^Jc}


which is equivalent to what done before.

Note: the current TeX engines are able to distinguish among different end-of-record signals, overriding the operating system; but once the end-of-record signal type is decided (by examining the beginning of the input file), that signal is used throughout the file. But this really has no consequence on what said before, because the operating system signal or the one determined at runtime are discarded anyway.

-
About "When TeX finds an end-of-record signal": Does TeX read the input file character by character or line by line? Is code at the begin of the line already processed before the end-of-line is encountered or not? –  Martin Scharrer Apr 16 '12 at 13:10
Thank you for the detailed explanation, and I like the \scantokens solution. –  Leo Liu Apr 16 '12 at 13:12
@Martin: As in TeX by Topic, TeX reads a file line by line. But I didn't plan to read the source code of TeX, so I know little about the internals. –  Leo Liu Apr 16 '12 at 13:14
@MartinScharrer As far as I can understand, the line is stored into a buffer (with \endlinechar added at the end) and only then the tokenizing process is started. So changing the catcode of ^^M in the middle of a line has consequences on the same line. But one must be aware of the fact that \catcode\^^M=12 at the end of the line will not change the category code of the immediately following ^^M character, because TeX looks for a space after a constant, so reads ahead (and does tokenization). –  egreg Apr 16 '12 at 13:21
@egreg: Thanks, that's what I thought. This can be a little confusing in special applications. Also, the EOL after \endinput still ends up in the document (if its active and therefore not discarded), IIRC. –  Martin Scharrer Apr 16 '12 at 13:33
show 1 more comment

^^M normally has catcode 5 which is why it is end of line, you can give it catcode (say) 13 and then define it to do anything you like. The standard verbatim environment makes it catcode 13 and defined to be \par (more or less). Try

\obeycr


and then ^^M will be the same as \\\relax as LaTeX does:

{\catcode\^^M=13 \gdef\obeycr{\catcode\^^M13 \def^^M{\\\relax}%
\@gobblecr}%

-
I'm sorry but I know how to use active characters, \\ and \par. And I know exactly how verbatim works. The newline character in TeX is special, one newline is a space and two newlines makes a \par. I mean it separates the text file into different lines --- and I can't do that without it. –  Leo Liu Apr 16 '12 at 12:58
No. It is not automatic that two newlines in the source make a \par. It requires two tokens of catcode5. If \endlinechar` is set to insert ^^M and ^^M is not category 5 then different things happen as I tried to explain in my answer. –  David Carlisle Apr 16 '12 at 15:01