Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX indentation (formatting) in Emacs

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!

like image 870
fikovnik Avatar asked Mar 19 '10 12:03

fikovnik


3 Answers

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)

like image 80
Adobe Avatar answered Oct 26 '22 22:10

Adobe


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.

like image 22
Marius Andersen Avatar answered Oct 26 '22 23:10

Marius Andersen


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)
like image 37
Chris Conway Avatar answered Oct 26 '22 23:10

Chris Conway