views:

334

answers:

2

Hi,

what is the correct way to do indentation of a LaTeX document in Emacs (AucTex)?

For example when I have a list:

\begin{itemize}
\item orem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim urna, mattis eu aliquet eget, condimentum id nibh. In hac habitasse platea dictumst.
\item orem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim urna, mattis eu aliquet eget, condimentum id nibh. In hac habitasse platea dictumst.
\end{itemize}

and would like to ended up with:

\begin{itemize}
  \item orem ipsum dolor sit amet, consectetur
    adipiscing elit. Aliquam enim urna, mattis eu aliquet eget,
    condimentum id nibh. In hac habitasse platea dictumst.
  \item orem
    ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim
    urna, mattis eu aliquet eget, condimentum id nibh. In hac
    habitasse platea dictumst.
\end{itemize}

I tried indent-region but it doesn't do anything and the LaTeX-fill-* produces weird results like:

\begin{itemize} \item orem ipsum dolor sit amet, consectetur
  adipiscing elit. Aliquam enim urna, mattis eu aliquet eget,
  condimentum id nibh. In hac habitasse platea dictumst. \item orem
  ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim
  urna, mattis eu aliquet eget, condimentum id nibh. In hac
  habitasse platea dictumst. \end{itemize}

Thanks!

+3  A: 

Are you sure you have installed AUCTeX correctly? AUCTeX's LaTeX mode is called LaTeX-mode, while latex-mode is the (lame) Emacs default. Check the current major mode with C-h m.

When I place the cursor at the beginning of the environment and press C-c C-q C-e (LaTeX-fill-environment), I get the following:

\begin{itemize}
\item orem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam
  enim urna, mattis eu aliquet eget, condimentum id nibh. In hac
  habitasse platea dictumst.
\item orem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam
  enim urna, mattis eu aliquet eget, condimentum id nibh. In hac
  habitasse platea dictumst.
\end{itemize}

List of AUCTeX fill commands:

  • C-c C-q C-e (LaTeX-fill-environment)
  • C-c C-q C-p (LaTeX-fill-paragraph)
  • C-c C-q C-r (LaTeX-fill-region)
  • C-c C-q C-s (LaTeX-fill-section)

You can also just press M-q (fill-paragraph) as you type.

Marius Andersen
I personally use fill paragraph (M-q), it work most mode, and it's simpler to type
Rémi
I looks like that it must be a problem in my Auctex installation as I'm using PDFLaTeX/F major mode (M-x LaTeX-mode) and if I do fill-paragraph then I have it messed up. The fill-paragraph seems to be LaTeX unaware and it just joins reformats the text.
fikovnik
After spending some more time with that I'm still getting a mixed results, but if I set fill-column to 70 it's mostly ok. Weird, but could some issue with the aquamacs 2b5. Anyway I think I will start doing one sentence per line way of editing and in that case this is no longer necessary.
fikovnik
A: 

I make extensive use of the following function, which I borrowed from Luca da Alfaro:

(defun fill-sentence ()
  (interactive)
  (save-excursion
    (or (eq (point) (point-max)) (forward-char))
    (forward-sentence -1)
    (indent-relative)
    (let ((beg (point)))
      (forward-sentence)
      (if (equal "LaTeX" (substring mode-name (string-match "LaTeX" mode-name)))
          (LaTeX-fill-region-as-paragraph beg (point))
        (fill-region-as-paragraph beg (point))))))

This works outside AUCTeX too. I bind it to M-j using:

(global-set-key "\ej" 'fill-sentence)
Chris Conway
Technically that's a function not a macro. I only mention it because between Emacs and LaTeX, there are 3 different types of macros already.
Ivan Andrus
Fair enough....
Chris Conway