When I'm in a .tex file, press M-: and run

(font-latex-add-keywords '(("autoref" "*{") ("Autoref" "{") ("nameref" "*{"))

the buffer fontifies occurrences of \nameref and \autoref appropriately (and my custom-defined \Autoref. However, if I put this same code in my .emacs file (say under the :config section of (use-package auctex ... or in the latex-mode-hook), then those macros are no longer fontified as references in .tex buffers. How do I fix this?

This is a related question, but I am looking for a simpler solution that, ideally, simply incorporates the one-liner above.


Call your setting from the LaTeX-mode-hook. It's not very user friendly but latex-mode-hook is defined in tex-mode.el, which is built-in but not part of AUCTeX, while LaTeX-mode-hook comes from latex.el which is part of AUCTeX.

So this should work,

(defun my-LaTeX-mode-setup ()
  (font-latex-add-keywords '(("autoref" "*{") ("Autoref" "{") ("nameref" "*{"))

(add-hook 'LaTeX-mode-hook #'my-LaTeX-mode-setup)
  • I have tried what you said, and this does not work for me. Feb 6 at 5:53
  • @bashfuloctopus I just tried with the code above as the sole content of my init file and it worked (on Emacs 27.1). Are you sure your AUCTeX is up to date and there are not other settings that may interfere? Maybe try for yourself running Emacs with just my code in the init. If it works, there's something else that interferes. Feb 6 at 9:16

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.