When inserting an itemize environment around some text (using C-c C-e) AucTeX insists on doing paragraph-filling on said text, which messes up my layout. Is there a simple way to prevent that ?

Context is, I'm turning plain-text notes into a latex document. When the plain-text draft contains:

- first item
- second item

I want to select the two lines, type C-c C-e itemize RET, and obtain:

\item - first item
- second item

Then it would be easy to add the missing \item commands where required. But instead, AucTeX calls LaTeX-fill-region on the content of the environment (cf latex.el line 686) and I end up with:

\item - first item - second item

Obviously this is no big deal for such a short list. But for some longer text it is, because the structure of my list is lost.


I had a quick look at the source and auctex really wants to fill the region-)

Obviously you could change everywhere that needs changing but it would be simpler just to write a simple function that just inserted begin and end not really based on the auctex code.

or simplest is to select the region

- first item
- second item

and use C-w C-x C-e <return> C-y

Ie kill the region insert the environment around nothing then yank the region back. It's only a couple of extra keystrokes so less than it would take to write a new function unless you are doing this a lot:-)


The reason is that LaTeX-env-item function in latex.el calls LaTeX-fill-paragraph.

Fortunately, this behavior can be changed with advising these functions. The following code turns off LaTeX-fill-paragraph while LaTeX-env-item is running.

;; Empty function that just do nothing
(defun my-empty-func (orig-func &rest args))

;; Call orig-func with LaTeX-fill-paragraph disabled
(defun my-tex-no-fill (orig-func &rest args)
  ;; Replace LaTeX-fill-paragraph with my-empty-func
  (advice-add 'LaTeX-fill-paragraph :around #'my-empty-func)
  (let ((res (apply orig-func args))) ;; Call orig-func
    ;; Enable original LaTeX-fill-paragraph
    (advice-remove 'LaTeX-fill-paragraph #'my-empty-func)

;; Disable filling for LaTeX-env-item
(advice-add 'LaTeX-env-item :around #'my-tex-no-fill)

Note that this code uses advice-add/advice-remove functions that have been introduced in Emacs 24.4.


Shorter version using ignore function (thanks to giordano for his comment):

;; Call orig-func with LaTeX-fill-paragraph disabled
(defun my-tex-no-fill (orig-func &rest args)
  ;; Disable LaTeX-fill-paragraph
  (advice-add 'LaTeX-fill-paragraph :around #'ignore)
  (let ((res (apply orig-func args))) ;; Call orig-func
    ;; Enable LaTeX-fill-paragraph
    (advice-remove 'LaTeX-fill-paragraph #'ignore)

;; Disable filling for LaTeX-env-item
(advice-add 'LaTeX-env-item :around #'my-tex-no-fill)
  • 1
    You don't need to define a new "empty" function, ignore works just fine. – giordano Nov 16 '15 at 10:13

Here's a quick & dirty function for you:

(defun itemize (beg end) 
 "wrap the active region in an 'itemize' environment,
  converting hyphens at the beginning of a line to \item"
  (interactive "r")
    (narrow-to-region beg end)
    (insert "\\begin{itemize}\n")
    (while (re-search-forward "^- " nil t)
      (replace-match "\\\\item "))
    (insert "\\end{itemize}\n")))

Select the region you want to convert and call M-x itemize. This hard-codes "- " at the beginning of a line as the beginning of an item. If you use other conventions, you'll need to tweak it accordingly.


Use M-<return> at the right position (after first item), which insert a return and a \item. M-<return> is also great when using RefTex-mode in align-environments, as it inserts \\ and the next label.

(More a comment, but too low reputation as a new user to comment :/)

  • Sure. And my point is precisely that I want to be able to type <M-return> at the beginning of each line without having to go fishing for my dashes :-) – Gyom Jun 14 '13 at 12:14

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.