Subject: Re: bug#28382: 11.90.2.2017-07-25; Wrapping content in an equation with LaTeX-environment results in invalid LaTeX

From: Ikumi Keita <[email-masked]>
Date: Sun, 10 Sep 2017 03:20:07 +0900

Hello Thibaut,

>>>>> Thibaut Verron <[email-masked]> writes:
> If now you insert the equation first, select it, and use `C-c C-e` to
> wrap it in an "equation" environment, the result is

> \begin{equation}
> 2+2=4\label{eq:2}
> [point here]
> \end{equation}

Confirmed.  The reason for this behaviour is that `LaTeX-env-label' does
not take care of the case that the region is active.  In fact, it seems
that most of the functions to insert environments in latex.el don't,
either.

The attached quick hack basically gives the output you suggested as
option 2, though for `LaTeX-env-label' only.

I hope that anyone else makes out a comprehensive solution that covers
other functions as well...

Bye,
Ikumi Keita

--- latex.el~	2017-09-10 03:04:17.265116000 +0900
+++ latex.el	2017-09-10 03:04:51.798080000 +0900
@@ -1152,9 +1152,13 @@
 (defun LaTeX-env-label (environment)
   "Insert ENVIRONMENT and prompt for label."
   (LaTeX-insert-environment environment)
+  (if (TeX-active-mark)
+      (exchange-point-and-mark))
   (when (LaTeX-label environment 'environment)
     (LaTeX-newline)
-    (indent-according-to-mode)))
+    (indent-according-to-mode))
+  (if (TeX-active-mark)
+      (exchange-point-and-mark)))
 
 (defun LaTeX-env-list (environment)
   "Insert ENVIRONMENT and the first item."