0

I have tons of LaTeX files with hard returns at column 80 like the test file below:

\documentclass{article}

\begin{document}

Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo
bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
foobar Foo bar foobar:
\begin{equation}
  x^2 + x + 1 = 0.
\end{equation}
% Foo
% Bar
\begin{itemize}
\item Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar.
\item Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo
  bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar
  Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar
  Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo
  bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar
  foobar Foo bar foobar Foo bar foobar.
\end{itemize}

\end{document}

Now, I use soft wrap with visual-line-mode. If I were to type the same file now, I would get something like this:

\documentclass{article}

\begin{document}

Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar:
\begin{equation}
  x^2 + x + 1 = 0.
\end{equation}
% Foo
% Bar
\begin{itemize}
\item Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar.
\item Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar Foo bar foobar.
\end{itemize}

\end{document}

Is there a way to tell emacs to do the conversion while preserving the environments and comments lines.

Note: I use auctex if it matters.

  • If you insert a special symbol after each place you do not want a soft return (e.g., at the end of a line containing a % and at the end of a } [with probably a few exceptions where they appear in the middle of the line]), then you can globally convert all hard returns to a space (i.e., that will become soft returns), and then search for that special symbol and convert all of them back to a hard return. With the multiple-cursors package, the project would take about 5 seconds to do the whole thing. – lawlist Sep 25 '18 at 15:23
  • @lawlist Thanks for the hint. I'll have to manage indents too (inside enumerate for example). Anyway, I'll have a look at multiple-cursors. – cjorssen Sep 25 '18 at 16:03
  • You may find this useful: emacs.stackexchange.com/questions/2606/… . It's not exactly automatic, but it offers some speedup over deleting hard returns one by one. – wvxvw Sep 25 '18 at 16:11
  • What @wvxvw said. You can also try just C-x f 999999, and then use M-q on the text to unfill. – Drew Sep 25 '18 at 16:38
  • @wvxvw Thanks for pointing me to that question. That sounds better than doing the thing by hand! – cjorssen Sep 25 '18 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.