开发者

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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜