With some help I have redefined the autoref command to take an optional argument, see https://tex.stackexchange.com/questions/428229/autoref-with-optional-argument-text-in-generated-link

Now I am trying to convince Emacs/auctex to properly syntax highlight the new command. AFAICT setting it through font-latex-match-reference-keywords does not work as it is already defined by auctex itself, but actually only once auctex has recognized the LaTeX document loads hyperref.

When I M-: (setcar (cdr (TeX-assoc-string "autoref" font-latex-match-reference-keywords-local)) "*[{") in an open LaTeX source file I seem to get what I want. However, I cannot seem to get this working automatically...

I've placed the above line in a defun jk/tex-mode-hook and then added that to TeX-mode-hook

(defun jk/tex-mode-hook ()
    "Set up TeX mode (auctex)"
    (add-hook 'before-save-hook 'delete-trailing-whitespace nil t)
    (setq reftex-plug-into-AUCTeX t
          reftex-refstyle "\\autoref")
    (setcar (cdr (TeX-assoc-string "autoref" font-latex-match-reference-keywords-local)) "*[{")
(add-hook 'TeX-mode-hook 'jk/tex-mode-hook)

but to no avail...

How do I evaluate the (correct) replacement command late enough to become effective in my LaTeX sources?


The docstring of font-latex-match-reference-keywords-local says:

font-latex-match-reference-keywords-local is a variable defined in ‘font-latex.el’.

Buffer-local keywords to add to font-latex-match-reference-keywords.

This must be a list where each element is a list consisting of a keyword string (not a regular expression) omitting the leading backslash and a format specifier as described in the doc string of font-latex-user-keyword-classes.

This is an internal variable which should not be set directly. Use font-latex-add-keywords instead.

For this kind of additions, I strongly suggest put your .tex code in an extra package autoref-withopt.sty which can look like this:



The corresponding AUCTeX style file looks like this:

 (lambda ()
   ;; Make sure we load hyperref.el
   (TeX-run-style-hooks "hyperref")

   ;; Make \autoref available with opt. argument
    '("autoref"  [ "Text" ] TeX-arg-ref)
    '("autoref*" [ "Text" ] TeX-arg-ref))

   ;; Fontification
   (when (and (fboundp 'font-latex-add-keywords)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("autoref" "*[{"))

In your init file, add these lines and save the code above in the directory specified in TeX-style-private as autoref-withopt.el

(setq TeX-style-private
      (expand-file-name "~/.emacs.d/private-styles"))
(setq TeX-parse-self t)

In your .tex file, add \usepackage{autoref-withopt} and restart Emacs, open your .tex file again and hit C-c RET autoref RET.

  • Great, thank you Arash! For completeness, simply putting (TeX-run-style-hooks "hyperref") before the (setcar ... also works, but kind of assumes that hyperref+the modified autoref is always loaded – which it effectively is for me... But the clean solution by Arash is way more general and much nicer – and what I am using now! ;-) – yogik Apr 27 '18 at 19:46

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.