Emacs中的LaTeX缩进(格式化)

时间:2010-03-19 12:29:48

标签: emacs latex formatting indentation

在Emacs(AucTex)中缩进LaTeX文档的正确方法是什么?

例如,当我有一个列表时:

\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}

并希望最终得到:

\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}

我尝试indent-region但它没有做任何事情而且LaTeX-fill-*会产生奇怪的结果,如:

\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}

谢谢!

3 个答案:

答案 0 :(得分:23)

您确定已正确安装AUCTeX吗? AUCTeX的LaTeX模式称为LaTeX-mode,而latex-mode是(跛脚)Emacs默认值。使用C-h m检查当前主要模式。

当我将光标放在环境的开头并按C-c C-q C-eLaTeX-fill-environment)时,我会得到以下信息:

\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}

AUCTeX填充命令列表:

  • C-c C-q C-eLaTeX-fill-environment
  • C-c C-q C-pLaTeX-fill-paragraph
  • C-c C-q C-rLaTeX-fill-region
  • C-c C-q C-sLaTeX-fill-section

您也可以在输入时按M-qfill-paragraph)。

答案 1 :(得分:3)

要获得2个空格缩进:

(setq LaTeX-item-indent 0)

source

您可以使用以下方式自动填充环境:

(add-hook 'LaTeX-mode-hook 'turn-on-auto-fill)

source

答案 2 :(得分:1)

我大量使用了以下函数,这是我从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))))))

这也适用于AUCTeX之外。我使用:

将其绑定到M-j
(global-set-key "\ej" 'fill-sentence)