LaTeX indentation (formatting) in Emacs
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.
To get 2 spaces indentation:
(setq LaTeX-item-indent 0)
(source)
You can fill environment automatically with:
(add-hook 'LaTeX-mode-hook 'turn-on-auto-fill)
(source)
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)