I'm using AUCTeX and would like the delimiters \{ and \} to be matched. How can I (permanently) achieve matching of these delimiters?

The link I provided indicates that I should edit a syntax table, but I cannot determine which one and how, exactly, I am to edit it.

Why aren't the given delimiters automatically matched? Since they're so common in LaTeX, I'd think they would be. (I asked this last question on the AUCTeX mailing list, but received no reply.)

Based on Tyler's comment, my question is technically ill-founded. However, I suspect there's still a way to get done what I want done, so I'll leave the question as is under the assumption that the answerer will know what I'm really asking.

  • 1
    I don't think this is possible using the Emacs syntax-tables. By design, they match characters, such as '(' and ')', and you want to match two-character strings, '\{' and '\}'. I expect this is the reason this is not done automatically - matching characters is a built-in feature of Emacs, matching strings is not. – Tyler Sep 25 '12 at 20:22
  • @Tyler Thanks a lot for the helpful comment. I've edited my question accordingly. – Quinn Culver Sep 25 '12 at 20:32
  • I'm not sure what you're really asking. Matching, as per the Emacs manual, isn't possible, as I explained, so maybe you want to see the corresponding '\{' blink when you add the '\}'? I can probably do that, but maybe there's something more you're after? – Tyler Sep 25 '12 at 21:01
  • @Tyler I guess what I'm really asking depends on all of the consequences and properties of matching. I would, at least, like the blink. I cannot remember, off hand, if there are any other properties/consequences that I'd also like, but if you'd show me how to at least attain the blink, that would be swell. – Quinn Culver Sep 25 '12 at 21:05
  • @Tyler : I think there is more to why it's not done than simply "it's not a single character". To see what I mean, try enclosing a pair of backslashed braces inside a pair of regular braces, and notice how the braces are mismatched : first opening brace matches the first closing brace, at first sight. There is some black magic going on with the backslashes it seems. – T. Verron Sep 26 '12 at 10:53

I was also annoyed by this, so I defined a few macros to just put parens/braces/brackets around an argument.

\newcommand*{\braces}[1]{ \{ #1 \} }
\newcommand*{\parens}[1]{\left( #1 \right)}
\newcommand*{\brackets}[1]{ \left[ #1 \right] }

This turns something like: $ x = \{ stuff \}$ into $ x = \braces{stuff}$

I find it slightly more readable in my source, and emacs correctly matches the open/close braces (even if they are nested).

  • 1
    For \{ \} I agreed with this type of approach. Using mathtools you can define commands such as \DeclarePairedDelimiterX{\Set}[2]{\lbrace}{\rbrace}{#1\,\delimsize\vert\, #2} with all the control those commands provide over sizing of the brackets. – Andrew Swann Sep 27 '12 at 12:24
  • Thanks Felix, I think this might be the simplest solution. Why do you have the * after the \newcommand? – Quinn Culver Sep 28 '12 at 0:07
  • To be honest I had no idea, then I found this question. – Felix Sep 28 '12 at 19:29

Change the syntax class of the \ character for TeX mode buffers by adding the following form to your Emacs initialization file:

(add-hook 'TeX-mode-hook
      (lambda ()
        (modify-syntax-entry ?\\ ".")))

After evaluation of this form, \ acts as a punctuation character rather than an escape character. But beware of potential side effects!

  • I like that, but you're right to expect odd side effects! – Tyler Sep 26 '12 at 21:09
  • 1
    Would you please explain what this form is doing? – Quinn Culver Sep 27 '12 at 0:49
  • @Quinn: Emacs uses syntax tables to figure out what role each character is playing (whitespace, word, punctuation, symbol etc.). This form tells Emacs that, when in TeX mode, treat '\' as punctuation, not an escape. If you don't do this, the '\' tells Emacs to 'escape' the following character, which is what prevents the { from getting matched. – Tyler Sep 27 '12 at 2:02
  • @Tyler Does that mean that \{ and \} won't actually be matched, but rather only the { and } will be matched? – Quinn Culver Sep 27 '12 at 11:22
  • 1
    @mhp In that case I will not accept your answer, since it's solution isn't really what I asked for. Good idea, though. If you could prove to (or at least convince) me that I cannot get what I want, I would accept that. – Quinn Culver Sep 27 '12 at 12:50

This functionality (for me at least, using Aquamacs 3.3), seems to be enabled by adding the following line to your emacs configuration file:

(add-hook 'LaTeX-mode-hook #'smartparens-mode)

provided of course that you have already installed the smartparens package (e.g. via MELPA). (See the instructions on the package's GitHub page.)

Note: If you also want to auto-balance \[ and \], then you may also need to add the following line before the aforementioned line in your .emacs config file:

(require 'smartparens-config)

also in accordance to the instructions. (See line 104 of the smartparens code here.)

So in total:

(require 'smartparens-config)
(add-hook 'LaTeX-mode-hook #'smartparens-mode)

This (adding both of the two lines) even allows one to auto-balance \left's and \right's (see lines 130-145 here) if, inside of a $ ... $ or \[ ... \] pair you type:

  • \l( auto-completes/balances to \left( \right)
  • \l[ auto-completes/balances to \left[ \right]
  • \left\{ auto-completes/balances to \left\{ \right\}
  • \left| auto-completes/balances to \left| \right|

There are also supposed to be more such auto-completions/balancing available for LaTeX using smartparens (see the lines after 145 there), but I haven't been able to get them to work on my setup, so I have no advice for how to implement them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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