5

font-lock-keyword-face is used by AUCTeX for all keywords, and I have not been able to find a way to create a new keyword that is a different color.

I have defined the foreground of font-lock-keyword-face to be the color red (with a white background), which affects every defined keyword.

I would like to create my own keyword that stands out from all the rest -- e.g., :foreground "yellow" :background "black".

\insert-data

It looks like there are only three (3) major groups available:

  1. keywords (controlled by font-lock-keyword-face);

  2. warnings (controlled by font-latex-warning-face); and

  3. undefined-keywords (controlled by font-latex-sedate-face).

All the examples on the internet appear to focus on changing the color inside square / wavy brackets using the variable font-latex-user-keyword-classes. I have not found any working examples where a new keyword group (with different colors) is created.

NONE of the following examples create a fourth (4th) major group:

;; \EFFECT{[font-lock-type-face]}
(setq font-latex-match-textual-keywords
    '(
        ("parentext" "{")
        ("hybridblockquote" "[{")
    )
)

;; \EFFECT{[font-lock-variable-name-face]}
(setq font-latex-match-variable-keywords
    '(
        ("setlist" "[{")
        ("setlist*" "[{")
    )
)

;; \EFFECT{[font-lock-constant-face]}
(setq font-latex-match-reference-keywords
    '(
        ("citet" "[{")
        ("degrees" "")
        ("units" "{")
    )
 )

;; \font-latex-warning-face
(setq font-latex-match-warning-keywords
    '(
        ("fixme" "{") 
        ("INSERT" "")
    )
)

;; only affects inside wavy square brackets
(setq font-latex-user-keyword-classes
          '(("my-warning-commands"
                (("fixme" "{"))
                (:foreground "purple")
                command))) 
1
  • I've found getting font-lock to change the way you want to be riddled with frustration. I've had successes, but I wouldn't really know what to tell you. However, there is hi-lock-mode you might consider looking at that. It's not as robust as font-lock and can occasionally get confused. But overall, it works fairly easily. – A.Ellett May 13 '13 at 0:51
6

Here is an example of setting somethings up for hi-lock mode.

First define a face:

(defface af-bold-yellow-box '((t  (:background  "yellow" 
                                   :foreground  "black"               
                                   :bold t
                                  )))  "yellow-box-face")

Then you can define a macro to apply your faces to specified regular expressions.

(defun z-hi-lock-quizzes ()
  ;; this next line is necessary to correct sloppy hi-locking
  (if (not hi-lock-mode) 
      (progn (hi-lock-mode -1) 
             (hi-lock-mode  1)) 
    (hi-lock-mode) 
    (hi-lock-mode))
  (highlight-regexp "^%-\\*-mode:LaTeX.*$" (quote hi-conceal-content));
  (highlight-regexp "^%-@-(.+$"            (quote hi-lock-page-break));
)

I define various macros to address highlighting different features. And then I write an interactive macro which assembles and applies these.

(defun ae-hi-lock-features ()
   (interactive)
   (z-hi-lock-quizzes)
   ... call other functions ...
)

FYI I believe all the various faces I use here are faces I've defined. Even in the examples below. But faces are defined as I've illustrated above.


adding to font-lock features

Here's some code that I've used to add elements to font-lock features. But, it's been so long since I wrote this, I don't really know if the following is sufficient to get all the desired results

(setq ae-keywords:tex-mode:list 
      (list  '( comment          "^\\s *\\(%.+$\\)"                       'hi-pink                            )
             '( newpara          "\\(^%-=-.+$\\)"                         'hi-skyblue-boldred-border          )
             '( newsect          "\\(^%-\\#-.+$\\)"                       'hi-goldback-boldbluefront-border   )
             '( newpage          "\\(^%-@-(.+$\\)"                        'hi-lock-page-break                 )
             '( tab              "\\(\t\\)"                               'hi-lightblue-red-text-border       )
             '( begin.doc        "\\(\\\\\\(begin\\|end\\\){document}\\)" 'hi-goldback-boldbluefront-border)
             '( comment.b        "\\(%\\.\\.%.*$\\)"                      'hi-darkblue-lightblue-text)
             '( multiplechoice.a "\\(^\\[o\\] .+$\\)"                     'hi-light-multiple-choices)
             '( multiplechoice.b "\\(^\\[[\\?\\.\\ ]\\] .+$\\)"                   'hi-goldback-boldbluefront-border)
             '( multiplechoice.c "\\(^\\[x\\] .+$\\)"                     'hi-light-multiple-choice-answer)
             ))
(dolist (elt ae-keywords:tex-mode:list)
  (font-lock-add-keywords nil (list (list (nth 1 elt) 1 (nth 2 elt) 'append)))
  )

If the above seem too elaborate, you can do something a bit more direct as in

  (font-lock-add-keywords nil '(("\\(hello\\) \\(World\\)" 
                                2 
                                'hi-goldback-boldbluefront-border)))

EDIT

I think what you probably want to do is something more like

  (font-lock-add-keywords 'latex-mode '(("<regex>" 
                                          <level-of-subexpression>
                                          '<face-name>)))

You probably want to set <level-of-subexpression> to 0, but using subexpressions you can get nice effects as in my hello World example.

3
  • Thank you so very much for the detailed answer -- I'll try it out later this evening when I return home and I'll report back with the results . . . – lawlist May 13 '13 at 1:27
  • 2
    Your first solution worked perfectly straight out of the box. The second solution needs a little something extra to work, perhaps similar to this link: tex.stackexchange.com/questions/81680/… , which I found based upon your usage of font-lock-add-keywords. I hadn't seen either solution prior to your post. I will work on adapting your second solution (with a cup of coffee) in the next day or so -- the link I found based on your usage does work also. Thank you so very much -- I spent all day working on this and you resolved the issue !!!! :) – lawlist May 13 '13 at 5:29
  • I am using the hello world example, but I haven't been able to figure out a way to define a whole-word that will not be treated as a sub-word. If we type the word helloo, the goal would be for the entire word to not be highlighted at all -- instead of helloo. Here is a new thread I opened on that specific issue if by any chance you have a suggestion: tex.stackexchange.com/questions/114133/… – lawlist May 14 '13 at 15:43

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.