4

I would like that the two following code become equivalent:

Code 1 (Prints "a b")

\documentclass{report}

\begin{document}
a
b
\end{document}

Code 2 (Prints "ab")

\documentclass{report}

\begin{document}
ab
\end{document}

Is such a thing possible?

1 Answers1

10

You can adjust the way the end-of-line character ^^M is handled. We first make it active (change its category code to 13) and then define it to be \ignorespaces:

\documentclass{article}

\catcode`\^^M=\active\relax% Make end-of-line character active
\let^^M\ignorespaces
\begin{document}
a
b
\end{document}
Werner
  • 603,163
  • For some unknown reason, I had to put "\catcode ... ignorespaces" after "\begin{document}" otherwise it would not compile. Thank you for your help. – 永劫回帰 Nov 13 '15 at 07:12
  • 1
    @変幻出没: Yes. Once you change the category code of the end-of-line character, it affects from that point onward. Therefore, having an end-of-line character in the preamble turn into \ignorespaces doesn't really help. You'll note I don't leave an end-of-line character after the redefinition to \ignorespaces. – Werner Nov 13 '15 at 07:15
  • Sorry, I am not sure to understand "You'll note I don't leave an end-of-line character after the redefinition to \ignorespaces.". There is no end-of-line active on the "\catcode" line (because of the %) but there is one afer \ignorespaces, no? But maybe you meant that you did not left a blank line before \begin{document}? – 永劫回帰 Nov 13 '15 at 07:25
  • 2
    @変幻出没: The end-of-line character is read first as an end-of-statement for the sequence \let^^M\ignorespaces. So, at the time of reading it, ^^M has not been redefined. The next end-of-line character is inside the document environment. Regardless, setting category codes like this could lead to problems, so you're best off setting them just before you need them, even if it means you do it in the main document. – Werner Nov 13 '15 at 07:28