Longlines mode in LaTeX30 Sep 2014, by Artur Malabarba.
Emacs.SE Lessons post series
Emacs.SE has truly revived my init file. Every day, a new snippet gets added. I started this series only 6 days ago, and it’s already on its fourth episode.
Today's question was asked by me, and answered by Tikhon Jelvis, Francesco, and Sacha Chua. As usual, this link might not work for you until the beta goes public.
I’ve long been keeping my LaTeX documents under version control. Still, the effectiveness of this initiative would be greatly improved if I could bring myself to adopt a “one-sentence-per-line” approach. It would facilitate managing people’s conributions and reverting old changes.
If you’re wondering what’s so difficult about that, here is what such a document would look like. (Long sentences are to be avoided, but in scientific writing you don’t always have a choice).
The lack of readability above is evident. Having text extend beyond 80 columns is trouble enough, but intertwining long and short lines just makes my brain weep in despair.
The official solution is to activate
visual-line-mode and add window
margins (or just decrease window width). That causes the lines to wrap
(visually, not in the file) and increases readability by an order of
But alas, it comes at a cost. If this text were indented by a few spaces (quite common in LaTeX), the outcome looks like it went through a blender. And don’t get me started on broken equations.
tdsh points out in the comments, the
adaptive-wrap package fixes
the indentation issue for
The solution reached is a hack to make
longlines-mode act more
intelligently on LaTeX buffers. This minor mode is similar to the
visual-line-mode and window margins mentioned
above. It makes lines wrap at
fill-column, intead of
The advantage to
longlines-mode is that it’s implemented in elisp
(while window margins are done in C code), so we can hack it to our
hearts’ content. Bafflingly, this little gold nugget has been marked
obsolete on Emacs 24.4.
The hack below does 3 things:
- It enables “soft” wrapping of text. That is, the text is wrapped to
fill-columnin the buffer, but that does not reflect in the file.
- It fixes the indentation problem. So wrapping an indented line follows the line’s indentation. So environments with indented text (like itemize, or theorem) are much more readable.
- It even prevents wrapping of equations!
The code is a bit heavy, so I’ve added comments describing the lines which were actually changed.
Fixed handling of comments. Now comments don’t get wrapped either.