Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I like the Format Paragraph function of Auctex/LaTeX in Emacs but it seems to move markup commands around. Is there any way I can format a buffer but only the plaintext sections of it so that it ends up like the fill paragraph or format region commands but without touching LaTeX commands?

Just to be a little more clear, if the command(s) are on a newline I want them to not be messed with, so commands that are inside the paragraph such as texbf can be moved around.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

I've written a quick hack to accomplish that in Emacs. The idea is the following: first, find the first "hard" paragraph separation (that is, two newlines). I'm sure there should be a better way of identifying paragraphs, maybe via some other elisp function, but I haven't looked at it. That separation will bound the area of application of the paragraph fill (fill-paragraph never goes beyond a hard paragraph). Then, we convert any "starting" TeX command (a \ just after a newline) into a starting of a paragraph of its own. It is ugly, as it changes the text, inserting newlines for TeX commands that start in a new line.

Note however that getting the correct behavior is sometimes impossible. For instance, if you maintain etiquette, your lines will be around 75 characters long and will end with a newline. By chance, a \textbf, for example, may start after a newline, but you obviously want that one inside the current paragraph.

This is the code. You can bind it to M-q when in TeX/LaTeX mode:

(defun latex-fill-paragraph ()
  (interactive)
  (save-excursion
    ;; Insert a newline before a TeX command starting a line to avoid
    ;; fill-paragraph to include it in the current paragraph
    (let ((end-point
           (or
            (save-excursion
              (re-search-forward "\n\n" (point-max) t))
            (point-max))))
      (replace-regexp "\n\\\\" "\n\n\\\\" t (point) (- end-point 2))))
  (save-excursion
    (fill-paragraph)))
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.