Subject: bug#65648: 13.2; indentation of nested environments

From: Ikumi Keita <[email-masked]>
Date: Sat, 09 Sep 2023 15:52:42 +0900

Hi Paul and Uwe,

>>>>> Paul Nelson <[email-masked]> writes:
> As for the "always-align t" case, there are still some natural "broken"
> examples, as you know:
> #+begin_src latex
> \begin{equation*}
>   g = \begin{pmatrix}
>         a & b \\
>         c & d \\
>       \end{pmatrix},
>       \quad
>       h = \begin{pmatrix}
>             x & y \\
>             z & w \\
>           \end{pmatrix}.
> \end{equation*}
> #+end_src
> #+begin_src latex
> The matrix $\begin{pmatrix}
>               a & b \\
>               c & d
>             \end{pmatrix}$ has determinant
>             \begin{equation*}
>               a d - b c.
>             \end{equation*}

>             The matrix $\begin{pmatrix}
>                           a & b \\
>                           c & d
>                         \end{pmatrix}$ has determinant
>                         \begin{equation*}
>                           a d - b c.
>                         \end{equation*}

>                         The matrix $\begin{pmatrix}
>                                       a & b \\
>                                       c & d
>                                     \end{pmatrix}$ has determinant
>                                     \begin{equation*}
>                                       a d - b c.
>                                     \end{equation*}
> #+end_src

I've been gradually inclined to the attached approach. Namely, align "&"
as well as \end{foo} relative to beg-col, not beg-pos. In this way, the
above examples turn into
----------------------------------------------------------------------
\begin{equation*}
  g = \begin{pmatrix}
    a & b \\
    c & d \\
  \end{pmatrix},
  \quad
  h = \begin{pmatrix}
    x & y \\
    z & w \\
  \end{pmatrix}.
\end{equation*}
----------------------------------------------------------------------
and
----------------------------------------------------------------------
The matrix $\begin{pmatrix}
  a & b \\
  c & d
\end{pmatrix}$ has determinant
\begin{equation*}
  a d - b c.
\end{equation*}

The matrix $\begin{pmatrix}
  a & b \\
  c & d
\end{pmatrix}$ has determinant
\begin{equation*}
  a d - b c.
\end{equation*}

The matrix $\begin{pmatrix}
  a & b \\
  c & d
\end{pmatrix}$ has determinant
\begin{equation*}
  a d - b c.
\end{equation*}
----------------------------------------------------------------------

This patch indents as
\begin{equation}
  n u m=\left[\begin{array}{ll}
    2 & 25
  \end{array}\right]
\end{equation}
, not the form Paul proposed
\begin{equation}
  n u m=\left[\begin{array}{ll}
                2 & 25
  \end{array}\right]
\end{equation}
. Maybe Uwe doesn't like the former indentation, but it is more similar
to the behavior of other (non-tabular type) environments.

> With a bit more massaging, it should be possible to avoid these.  I had in
> mind modifying LaTeX-indent-level-count (or the block surrounding where it
> is called) in \end{TABULAR} cases, adjusting by the difference between the
> matching \begin and its beginning of line.

A similar idea crossed my brain, too. The best position to modify would be
`LaTeX-indent-calculate-last'.
It would make the indent algorithm more complex, so I began to search
for more simple solution.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine

>From 5c87b9e72ceddf3459abe8f59e2c6b61316d71d8 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email-masked]>
Date: Fri, 8 Sep 2023 17:13:07 +0900
Subject: [PATCH] temporal commit

---
 latex.el | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/latex.el b/latex.el
index d855b5c5..9a5b753e 100644
--- a/latex.el
+++ b/latex.el
@@ -8884,6 +8884,12 @@ function would return non-nil and `(match-string 1)' would return
     (LaTeX-find-matching-begin)
     (cons (point) (current-column))))
 
+(defun LaTeX-env-beginning-pos-indent ()
+  "Return a cons: (POINT . INDENT) for current environment's beginning."
+  (save-excursion
+    (LaTeX-backward-up-environment)
+    (cons (point) (LaTeX-current-indentation))))
+
 (defun LaTeX-hanging-ampersand-position (&optional pos col)
   "Return indent column for a hanging ampersand (that is, ^\\s-*&).
 When you know the position and column of the beginning of the
@@ -8893,7 +8899,7 @@ COL for efficiency."
       (beg-pos . beg-col)
       (if pos
           (cons pos col)
-        (LaTeX-env-beginning-pos-col))
+        (LaTeX-env-beginning-pos-indent))
     (let ((cur-pos (point)))
       (save-excursion
         (if (and (search-backward "\\\\" beg-pos t)
@@ -8924,7 +8930,7 @@ COL for efficiency."
   "Return indent column for the current tabular-like line."
   (cl-destructuring-bind
       (beg-pos . beg-col)
-      (LaTeX-env-beginning-pos-col)
+      (LaTeX-env-beginning-pos-indent)
     (let ((tabular-like-end-regex
            (format "\\\\end{%s}"
                    (regexp-opt
-- 
2.41.0