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

From: Uwe Brauer <[email-masked]>
Date: Sun, 03 Sep 2023 17:30:57 +0200

>>> "PN" == Paul Nelson <[email-masked]> writes:
Hi Paul

> Hi Ikumi and Uwe,
> Thanks to both of you for your feedback.

Thanks for your proposal

> I agree that it's better to have begin/end line up when all else is
> equal, but with the current state of AUCTeX (since this bug was
> introduced), it is a severe user error to put \begin{array} on a line
> following non-whitespace.  The reason is the same as in my original
> message, or in what one gets from Uwe's example by deleting the inner
> aligned environment and applying M-x indent-region:

> #+begin_src latex
> \begin{equation}
>   n u m=\left[\begin{array}{ll}
>                 2 & 25
>               \end{array}\right]
>           \end{equation}
> #+end_src

I am a bit confused. So let me try to sort it out.

You say indent-region

I am referring here to the LaTeX-filling-functions, such as 
 LaTeX-fill-environment,  LaTeX-fill-section etc

Now I presume we all agree that 
the correct form of the above example should be

\begin{equation}
   n u m=\left[
      \begin{array}{ll}
                 2 & 25
      \end{array}
\right]
\end{equation}

Then 

 LaTeX-fill-environment 
results in 

 \begin{equation}
   n u m=\left[
     \begin{array}{ll}
       2 & 25
     \end{array}
   \right]
 \end{equation}

> In Uwe's criticism, the user already messed up (with the current state
> of AUCTeX) by forgetting to put a newline before \begin{array}.  Their
> mistake is covered up because they were lucky enough to enclose the
> array environment in an aligned environment, but this does not forgive
> the mistake, and I'm not convinced by this example that AUCTeX's
> current approach is superior to the proposed one.  

So are you saying that your proposal in my example would result in a
better result? If so I disagree.

My ranking of the
> available alternatives:

> 1. Some better proposal

Since I receive a *lot* of theses badly (in the sense above)
environments in the incorrect form
\begin{equation}
   n u m=\left[\begin{array}{ll}
                 2 & 25
               \end{array}\right]
\end{equation}

So I came up with a solution heavily rewritten and improved by Arash

(defun my-change-begin-array-to-newline-begin-array (); important function before running filling functions
  (interactive)
    (while (re-search-forward "\\\\begin{array}\\(\\(.\\|\n\\)*?\\)" nil t)
      (replace-match "\n\\\\begin{array}\\1" nil nil)))

(defun my-change-end-array-to-newline-end-array (); important function before running filling functions
  (interactive)
  (save-excursion
    (while (re-search-forward"\\\\end{array}\\(\\(.\\|\n\\)*?\\)" nil t)
      (replace-match "\\\\end{array}\n\\1" nil nil))))

However it should 

> 2. My proposal
> 3. Current state

> To emphasize, the user error is severe because the resulting
> indentation errors propagate through the buffer, making indent-region
> useless when applied globally. 

Frankly in my opinion, one should *not* use this function or
LaTeX-fill-buffer
globally.

Unfortunately because of bad practise, or laziness a lot of users or
their editors do not provide very well formatted environments.

>  On the other hand, this user error is
> committed repeatedly in every .tex file I encounter in practice.
> Moreover, the underlying bug cannot always be circumvented just by
> putting each \begin at the start of its own line, as the following
> example shows:

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

Frankly one should not use $ in LateX file and in this example, from my
point of view, since there is already a \begin{pmatrix}

The correct syntaxes should be

\begin{math}
  \begin{pmatrix}
    a & b \\
    c & d
  \end{pmatrix}
\end{math}

> The "user workaround" here is to put the entire pmatrix environment on
> one line.  I've been using and automating such workarounds for years
> -- writing my own linters that "fix" a tex buffer to avoid such "user
> errors" -- but don't think new users should be forced to do so, and
> can emphasize with some of Robert's comments in Ikumi's reference [1].

That seems to me much less readable than my solution.

> As a final remark, one can fix my original example by adjusting the
> "equation" entry of LaTeX-indent-environment-list to
> LaTeX-indent-tabular, but not this last example.

What's about a new variable then.
 
(defvar LaTeX-fill-sloppy t
  "When t, Paul's proposal, when nil, Uwe's proposal, that is the old behavior")

Or does this make the code more difficult to maintain?

Uwe 

-- 
Warning: Content may be disturbing to some audiences
I strongly condemn Putin's war of aggression against the Ukraine.
I support to deliver weapons to Ukraine's military. 
I support the NATO membership of the Ukraine.
I support the EU membership of the Ukraine. 
https://addons.thunderbird.net/en-US/thunderbird/addon/gmail-conversation-view/