auctex-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: indentation problem II


From: Arash Esbati
Subject: Re: indentation problem II
Date: Thu, 21 Apr 2022 13:59:46 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50

Uwe Brauer <oub@mat.ucm.es> writes:

>>>> "IK" == Ikumi Keita <ikumi@ikumi.que.jp> writes:
>
>> Hmm, it would basically be useful, but I think automatic insertion of
>> ampersands at the first line should be suppressed because matrix-like
>> environments are used when writing column-vector like
>> \begin{pmatrix}
>>   1 \\
>>   2 \\
>>   3
>> \end{pmatrix}
>> , without "&".
>
> Right, the problem is, you can also have matrix like structures and 
> pmatrix does not take arguments, while array does.
>
> So there is no way of knowing whether a matrix or a single column vector
> will be generated.
>
> Best course would be
>
>     1. You insert the first row which might be
>        1 & 4 &
>
>     2. And then M-ret, based on an analysis of that line inserts the
>        needed number of &,

Thanks for your responses.  Indeed, there is a big chance that inserting
a single & doesn't match the user expectation.  This makes the change
even smaller:

--8<---------------cut here---------------start------------->8---
diff --git a/style/amsmath.el b/style/amsmath.el
index 5e7812e7..f72f8be8 100644
--- a/style/amsmath.el
+++ b/style/amsmath.el
@@ -57,7 +57,8 @@
     "align*" "gather*" "flalign*" "multline*" "equation*"
     "split"
     "cases"
-    "matrix" "smallmatrix" "pmatrix" "bmatrix" "Bmatrix" "vmatrix" "Vmatrix"
+    "matrix" "smallmatrix"
+    "bmatrix" "Bmatrix" "pmatrix" "vmatrix" "Vmatrix"
     "subequations"
     '("subarray" "Alignment"))

@@ -126,13 +127,13 @@
                    ("alignedat" . LaTeX-item-equation-alignat)
                    ("flalign"  . LaTeX-item-equation)
                    ("flalign*" . LaTeX-item-equation)
-                   ("matrix" .  LaTeX-item-equation)
-                   ("pmatrix" .  LaTeX-item-equation)
-                   ("bmatrix" .  LaTeX-item-equation)
-                   ("Bmatrix" .  LaTeX-item-equation)
-                   ("vmatrix" .  LaTeX-item-equation)
-                   ("Vmatrix" .  LaTeX-item-equation)
-                   ("smallmatrix" . LaTeX-item-equation)
+                   ("matrix" .  LaTeX-item-equation-matrix)
+                   ("pmatrix" .  LaTeX-item-equation-matrix)
+                   ("bmatrix" .  LaTeX-item-equation-matrix)
+                   ("Bmatrix" .  LaTeX-item-equation-matrix)
+                   ("vmatrix" .  LaTeX-item-equation-matrix)
+                   ("Vmatrix" .  LaTeX-item-equation-matrix)
+                   ("smallmatrix" . LaTeX-item-equation-matrix)
                    ("subarray" . LaTeX-item-equation)
                    ("cases"    . LaTeX-item-equation))
                  LaTeX-item-list))
@@ -225,6 +226,31 @@ If SUPPRESS is non-nil, do not insert line break macro."
       (LaTeX-newline)
       (indent-according-to-mode))))

+(defun LaTeX-item-equation-matrix ()
+  "Insert contents to terminate a line in matrix environments.
+Put line break macro on the last line and insert suitable number
+of ampersands if possible.  This number is extracted from the
+first line of the environment."
+  (end-of-line 0)
+  (just-one-space)
+  (TeX-insert-macro "\\")
+  (forward-line 1)
+  (indent-according-to-mode)
+  (let* ((p (point))
+         (s (save-excursion
+              (LaTeX-find-matching-begin)
+              (point)))
+         (e (save-excursion
+              (goto-char s)
+              (search-forward "\\\\" p t)))
+         n)
+    (save-excursion
+      (goto-char s)
+      (setq n (how-many "[^\\]&" s e)))
+    (when (and n (> n 0))
+      (save-excursion
+        (insert (make-string n ?&))))))
+
 (defun LaTeX-item-equation-alignat (&optional suppress)
   "Insert contents to terminate a line in multi-line equations environment.
 Put line break macro on the last line.  Next, if the current
--8<---------------cut here---------------end--------------->8---

> But that sounds quite complicated.

Can you please elaborate what sounds complicated?

Best, Arash



reply via email to

[Prev in Thread] Current Thread [Next in Thread]