I find the asterisk of kpfonts too small when used in super/subscripts so, following answers here and here, I'm using @egreg's brilliant \DeclareMathActive to redefine * in math mode as \textstyle\ast:
\documentclass{article}
\usepackage{kpfonts,etoolbox}
\makeatletter
\newcommand{\DeclareMathActive}[2]{%
% #1 is the character, #2 is the definition
\expandafter\edef\csname keep@#1@code\endcsname{\mathchar\the\mathcode#1 } \begingroup\lccode~=#1\relax \lowercase{\endgroup\def~}{#2}% \AtBeginDocument{\mathcode#1="8000 }%
}
\newcommand{\std}[1]{\csname keep@#1@code\endcsname}
\patchcmd{\newmcodes@}{\mathcode\-\relax}{\std@minuscode\relax}{}{\ddt} \AtBeginDocument{\edef\std@minuscode{\the\mathcode-}}
\makeatother
\DeclareMathActive{*}{{\textstyle\ast}}
\begin{document}
$f_\mathscr{F} \quad f^\mathscr{F} \quad R^*$
$A*B$
\end{document}
This works wonderfully, except that in order to make R^* work without bracing the * (as in R^{*}), I have to add braces around \textstyle\ast. This then removes the \mathbin spacing in expressions like A*B.
Is there any hack to \DeclareMathActive that would allow for both unbraced * in super/subscripts and correct spacing in A*B? One idea is to extend DeclareMathActive to take arguments like \mathchoice:
\DeclareMathActiveChoice{<char>}{<display code>}{<text code>}{<script code>}{<scriptscript code>}
Then one could use \DeclareMathActiveChoice{*}{\ast}{\ast}{{\textstyle\ast}}{\ast}.
However I have no idea how to implement such an extension.
Just for context, here's why I want to do this at all. Compare
And for a professionally typeset comparison, here's the same line in Hartshorne's Algebraic Geometry:



^math active defined by\def^#1{\sp{#1}}– David Carlisle Apr 29 '23 at 18:58\catcode`\^=\active \def^#1{\sp{#1}}works. One also has to do\catcode`\_=\active \def_#1{\sb{#1}}. I thought perhaps this would breake{_}-type args of\NewDocumentCommandbut it seems to work fine. Is there any downside? Also feel free to post your comment as an answer – mbert Apr 29 '23 at 19:27\input{my_file}, which I suppose is expected – mbert Apr 29 '23 at 19:37\ifmmode, with\DeclareMathActive, or another way? – mbert Apr 29 '23 at 20:05\def\foohat#1{\sp{#1}}\DeclareMathActive{^}{\foohat}I guess – David Carlisle Apr 29 '23 at 20:13Missing { inserted– mbert Apr 30 '23 at 00:09^*. – Gaussler Sep 25 '23 at 16:15