在 \[...\] 环境中使用 AucTeX 自动缩进

在 \[...\] 环境中使用 AucTeX 自动缩进

AucTex 可以很好地自动缩进大多数环境,例如

\begin{displaymath}
  \cos x
\end{displaymath}

但当我使用 \[ 和 \] 时不会缩进,并自动删除我手动完成的任何缩进。它看起来像这样

\[
\cos x
\]

但我希望它看起来像这样

\[
  \cos x
\]

似乎 AucTeX 无法将 \[...\] 识别为缩进的 displaymath 环境。我查看了 AucTeX 的缩进自定义,但似乎没有办法添加要缩进的环境。据我了解,用户选项“LaTeX-indent-environment-list”用于特殊例外。

有什么办法可以让 AucTeX 在 \[ 和 \] 之间缩进?

答案1

将以下代码添加到初始化文件中并重新启动 Emacs

(eval-after-load "latex"
  '(progn
     (defun LaTeX-indent-calculate (&optional force-type)
       "Return the indentation of a line of LaTeX source.
FORCE-TYPE can be used to force the calculation of an inner or
outer indentation in case of a commented line.  The symbols
'inner and 'outer are recognized."
       (save-excursion
     (LaTeX-back-to-indentation force-type)
     (let ((i 0)
           (list-length (safe-length docTeX-indent-inner-fixed))
           (case-fold-search nil)
           entry
           found)
       (cond ((save-excursion (beginning-of-line) (bobp)) 0)
         ((and (eq major-mode 'doctex-mode)
               fill-prefix
               (TeX-in-line-comment)
               (progn
             (while (and (< i list-length)
                     (not found))
               (setq entry (nth i docTeX-indent-inner-fixed))
               (when (looking-at (nth 0 entry))
                 (setq found t))
               (setq i (1+ i)))
             found))
          (if (nth 2 entry)
              (- (nth 1 entry) (if (integerp comment-padding)
                       comment-padding
                     (length comment-padding)))
            (nth 1 entry)))
         ((looking-at (concat (regexp-quote TeX-esc)
                      "\\(begin\\|end\\){\\("
                      LaTeX-verbatim-regexp
                      "\\)}"))
          ;; \end{verbatim} must be flush left, otherwise an unwanted
          ;; empty line appears in LaTeX's output.
          0)
         ((and LaTeX-indent-environment-check
               ;; Special environments.
               (let ((entry (assoc (or LaTeX-current-environment
                           (LaTeX-current-environment))
                       LaTeX-indent-environment-list)))
             (and entry
                  (nth 1 entry)
                  (funcall (nth 1 entry))))))
         ((looking-at (concat (regexp-quote TeX-esc)
                      "\\("
                      LaTeX-end-regexp
                      "\\)"))
          ;; Backindent at \end.
          (- (LaTeX-indent-calculate-last force-type) LaTeX-indent-level))
         ((looking-at (concat (regexp-quote TeX-esc) "right\\b"))
          ;; Backindent at \right.
          (- (LaTeX-indent-calculate-last force-type)
             LaTeX-left-right-indent-level))
         ((looking-at (concat (regexp-quote TeX-esc)
                      "\\("
                      LaTeX-item-regexp
                      "\\)"))
          ;; Items.
          (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
         ((looking-at "}")
          ;; End brace in the start of the line.
          (- (LaTeX-indent-calculate-last force-type)
             TeX-brace-indent-level))
         ((and (texmathp)
               ;; Display math \[...\], treat as a generic environment.
               (equal "\\[" (car texmathp-why)))
          LaTeX-indent-level)
         (t (LaTeX-indent-calculate-last force-type))))))
     ;; Treat \] as a generic \end{...}
     (setq LaTeX-end-regexp "end\\b\\|\\]")))

重新定义LaTeX-indent-calculate以适应\[...\]数学模式。尚未彻底测试,可能需要一些修复。

答案2

按照@giordano 的出色回答并使用其作为基础,对问题进行了跟进,我直接在文件中进行了更改latex.el,然后配置并编译了 AucTeX。

这些改变大多是为了尝试\(而不是\[

         ;; Items.
     (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
    ((looking-at "}")
     ;; End brace in the start of the line.
     (- (LaTeX-indent-calculate-last force-type)
    TeX-brace-indent-level))
    ((and (texmathp)
      ;;Display math \[...\], treat as a generic environment
      (equal "\\[" (car texmathp-why)))
     LaTeX-indent-level)
    ((or (texmathp)
     (equal "\\(" (car texmathp-because)))
     LaTeX-indent-level)
    (t (LaTeX-indent-calculate-last force-type))))))

但以上是对当前版本的一次平庸尝试。\(它做了它应该做的事......但仅此而已,它毫无用处。

另一方面,下面的内容比上面的内容更不低于标准:

              ;; Items.
 (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
((looking-at "}")
 ;; End brace in the start of the line.
 (- (LaTeX-indent-calculate-last force-type)
TeX-brace-indent-level))
((and (texmathp)
  ;;Display math \[...\], treat as a generic environment
  (equal "\\[" (car texmathp-why)))
 LaTeX-left-right-indent-level)
(t (LaTeX-indent-calculate-last force-type))))))

第三次也是最后一次编辑:这就是我要说的!我没看到,真丢脸。

\(和均\[使用最新的 AucTeX 进行编译。

              ;; Items.
 (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
((looking-at "}")
 ;; End brace in the start of the line.
 (- (LaTeX-indent-calculate-last force-type)
TeX-brace-indent-level))
((and (texmathp)
  ;;Display math \[...\], treat as a generic environment
  (equal "\\[" (car texmathp-why)))
 LaTeX-indent-level)
((and (texmathp)
  (equal "\\(" (car texmathp-why)))
 LaTeX-indent-level)
 (t (LaTeX-indent-calculate-last force-type)))))) 

相关内容