Emacs Auctex custom syntax highlight
Asked Answered
S

2

6

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?

Scallion answered 7/8, 2012 at 11:26 Comment(0)
A
6

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.

Aria answered 7/8, 2012 at 14:40 Comment(4)
For the record, in my deleted post, I pointed out some mistakes, such as consecutive double quotes "" and missing \\ before ), in the regular expression in the question. But Thomas notified me that other problems still lurked there and that my solution was not enough. So I've delete my post to avoid any complications.Armoury
Nice work! One more suggestion is that, in the last example, \\s-* had better not be there. In LaTeX, an argument should immediately follow its macro without any space between them, shouldn't it?Armoury
@Deokhwan Kim - No, whitespace is fine between the command and the {.Aria
Oops. I've misunderstood it so far. Thanks again!Armoury
F
3

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.

Farthing answered 28/1, 2014 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.