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.

- 179
- 1
- 8
1 Answers
You can add fontification support for your \foo
like other macros:
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'tsetq
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, hencesetq
-ing it doesn't work.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:
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.

- 1,975
- 1
- 8
- 14
(setq font-latex-match-reference-keywords '( ("foo" "*[<") ) )
but that this did not seem to work. – sgmoye Mar 14 '21 at 12:27