1

I am using AUCTeX for editing in LaTeX, with LaTeX-electric-left-right-brace enabled, so that when I press for example C-c C-m \left in a math environment, the right brace is also automatically put in my expression.

My problem is that this automatically inserts a newline after the left brace and before the right brace, so that it looks like this for example:

\left(

\right)

with the point inserted in the middle. But I don't want that. I couldn't find anything about it in the Info documentation. Could anyone please tell me how to disable this behavior? (i.e. have the same but on one single line)

Giuseppe
  • 455
  • 2
  • 14

2 Answers2

2

I'm afraid this behavior is hard-coded in the function TeX-arg-insert-braces and cannot be disabled (besides overwriting or better advising). As a quick fix, I suggest you take the current behavior, insert your math and hit M-q. It turns

$
\left(
  math and hit M-q
\right)$

into

$ \left( math and hit M-q \right)$
Arash Esbati
  • 1,795
  • 1
  • 8
  • 13
2

Thanks to arash-esbati's findings it is quite easy to deactivate LaTeX-newline locally within TeX-arg-insert-braces through advices.

Install the following code into your init file and TeX-arg-insert-braces does no longer generate newlines.

;; The following macro should actually be part of package `advice`:
(defmacro advise-flet (flist &rest body)
  "Apply local advices FLIST and execute BODY.
Each advice in FLIST should look like (FUN DEF)
where FUN is a function symbol and DEF is the
locally defined function."
  (declare (indent 1) (debug (sexp body)))
  (append
   `(unwind-protect
    (progn
      ,(cons 'progn
         (cl-loop
          for f in flist
          collect `(advice-add ,(list 'quote (car f)) :override ,(cadr f))
          ))
      ,@body))
   (cl-loop
    for f in flist
    collect `(advice-remove ,(list 'quote (car f)) ,(cadr f))
    )))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Begin of customization code:

(defun LaTeX-without-newline (oldfun &rest args)
  "`flet' `LaTeX-newline' to `ignore' and run `TeX-arg-insert-braces'."
  (advise-flet
      ((LaTeX-newline #'ignore))
    (apply oldfun args)))

(advice-add #'TeX-arg-insert-braces :around #'LaTeX-without-newline)
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • Thanks a lot. As I understand what is written here (https://www.emacswiki.org/emacs/AdviceVsHooks) about "advising", it is not advised :). What about it in the present case? And how did you get to this solution? – Giuseppe Jan 25 '18 at 16:00
  • @Giuseppe The situation is quite simple. If you are one of the AucTeX developers you can change `TeX-arg-insert-brace` directly and you don't need the advice. If you aren't an author of AucTeX you can clone the repository change the code and make a pull-request. If you are lucky the request is accepted in some time. But, if you want to get the job done as soon as possible and you don't want to change the source the advice is the only choice for you since the newlines are hardcoded as @arash-esbati mentioned in his answer. – Tobias Jan 25 '18 at 19:16
  • The problem with this is that sometimes this erases the previous blank line, if there is any. That being said, the default behaviour shouldn't add any lines, idk why the developers thought it was a good idea to add blanklines to ``\left`` and ``\right``. – Zero Aug 15 '23 at 00:01