Emacs Auctex custom syntax highlight

一笑奈何 提交于 2019-12-09 18:24:20

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/11844936/emacs-auctex-custom-syntax-highlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!