TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 ()
    ;; Insert a newline before a TeX command starting a line to avoid
    ;; fill-paragraph to include it in the current paragraph
    (let ((end-point
              (re-search-forward "\n\n" (point-max) t))
      (replace-regexp "\n\\\\" "\n\n\\\\" t (point) (- end-point 2))))
share|improve this answer

Your Answer


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.