Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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
show 4 more comments

2 Answers

up vote 6 down vote accepted

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).

share|improve this answer
    
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
add comment

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!

share|improve this answer
    
I like that, but you're right to expect odd side effects! –  Tyler Sep 26 '12 at 21:09
    
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
    
@QuinnCulver: Yes, { and \{ as well as } and \} are no longer distinguished in terms of parenthesis matching. Fontification of \{ and \} is not affected, though. –  mhp Sep 27 '12 at 12:41
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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