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.

1 Answer 1

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.

4
  • Perfect. Thank you.
    – sgmoye
    Commented Mar 11, 2021 at 17:53
  • I should have pointed out in my question that I tried in my .emacs file: (setq font-latex-match-reference-keywords '( ("foo" "*[<") ) ) but that this did not seem to work.
    – sgmoye
    Commented Mar 14, 2021 at 12:27
  • @sgmoye - See my updated answer. Commented Mar 14, 2021 at 17:19
  • Ah. Thanks for the information!
    – sgmoye
    Commented Mar 14, 2021 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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