Subject: 13.2; indentation of nested environments

From: Paul Nelson <[email-masked]>
Date: Thu, 31 Aug 2023 06:55:39 +0200

Take a LaTeX buffer with a nested environment where the inner \begin{}
follows some non-whitespace:

#+begin_src latex
\begin{equation*}
  g=\begin{pmatrix}
    a & b \\
    c & d \\
  \end{pmatrix}
\end{equation*}
#+end_src

After =M-x mark-whole-buffer= and =M-x indent-region=, the outer
\end{} is indented too much:

#+begin_src latex
\begin{equation*}
  x=\begin{pmatrix}
      a & b \\
      c & d \\
    \end{pmatrix}
  \end{equation*}
#+end_src

The issue is that indentation for the outer \end{} is anchored to that
for the inner \end{}, which is calculated relative to the position of
the inner \begin{}.  It should instead be calculated relative to the
beginning of the line (excluding whitespace) on which the inner
\begin{} occurs (the "x=" in this example).

A practical workaround for the user is to make sure that \begin{} is
always the first non-whitespace on its line (and to enforce that
requirement on coauthors via ~(defun fix-tex-buffer ...)~), but that
seems suboptimal.

The following seems to resolve the issue:

#+begin_src example
diff --git a/latex.el b/latex.el
index d855b5c..9648714 100644
--- a/latex.el
+++ b/latex.el
@@ -8922,9 +8922,13 @@ COL for efficiency."

 (defun LaTeX-indent-tabular ()
   "Return indent column for the current tabular-like line."
-  (cl-destructuring-bind
-      (beg-pos . beg-col)
-      (LaTeX-env-beginning-pos-col)
+  (let (beg-pos beg-col bol-col)
+    (save-excursion
+      (LaTeX-find-matching-begin)
+      (setq beg-pos (point))
+      (setq beg-col (current-column))
+      (LaTeX-back-to-indentation)
+      (setq bol-col (current-column)))
     (let ((tabular-like-end-regex
            (format "\\\\end{%s}"
                    (regexp-opt
@@ -8935,7 +8939,7 @@ COL for efficiency."
                             LaTeX-indent-environment-list)
                       out)))))
       (cond ((looking-at tabular-like-end-regex)
-             beg-col)
+             bol-col)

             ((looking-at "\\\\\\\\")
              (+ 2 beg-col))
#+end_src

Here I've inlined the call to ~LaTeX-env-beginning-pos-col~
(modification seemed inappropriate) and added the computation of
"bol-col", which is then used in place of "beg-col" when indenting the
inner \end{}.

Emacs  : GNU Emacs 30.0.50 (build 2, x86_64-apple-darwin21.6.0, NS
appkit-2113.60 Version 12.6 (Build 21G115))
 of 2023-08-11
Package: 13.2

current state:
==============
(setq
 AUCTeX-date "2023-08-28"
 window-system 'ns
 LaTeX-version "2e"
 TeX-style-path '("/usr/local/var/auctex"
"/Users/paulnelson/.emacs.d/elpaca/builds/auctex/auctex/style"
                  "/Users/paulnelson/.emacs.d/auctex/auto"
"/Users/paulnelson/.emacs.d/auctex/style" "auto" "style")
 TeX-auto-save t
 TeX-parse-self t
 TeX-master t
 TeX-command-list '(("TeX" "%(PDF)%(tex) %(file-line-error)
%`%(extraopts) %S%(PDFout)%(mode)%' %(output-dir) %t"
                     TeX-run-TeX nil (plain-tex-mode ams-tex-mode
texinfo-mode) :help "Run plain TeX")
                    ("LaTeX" "%`%l%(mode)%' %T" TeX-run-TeX nil
(latex-mode doctex-mode) :help "Run LaTeX")
                    ("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t"
TeX-run-compile nil (texinfo-mode) :help
                     "Run Makeinfo with Info output")
                    ("Makeinfo HTML" "makeinfo %(extraopts) %(o-dir)
--html %t" TeX-run-compile nil (texinfo-mode) :help
                     "Run Makeinfo with HTML output")
                    ("AmSTeX" "amstex %(PDFout) %`%(extraopts)
%S%(mode)%' %(output-dir) %t" TeX-run-TeX nil (ams-tex-mode)
                     :help "Run AMSTeX")
                    ("ConTeXt" "%(cntxcom) --once --texutil
%(extraopts) %(execopts)%t" TeX-run-TeX nil (context-mode)
                     :help "Run ConTeXt once")
                    ("ConTeXt Full" "%(cntxcom) %(extraopts)
%(execopts)%t" TeX-run-TeX nil (context-mode) :help
                     "Run ConTeXt until completion")
                    ("BibTeX" "bibtex %(O?aux)" TeX-run-BibTeX nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode context-mode) :help "Run BibTeX")
                    ("Biber" "biber %(output-dir) %s" TeX-run-Biber nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help "Run Biber")
                    ("Texindex" "texindex %s.??" TeX-run-command nil
(texinfo-mode) :help "Run Texindex")
                    ("Texi2dvi" "%(PDF)texi2dvi %t" TeX-run-command
nil (texinfo-mode) :help "Run Texi2dvi or Texi2pdf")
                    ("View" "%V" TeX-run-discard-or-function t t :help
"Run Viewer")
                    ("Print" "%p" TeX-run-command t t :help "Print the file")
                    ("Queue" "%q" TeX-run-background nil t :help "View
the printer queue" :visible TeX-queue-command)
                    ("File" "%(o?)dvips %d -o %f " TeX-run-dvips t
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help "Generate PostScript file")
                    ("Dvips" "%(o?)dvips %d -o %f " TeX-run-dvips nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Convert DVI file to PostScript")
                    ("Dvipdfmx" "dvipdfmx -o %(O?pdf) %d" TeX-run-dvipdfmx nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Convert DVI file to PDF with dvipdfmx")
                    ("Ps2pdf" "ps2pdf %f %(O?pdf)" TeX-run-ps2pdf nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Convert PostScript file to PDF")
                    ("Glossaries" "makeglossaries %(d-dir) %s"
TeX-run-command nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Run makeglossaries to create glossary file")
                    ("Index" "makeindex %(O?idx)" TeX-run-index nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Run makeindex to create index file")
                    ("upMendex" "upmendex %(O?idx)" TeX-run-index t
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Run upmendex to create index file")
                    ("Xindy" "texindy %s" TeX-run-command nil
                     (plain-tex-mode latex-mode doctex-mode
ams-tex-mode texinfo-mode) :help
                     "Run xindy to create index file")
                    ("Check" "lacheck %s" TeX-run-compile nil
(latex-mode) :help "Check LaTeX file for correctness")
                    ("ChkTeX" "chktex -v6 %s" TeX-run-compile nil
(latex-mode) :help "Check LaTeX file for common mistakes")
                    ("Spell" "(TeX-ispell-document \"\")"
TeX-run-function nil t :help "Spell-check the document")
                    ("Clean" "TeX-clean" TeX-run-function nil t :help
"Delete generated intermediate files")
                    ("Clean All" "(TeX-clean t)" TeX-run-function nil t :help
                     "Delete generated intermediate and output files")
                    ("Other" "" TeX-run-command t t :help "Run an
arbitrary command"))
 )