TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, I want to highlight the theorem and lemma environments, but leave the proof environment unchanged. I tried to search the variables in font locking but no luck.

share|improve this question
up vote 5 down vote accepted

That's not terribly easy to achieve, because AucTeX wasn't designed with that kind of extensibility in mind. You have a couple of options:

  • You can customize the font-latex-math-environments to add theorem and lemma to the list, and then AucTeX should highlight them as math environments.

  • If you don't want them to highlight as math, you can copy/paste/modify the functions in font-latex.el that are responsible for recognizing math environments and tweak them for your new ones. The relevant functions are and font-latex-extend-region-backwards-math-envII and font-latex-match-math-envII. The former needs to be added to the list font-latex-extend-region-functions, so that if you edit text in the middle of a multiline region emacs can properly fix up the font locking. The latter is added to font-latex-keywords-2 so that emacs knows how to fontify your new regions. The following code will do this:

(defcustom font-latex-lemma-environments
  '("theorem" "lemma")
  "List of lemma-like environment names for font locking."
  :type '(repeat string)
  :group 'font-latex)

(add-hook 'LaTeX-mode-hook
          (lambda ()
(defun font-latex-match-lemma-envII (limit)
  "Match lemma patterns up to LIMIT.
Used for patterns like:
 fontified stuff
The \\begin{equation} and \\end{equation} are not fontified here."
  (when (re-search-forward (concat "\\\\begin[ \t]*{"
                   (regexp-opt font-latex-lemma-environments t)
               limit t)
    (let ((beg (match-end 0)) end)
      (if (re-search-forward (concat "\\\\end[ \t]*{"
                       (match-beginning 1)
                       (match-end 0))))
                 ;; XXX: Should this rather be done by
                 ;; extending the region to be fontified?
                 (+ limit font-latex-multiline-boundary) 'move)
          (setq end (match-beginning 0))
    (goto-char beg)
        (setq end beg))
      (font-latex-put-multiline-property-maybe beg end)
      (store-match-data (list beg end))

(defun font-latex-extend-region-backwards-lemma-envII (beg end)
  "Return position to extend region backwards for lemma environments.
Return nil if region does not have to be extended for a multiline
environment to fit in.  The region between the positions BEG and
END marks boundaries for searching for environment ends."
    (goto-char end)
    (catch 'extend
      (while (re-search-backward
          (concat "\\\\end[ \t]*{"
              (regexp-opt font-latex-lemma-environments t)
              "\\*?}") beg t)
    (when (and (re-search-backward (concat  "\\\\begin[ \t]*{"
                         (match-beginning 1)
                         (match-end 0)))
                       (- beg font-latex-multiline-boundary) t)
           (< (point) beg))
      (throw 'extend (point))))

(add-to-list 'font-latex-keywords-2 '(font-latex-match-lemma-envII (0 'font-latex-warning-face t t)))
(add-to-list 'font-latex-extend-region-functions 'font-latex-extend-region-backwards-lemma-envII)
share|improve this answer
This works. Thanks! – Wei Yu Nov 25 '11 at 16:57

I am not very sure what you mean by highlight. Do you want to change the font of the begin/end or the general face of the whole environment?

Maybe using the the Hi Lock mode you could achieve something easily.

If this is what you are looking for, let me know and we'll figure out a simple way to add that to the AucTex hook to have it highlight every time.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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