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.
\ensuremath. See When not to use \ensuremath for math macro? – Henri Menke Aug 30 '18 at 05:20\included files for command definitions. – Henri Menke Aug 30 '18 at 05:23