4

I found the reference to "\\" on line 283 of font-latex.el of auctex-11.86, and I'd like to remove it as a warning (which is easy enough to delete manually). I have not figured out a way to define certain special keywords so that font-lock knows that I am referring to a whole-word, rather than a potential sub-word. For example, if I define the word not as a keyword and then type the word nothing, the first part of nothing gets higlighted. The same thing happens with slashes and so forth.

Set forth below is a list of special character keywords that I'd like to please define as whole-words so that I can classify them as different colors using variations of the method described below. Some of the following would be defined as a warning color (i.e., because they are missing the backslash). When corrected within the *.tex document, the symbol with the backslash would be a different color -- e.g., # could be red, and \# could be blue. For that to work, there would two variations of the sample code below, each defined with a different set of colors.

"
    ~
\\
~\\
\S
#
\#
$
\$
%
\%
&
\&
not
(defvar lawlist-face-red (make-face 'lawlist-face-red))
(set-face-attribute 'lawlist-face-red nil :background "white" :foreground "red" :bold t)
(font-lock-add-keywords 'latex-mode '(
("
"
\\|
#
\\|
$
\\|
%
\\|
&
" 0 lawlist-face-red prepend)

(defvar lawlist-face-blue (make-face 'lawlist-face-blue))
(set-face-attribute 'lawlist-face-blue nil :background "white" :foreground "blue" :bold t)
(font-lock-add-keywords 'latex-mode '(
("
\\\\
\\|
~
\\|
\\\\\\\\
\\|
~\\\\\\\\
\\|
\\\\S
\\|
\\\\#
\\|
\\\\$
\\|
\\\\%
\\|
\\\\&
\\|
not
" 0 lawlist-face-blue prepend)

EDIT

Here is a working draft that incorporates the the answer of A.Ellett. AUCTeX already has a predefined warning mechanism for $, &, and \\. % is a comment without the backslash, which is already colorized. I've overridden the \\ and # with the code below -- the Emacs built-in warning for # is italics. AUCTeX has auto-correct for quotations when typing them in latex-mode, but I frequently copy / paste data containing improper LaTeX quotations and I needed a warning mechanism such as is outlined below:

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :background "white" :foreground "orange" :bold t :underline nil :font "Courier" :height 200)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :background "white" :foreground "cyan" :bold t :underline nil :font "Courier" :height 200)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :background "white" :foreground "blue" :bold t :underline nil :font "Courier" :height 200)

(font-lock-add-keywords 'latex-mode '(

("#\\|\"\\|~" 0 lawlist-face-a t)

("\\(\\\\\\)[^a-zA-Z@]" 1 lawlist-face-a t)

("\\\\\\\\" 0 lawlist-face-b t)

("\\bnot\\b\\|~\\\\\\\\\\|\\\\#\\|\\\\\\$\\|\\\\S\\|\\\\%\\|\\\\&" 0 lawlist-face-c t)

) 'prepend)
  • Hmmm. I'm not sure this is the best forum for answering this question. This is more about getting the correct regular expression match for the expressions you're trying to highlight. Since there's not natural way to define a word boundary around not alphanumeric symbols (ie., you can't use "\b" to match word boundaries), this is a bit non-trivial. – A.Ellett May 14 '13 at 17:47
  • Thank you very much for your help. I've posted a working draft below the original question that incorporates your solution, and I'll keep my eyes open for a better way to handle the special symbols. – lawlist May 14 '13 at 22:20
3

If you want emacs to only match on a whole word, you need to write something like

\bnot\b

In terms of defining this for font-locking, you may want to try something along the lines of

"\\bnot\\b"

The following macro can show you how this will match only "not" as a word.

(defun example-search ()
  (interactive)
  (re-search-forward "\\bnot\\b"))

If you've turned on hi-lock mode, this next function can help:

(defun example-hi-lock ()
  (interactive)
  (highlight-regexp "\\bnot\\b" 'hi-lock-word-not))

though you'll have to define the face hi-lock-word-not.

Note The syntax \b only matches at word boundaries. So if you try to use it with nonword characters, you're not likely to be able to match the expressions you're interested in.

  • That worked . . . thank you .. I'm going to use that example to try and define the other special characters with slashes and so forth and I'll report back with the results . . . – lawlist May 14 '13 at 18:08
  • I'm adding the link to the other related post describing the procedure you have outlined for using hi-lock-mode for my future reference and anyone else interested: tex.stackexchange.com/questions/113970/… – lawlist May 14 '13 at 18:16
  • Thank you for helping me to understand how whole-words are differentiated from sub-words -- greatly appreciated !!! – lawlist May 14 '13 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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