Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Auctex custom syntax highlight

I'd like to highlight a new command I created in LaTeX:

\newcommand{\conceito}[3]{
  \subsection{#1} (Original: \textit{#2} #3).
}

I use this code in this way:

\conceito{Foo}{Bar}{Bla}

I followed the manual and put this code in my ~/.emacs, but it didn't work:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
          '((""\\<\\(\\conceito)\\>"" 1 font-lock-warning-face t)))))

What's wrong?

like image 243
msampaio Avatar asked Aug 07 '12 11:08

msampaio


2 Answers

EDIT: Deokhwan Kim originally pointed out that your regexp contains two consecutive double quotes, and that the closing parenthesis ) needs to be escaped with double quotes as well:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
          '(("\\<\\(\\conceito\\)\\>" 1 font-lock-warning-face t)))))

In addition to the points pointed out by Deokhwan Kim, there are also the following two issues:

  • You need four backslashs instead of two in front of 'conceito': \\\\conceito

  • The backslash sequence \\< matches the empty string only at the beginning of a word, however, the backslash at the beginning of your new LaTeX command is not considered part of a word, so \\< will not match.

Try this instead:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
            '(("\\(\\\\conceito\\)\\>" 1 font-lock-warning-face t)))

EDIT: Another good observation that Deokhwan Kim made is that in this particular case, you don't really need the parenthesis at all, because you're attempting to match the whole expression anyway. So an alternative to the last line could be:

'(("\\\\conceito\\>" 0 font-lock-warning-face t)))))

The point about the parenthesis is correct, but you could actually extend your regexp to only match when an opening curly brace { follows the word "conceito". But since you don't really want to highlight that brace, using sub-groups defined by parentheses is the way to go:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
            '(("\\(\\\\conceito\\)\\s-*{" 1 font-lock-warning-face t)))

Note that since we're testing for a { that follows directly after "conceito" (unless there's whitespace in between), we don't need the test for \\> any more at all.

In general, try M-x re-builder to craft regular expression interactively: you can edit a new regexp in a small buffer and instantly see what is highlighted in the buffer from which you invoked the re-builder.

like image 185
Thomas Avatar answered Oct 18 '22 20:10

Thomas


GNU AUCTeX has a built-in way of defining custom highlighting to user-defined macros. Have a look at the variable font-latex-user-keyword-classes and the AUCTeX documentation.

Here's a simple example (my configuration):

(setq font-latex-user-keyword-classes
      '(("shadow-hidden"    (("hide" "{"))      shadow command)
        ("shadow-mycomment" (("mycomment" "{")) shadow command)
        ("shadow-comment"   (("comment" "{"))   shadow command)))

This will show the contents of \hide{}, \mycomment{}, and \comment{} macros in the dim shadow face.

like image 3
Tassilo Horn Avatar answered Oct 18 '22 20:10

Tassilo Horn