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.

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

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:

\begin{itemize}
\item - first item
- second item
\end{itemize}

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:

\begin{itemize}
\item - first item - second item
\end{itemize}

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.

share|improve this question
up vote 7 down vote accepted

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:-)

share|improve this answer

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)
    res))

;; 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.

EDIT:

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)
    res))

;; Disable filling for LaTeX-env-item
(advice-add 'LaTeX-env-item :around #'my-tex-no-fill)
share|improve this answer
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")
  (save-restriction
    (narrow-to-region beg end)
    (beginning-of-buffer)
    (insert "\\begin{itemize}\n")
    (while (re-search-forward "^- " nil t)
      (replace-match "\\\\item "))
    (end-of-buffer)
    (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.

share|improve this answer

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 :/)

share|improve this answer
    
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

 
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.