3

LaTeX3 allows for some flexibility in specifying optional arguments to create commands and environments. For example you could specify \NewDocumentCommand{\foo}{d<>m}{...} and this would be used as \foo<something optional>{something required} -- d<> specifies an optional argument surrounded by angle brackets. What I would like to be able to do is to color the optional argument in AUCTeX in the same way that occurs with \section[something optional]{something mandatory}. Is this possible, and, if so, how accomplished? Thanks.

sgmoye
  • 179
  • 1
  • 8

1 Answers1

3

You can add fontification support for your \foo like other macros:

  1. Customize one of the variables font-latex-match-function-keywords, font-latex-match-reference-keywords etc. as described in the manual. This way, the fontification will be available for \foo in all your documents.
    Note that in this case, customize means use the customize interface and don't setq it in your init file. The docstring says:

    [...] Setting this variable directly does not take effect; restart Emacs.

    Customize has some code to run (font-latex-match-reference-make) in this particular case, hence setq-ing it doesn't work.

  2. In your .tex file, add an entry to the file local variables which can look like this:

    \documentclass{article}
    
    \usepackage{xparse}
    \NewDocumentCommand\foo{d<>m}{...}
    
    \begin{document}
    
    \foo<optional>{mandatory}
    
    \end{document}
    
    %%% Local Variables:
    %%% mode: latex
    %%% TeX-master: t
    %%% eval: (font-latex-add-keywords '(("foo" "<{")) 'reference)
    %%% End:
    

    It looks like this for me:
    enter image description here

  3. Finally, if you have your definition in a .sty file and load it via \usepackage, you have to write an AUCTeX style file and put some lisp code there.

Arash Esbati
  • 1,975
  • 1
  • 8
  • 14