When I edit a .tex file in emacs, I frequently use fill-paragraph. Comments indicated with '%' sometimes wrap correctly, and sometimes they do not. It is not clear to me what is making this fail.


% This is a comment
This is a very very very long line of very very very long text

When I run fill-paragraph, I sometimes get the undesirable:

% This is a comment This is a very very 
very long line of very very very long text

I'm using Emacs 23.1.1 in Latex-mode, and the following minor modes are enabled:

Enabled minor modes: Abbrev Auto-Composition Auto-Compression
Auto-Encryption Auto-Fill Display-Time File-Name-Shadow Font-Lock
Global-Auto-Composition Global-Font-Lock Iswitchb Line-Number Outline
Shell-Dirtrack Show-Paren
  • Without a precise recipe to reproduce the problem it's hard to help you. – giordano Sep 5 '13 at 19:04
  • Doesn't happen to me. – egreg Sep 5 '13 at 19:09
  • 3
    Doesn't happen to me either, although I find wrapping text near comments sometimes behaves 'incorrectly' when using TeX-mode. Are you sure you're in LaTeX-mode? And do you have AUCTeX installed? – jon Sep 5 '13 at 19:44
  • jon, I've just installed AUCTex (hadn't heard of it before), and commenting works correctly and everything looks much better. Thanks! – pnj Sep 6 '13 at 3:15
  • 1
    AUCTeX (and RefTeX) is great! As for the protocol, I'm pretty sure there must be discussion on the meta site. I think the best thing is either to close to question or get it answered somehow. I can try to find time this weekend to post a slightly more verbose answer, unless it gets closed in the meantime. – jon Sep 6 '13 at 16:04

(Just to take the question off the unanswered list.)

I find this behaviour --- i.e., incorrect line-wrapping near comments via M-x fill-paragraph or M-q --- sometimes occurs when using Emacs in TeX-mode. With LaTeX-mode, I have not experienced this problem. With AUCTeX the situation is still improved, and this is one of the least important reasons to use AUCTeX. (Anyway, for more information on modes, see here.)

If Emacs is guessing the 'wrong' mode for your file, you can help it by setting the local variables. This can be done with a simple

% -*- mode: latex -*-

at the top of the file. A preferred method, however, is to use the Local Variables block at/near the end of the file:

%%% Local Variables: 
%%% mode: latex   
%%% TeX-master: t   
%%% End:

I like this better because you can set other options at the same time; e.g.,

%%% Local Variables: 
%%% mode: latex   
%%% TeX-master: t   
%%% TeX-engine: luatex
%%% mode: flyspell
%%% End:
  • I am using (add-hook 'LaTeX-mode-hook 'turn-on-auto-fill) which still generates the same problem – alper May 7 '20 at 21:15

For GNU Emacs, this bug has been reported by Michael Orlitzky, who submitted the following patch which will be included in Emacs 27.1:

--- a/tex-mode.el   2019-01-07 09:26:07.000000000 -0500
+++ b/tex-mode.el   2019-11-09 08:42:56.649424361 -0500
@@ -1155,7 +1155,7 @@
        "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t)
   (setq paragraph-separate
-   (concat "[\f%]\\|[ \t]*\\($\\|"
+   (concat "\\([ \t]*%\\)\\|[\f]\\|[ \t]*\\($\\|"

The patch fixes some cases but not all. For instance the bug still occurs if you have a comment at the end of a line of code, followed by another line of code. I've reported this as well and will edit this answer with any updates.

Your Answer

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

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