TransWikia.com

Is there any difference between a space and a newline for compilation?

TeX - LaTeX Asked on July 12, 2021

We handle arbitrary user input and attempt to programmatically generate LaTeX for compilation.
One issue we sometimes run into is users writing very long lines (paragraphs) of text, resulting in an error:

! Unable to read an entire line

The sensible — but potentially naïve — solution seems to be for us to set some line length limit, and just break the line ourselves on a space. For example, given:

foo bar baz

and a line limit of 5 characters, we’d end up with:

foo bar
baz

…which should just render the same, since spaces and newlines are interchangeable.

This even works fine for macros:

textit{foo
bar baz}

However, does this work in all cases? Are there any instances where a newline character would be treated differently to a space character? Or are they perfectly interchangeable?

One Answer

A single end of line in the default catcode regime produces a space token.

In verbatim-like environments or more generally in the scope of obeylines then it will produce a forced linebreak in the output.

In special environments endlinechar may be set negative then the end of line does not produce a token at all. (Separately in expl3 space characters do not produce a token either so in that case end of line still acts as space)

One other case is long comments

LaTeX code % very very very long comment

If you simply wrap long lines with perl or python etc before passing to tex you will end up with

LaTeX code % very very
very long comment

Then you end up uncommenting text.

Correct answer by David Carlisle on July 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP