[Tex/LaTex] Best practices for source file line lengths

best practicesgitline-breakingsourcesourcecode

Are there any best practices for how long to make your lines in your source file? I'm also interested if there are best practices for it if there aren't any LaTeX-specific best practices.

I originally typed my LaTeX files like I was in Word:

  • each paragraph on its own line.
  • However, I eventually found that to be rather cumbersome and so I switched to one sentence per line (around the same time I moved from Dropbox to git).

That was large improvement for me.

Since I tend to write long sentences and like my footnotes, I tried an experiment in a file:

  • break lines after commas as well as after periods.
  • So far, it seems that this method would require a good bit of manual clean-up, since it produces some very short lines and there are still overly-long lines in the file.

What methods have you found to be the best for keeping your source's line lengths reasonable?

Best Answer

I'll use an example from the TeXBook source

This manual is intended for people who have never used \TeX\ before, as
well as for experienced \TeX\ hackers. In other words, it's supposed to
be a panacea that satisfies everybody, at the risk of satisfying nobody.
Everything you need to know about \TeX\ is explained
here somewhere, and so are a lot of things that most users don't care about.
If you are preparing a simple manuscript, you won't need to
learn much about \TeX\ at all; on the other hand, some
things that go into the printing of technical books are inherently
difficult, and if you wish to achieve more complex effects you
will want to penetrate some of \TeX's darker corners. In order
to make it possible for many types of users to read this manual
effectively, a special sign is used to designate material that is
for wizards only: When the symbol
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
appears at the beginning of a paragraph, it warns of a ``^{dangerous bend}''
in the train of thought; don't read the paragraph unless you need to.
Brave and experienced drivers at the controls of \TeX\ will gradually enter
more and more of these hazardous areas, but for most applications the
details won't matter.

the lines here are all less than 80 characters wide, I can show it here and everyone sees the same thing. I (or diff or similar tools) can refer to "experienced" in line 2, and everyone sees the same linebreaks and the whole whole thing is understandable.

If it was all one line it would look like

This manual is intended for people who have never used \TeX\ before, as well as for experienced \TeX\ hackers. In other words, it's supposed to be a panacea that satisfies everybody, at the risk of satisfying nobody. Everything you need to know about \TeX\ is explained here somewhere, and so are a lot of things that most users don't care about. If you are preparing a simple manuscript, you won't need to learn much about \TeX\ at all; on the other hand, some things that go into the printing of technical books are inherently difficult, and if you wish to achieve more complex effects you will want to penetrate some of \TeX's darker corners. In order to make it possible for many types of users to read this manual effectively, a special sign is used to designate material that is for wizards only: When the symbol $$\vbox{\hbox{\dbend}\vskip 11pt}$$ appears at the beginning of a paragraph, it warns of a ``^{dangerous bend}'' in the train of thought; don't read the paragraph unless you need to. Brave and experienced drivers at the controls of \TeX\ will gradually enter more and more of these hazardous areas, but for most applications the details won't matter.

which is OK for TeX but looks horrible here

Even if your editor "soft wraps" to (say) 70 characters it would look like

This manual is intended for people who have never used \TeX\ before,
as well as for experienced \TeX\ hackers. In other words, it's
supposed to be a panacea that satisfies everybody, at the risk of
satisfying nobody. Everything you need to know about \TeX\ is
explained here somewhere, and so are a lot of things that most users
don't care about. If you are preparing a simple manuscript, you won't
need to learn much about \TeX\ at all; on the other hand, some things
that go into the printing of technical books are inherently difficult,
and if you wish to achieve more complex effects you will want to
penetrate some of \TeX's darker corners. In order to make it possible
for many types of users to read this manual effectively, a special
sign is used to designate material that is for wizards only: When the
symbol $$\vbox{\hbox{\dbend}\vskip 11pt}$$ appears at the beginning of
a paragraph, it warns of a ``^{dangerous bend}'' in the train of
thought; don't read the paragraph unless you need to. Brave and
experienced drivers at the controls of \TeX\ will gradually enter more
and more of these hazardous areas, but for most applications the
details won't matter.

but the apparent line breaks come in more arbitrary places and as they are not really there different tools and different people would see different line breaks.

You could add a linebreak at major punctuation (here ,.;: and $$ math display)

This manual is intended for people who have never used \TeX\ before,
as well as for experienced \TeX\ hackers.
In other words,
it's supposed to be a panacea that satisfies everybody,
at the risk of satisfying nobody.
Everything you need to know about \TeX\ is explained here somewhere,
and so are a lot of things that most users don't care about.
If you are preparing a simple manuscript,
you won't need to learn much about \TeX\ at all; on the other hand,
some things that go into the printing of technical books are inherently difficult,
and if you wish to achieve more complex effects you will want to penetrate some of \TeX's darker corners.
In order to make it possible for many types of users to read this manual effectively,
a special sign is used to designate material that is for wizards only:
When the symbol
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
appears at the beginning of a paragraph,
it warns of a ``^{dangerous bend}'' in the train of thought;
don't read the paragraph unless you need to.
Brave and experienced drivers at the controls of \TeX\ will gradually enter more and more of these hazardous areas,
but for most applications the details won't matter.

which is sort of OK, but still has some over-long lines and just makes editing harder as you edit the punctuation you have to merge lines.