简体   繁体   English

Emacs Auctex自定义语法高亮

[英]Emacs Auctex custom syntax highlight

I'd like to highlight a new command I created in LaTeX: 我想强调一下我在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: 我按照手册将这段代码放在我的~/.emacs ,但它不起作用:

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

What's wrong? 怎么了?

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: 编辑:Deokhwan Kim最初指出你的正则表达式包含两个连续的双引号,并且右括号)需要用双引号进行转义:

(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: 除了Deokhwan Kim指出的观点之外,还有以下两个问题:

  • You need four backslashs instead of two in front of 'conceito': \\\\\\\\conceito 在'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. 反斜杠序列\\\\<仅在单词的开头匹配空字符串,但是,新LaTeX命令开头的反斜杠不被视为单词的一部分,因此\\\\<将不匹配。

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. 编辑:Deokhwan Kim提出的另一个好观察是,在这种特殊情况下,你根本不需要括号,因为你无论如何都试图匹配整个表达式。 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". 关于括号的观点是正确的,但实际上你可以扩展你的正则表达式只匹配一个开口大括号{跟随单词“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. 请注意,由于我们正在测试一个{紧跟在“conceito”之后(除非两者之间有空格),我们根本不需要对\\\\>进行测试。

In general, try Mx 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. 通常,尝试使用Mx重构器以交互方式创建正则表达式:您可以在小缓冲区中编辑新的正则表达式,并立即查看缓冲区中突出显示的内容,从中调用重构器。

GNU AUCTeX has a built-in way of defining custom highlighting to user-defined macros. GNU AUCTeX具有为用户定义的宏定义自定义突出显示的内置方法。 Have a look at the variable font-latex-user-keyword-classes and the AUCTeX documentation . 看看变量font-latex-user-keyword-classesAUCTeX文档

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. 这将显示昏暗阴影面中\\ hide {},\\ mycomment {}和\\ comment {}宏的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM