Highlighting latex code

Discussion of programming Lisps using Emacs, including SLIME and other tools
Post Reply
baranas
Posts: 6
Joined: Tue Oct 30, 2012 6:30 am

Highlighting latex code

Post by baranas » Sat Nov 03, 2012 8:01 am

I am using Emacs+Auctex. I need additional highlighting in math mode. How can I force Emacs to highlight braces and some commands (like \quad) inside math mode with different color?

stackman
Posts: 28
Joined: Sat Oct 06, 2012 5:44 am

Re: Highlighting latex code

Post by stackman » Sat Nov 03, 2012 10:42 am

For parens or braces highlighting, I suggest paren-mode or mic-paren-mode.
Highlighting specific words is done with font-lock-add-keywords.
The following snippet in your init file should color \quad in red in tex or latex buffers.

Code: Select all

(add-hook 'TeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
            '(("\\\\quad" 0 font-lock-warning-face t)))))

baranas
Posts: 6
Joined: Tue Oct 30, 2012 6:30 am

Re: Highlighting latex code

Post by baranas » Tue Nov 06, 2012 12:14 pm

Thanks, seems that it works. First time I tried similar method it messed up conditional highlighting. Can you explain to me what means 0 and t.

baranas
Posts: 6
Joined: Tue Oct 30, 2012 6:30 am

Re: Highlighting latex code

Post by baranas » Wed Nov 07, 2012 5:01 am

Still it messes up conditional Auctex highlighting when i add more complicated highlighting expressions. One of the examples would be when i change 0 to nil. I still don't know what it means :) , but that is an example.

stackman
Posts: 28
Joined: Sat Oct 06, 2012 5:44 am

Re: Highlighting latex code

Post by stackman » Wed Nov 07, 2012 3:00 pm

The 0 means "highlight the whole match".
A positive number means "highlight the nth group in the regexp". For example,
the following code italicizes the word between braces in expressions of the form \begin{***}.

Code: Select all

(add-hook 'TeX-mode-hook
       (lambda () (font-lock-add-keywords nil
               '(("\\\\begin{\\([^}\\s-]*\\)}" 1 'italic t)))))
t means override any previous fontifications.
See the documentation of the functions font-lock-add-keywords and font-lock-keywords for more details.
There are some infos in the emacs manual about what is called "font lock". You can access the relevant part by evaluating (info "(emacs) Font Lock").

Post Reply