1

When writing tex documents, I often create and include a file that contains a number of user defined macros, e.g., \newcommand{\pstar}{\ensuremath{\mathcal{P}^\star}}.

I'd like to set up emacs syntax highlighting so that every time I use one of my user defined macros, e.g., \pstar, the macro is highlighted in a default color. How can I do this without having to specify a list of my macros up front (as in this answer)?

  • You are abusing \ensuremath. See When not to use \ensuremath for math macro? – Henri Menke Aug 30 '18 at 5:20
  • You will have to specify a list of macros. Otherwise you'd have to implement a LaTeX parser in Lisp which scans the whole document and any \included files for command definitions. – Henri Menke Aug 30 '18 at 5:23
  • Thanks @HenriMenke. Before I started using auctex, I had the syntax highlighting I wanted (although, I didn't have a lot of other things I wanted). But, I'm actually not sure what tool was providing the highlight :). Perhaps it was the oh-my-zsh template I am using. Maybe there is another easy way to highlight expressions that start with a backslash in a default color when in latex mode (unless they are known keywords that already have a specific highlighting pattern). – akobre01 Aug 30 '18 at 13:16
0

It is possible to do what you're asking for, but I'm not recommending it. Hence, I show 2 solutions:

Not recommended solution

I presume you're not byte-compiling your init file. Put this function somewhere in your init file:

(defun LaTeX-fontify-parsed-macros ()
  "Add fontification support for parsed macros by AUCTeX."
  (let ((TeX-grop-string (string-to-char TeX-grop))
        macs macname args)
    ;; Process elements from `LaTeX-auto-optional' which look like
    ;; ("PSTAR" "1" "x")
    (dolist (mac LaTeX-auto-optional)
      (setq macname (car mac)
            args (string-to-number (cadr mac)))
      (push (list macname (concat LaTeX-optop
                                  (make-string (1- args)
                                               TeX-grop-string)))
            macs))
    ;; Process elements from `LaTeX-auto-arguments' which look like
    ;; ("PStar" "1")
    (dolist (mac LaTeX-auto-arguments)
      (setq macname (car mac)
            args (string-to-number (cadr mac)))
      (push (list macname (make-string args TeX-grop-string))
            macs))
    ;; Process only string elements from `TeX-auto-symbol' and ignore
    ;; lists inside the list
    (dolist (mac TeX-auto-symbol)
      (unless (listp mac)
        (push (list mac "") macs)))
    ;; Fontify the macros:
    (when (and macs
               (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords macs
                               'textual)) ))

Now in your .tex file, add this line to your file local variables at the end of the file:

%%% eval: (add-hook 'TeX-update-style-hook #'LaTeX-fontify-parsed-macros t)

It should look like this:

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% eval: (add-hook 'TeX-update-style-hook #'LaTeX-fontify-parsed-macros t)
%%% End:

Now hit C-c C-n, and you should get proper fontification for your macros.

Recommended solution

Move your definitions into a separate package, say mymathmacros.sty which looks like this:

\ProvidesPackage{mymathmacros}
\newcommand{\pstar}{\ensuremath{\mathcal{P}^\star}}
\newcommand{\Pstar}{\ensuremath{\mathcal{P}^\star}}

\newcommand{\PStar}[1]{\ensuremath{\mathcal{P}^\star}}

\newcommand{\PSTar}[1][x]{\ensuremath{\mathcal{P}^\star}}
\newcommand{\PSTAR}[3][x]{\ensuremath{\mathcal{P}^\star}}
\endinput

Customize the variable TeX-style-private to a directory of your choice, e.g. with a line like this in your init file:

(setq TeX-style-private (expand-file-name "~/.emacs.d/my-auctex-styles/"))

Save this piece of code in that directory as mymathmacros.el:

(TeX-add-style-hook
 "mymathmacros"
 (lambda ()
   ;; Macros:
   (TeX-add-symbols
    '("pstar" 0)
    '("Pstar" 0)
    '("PStar"  1)
    '("PSTar" [ "argument" ] 1)
    '("PSTAR" [ "argument" ] 2))

   ;; Fontification:
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-lock-add-keywords '(("pstar" "")
                               ("Pstar" "")
                               ("PStar" "{")
                               ("PSTar" "[{")
                               ("PSTAR" "[{{"))
                             'textual)))
 LaTeX-dialect)

Restart your Emacs, add the line \usepackage{mymathmacros} to your .tex file and hit C-c C-n, if necessary.

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.