Hi all,

To reproduce, put this in a LaTeX-mode buffer:

--8<---------------cut here---------------start------------->8---
% \begin{split}
%   a &= b.
% \end{split}
--8<---------------cut here---------------end--------------->8---

Make sure LaTeX-syntactic-comments is set to t (the default).

There are two related issues:

1. Run indent-region on the second line.  This inserts two spaces before
"%", when it shouldn't.

2. Then, run indent-region on the third line.  This gives the error:

"Can’t locate beginning of current environment"

The issue is that LaTeX-indent-calculate uses the indent logic for
special environments even when computing the outer indentation for
comments (i.e., what comes before the "%"), where it shouldn't apply.

With the attached patch, it skips that logic in such cases.

Any feedback welcome.  I'll install if there are no objections.

Thanks, best,

Paul

>From 036e8fa592e5e4bb3de7146bbf1838b9c3c829f1 Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email protected]>
Date: Thu, 25 Jun 2026 14:45:36 +0200
Subject: [PATCH] Fix indentation of commented special environments

On a commented line, indentation for special environments should
apply only to the inner content.  Otherwise, commented
environments such as 'split' are indented incorrectly.

* latex.el (LaTeX-indent-calculate): Skip special environment
indentation when computing the outer indentation of a commented
line.
* tests/latex/math-indent-in.tex:
* tests/latex/math-indent-out.tex: New test cases.
---
 latex.el                        |  4 ++++
 tests/latex/math-indent-in.tex  | 10 ++++++++++
 tests/latex/math-indent-out.tex | 10 ++++++++++
 3 files changed, 24 insertions(+)

diff --git a/latex.el b/latex.el
index cc24b3e0..bbf74a3f 100644
--- a/latex.el
+++ b/latex.el
@@ -4553,6 +4553,10 @@ outer indentation in case of a commented line.  The symbols
              ;; empty line appears in LaTeX's output.
              0)
             ((and LaTeX-indent-environment-check
+                  ;; Special indentation should apply only to the inner
+                  ;; part of comments.
+                  (not (and (eq force-type 'outer)
+                            (TeX-in-commented-line)))
                   ;; Special environments.
                   (let ((entry (assoc (or LaTeX-current-environment
                                           (LaTeX-current-environment))
diff --git a/tests/latex/math-indent-in.tex b/tests/latex/math-indent-in.tex
index 16acac60..c1f9fd46 100644
--- a/tests/latex/math-indent-in.tex
+++ b/tests/latex/math-indent-in.tex
@@ -15,3 +15,13 @@ y &= z
 x
 \]
 \end{itemize}
+
+% \begin{split}
+%   a &= b.
+% \end{split}
+
+% \begin{equation*}
+%   \begin{split}
+%     a &= b.
+%   \end{split}
+% \end{equation*}
diff --git a/tests/latex/math-indent-out.tex b/tests/latex/math-indent-out.tex
index e895be28..6996d43d 100644
--- a/tests/latex/math-indent-out.tex
+++ b/tests/latex/math-indent-out.tex
@@ -15,3 +15,13 @@
     x
   \]
 \end{itemize}
+
+% \begin{split}
+%   a &= b.
+% \end{split}
+
+% \begin{equation*}
+%   \begin{split}
+%     a &= b.
+%   \end{split}
+% \end{equation*}
-- 
2.54.0

_______________________________________________
bug-auctex mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-auctex

Reply via email to