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)

2 Answers 2

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)
3
  • Thanks a lot. As I understand what is written here (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
    Commented Jan 25, 2018 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
    Commented Jan 25, 2018 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
    Commented Aug 15, 2023 at 0:01
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)$
2
  • This is a nice analysis and a nice workaround (+1). It helped me with my answer.
    – Tobias
    Commented Jan 25, 2018 at 19:18
  • @Tobias - Sure, you're welcome. Nice answer from your side as well (+1). Commented Feb 3, 2018 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.