Nonstandard analysis: Typesetting `^*` depending on context

Until someone comes up with a nice solution, a brute-force one could be:

\documentclass{scrartcl}
\usepackage{xparse,dsfont}

\ExplSyntaxOn
\NewDocumentCommand \nsext { m }
 {
  {\vphantom{#1}}
  \sp
   {
    *
    \str_case:nn {#1}
     {
      { X } { \mskip-3mu }
      { A } { \mskip-6mu }
     }
   }
  #1
 }
\ExplSyntaxOff

\newcommand*{\R}{\mathds{R}}

\begin{document}
$\nsext\R \quad \nsext X \quad \nsext V \quad \nsext A$
\end{document}

You just need to add a pair inside the \str_case:nn second argument if you want to add a new letter and the according space to remove.

enter image description here


REVISED APPROACH

Comments from the OP indicated that my original solution, while perhaps nice to look at, relied on changing the math font to ptmx, which was not acceptable. So the issue seemed to be that the math kerning of the ptmx font was OK, but that of ComputerModern (CM) was inadequate for the current task.

With that in mind, I decided to declare the ptmx math alphabet separately, and use it only for positioning of the CM glyphs. EDITED to declare a new math alphabet. Then, when I am stacking the * over/before the given argument, I use the \mathptmx version of the argument (that I just declared) to govern the offset from the right hand.

To account for arguments that are not pure alphabetic glyphs, I start out with a catcode test. In this MWE below, you see my approach on the top line, compared with the raw ComputerModern construction of $^*<letter>$ on the second line.

EDITED (8/2016) to work in subscript math styles, per an e-mail request of a reader. For this, I use the \ThisStyle{...\SavedStyle...} feature of the scalerel package to import the math style to places where it would otherwise be lost. RE-EDITED to \leavevmode to handle use-case in \substack.

\documentclass{article}
\usepackage{amssymb,stackengine,xcolor,scalerel,mathtools}
\stackMath
\def\nsa#1{\leavevmode\ThisStyle{%
\def\stackalignment{r}\def\stacktype{L}%
\ifcat A#1
  \mkern-6.5mu\stackon[0pt]{\SavedStyle\phantom{f}#1}  
    {\SavedStyle^*\mkern-1.1mu\phantom{\mathptmx{#1}}}%
\else
  \mkern-4mu\stackon[0pt]{\SavedStyle\phantom{f}#1}  
    {\SavedStyle^*\mkern-1.7mu\phantom{#1}}%
\fi
}}
\def\R{\mathbb{R}}
\DeclareMathAlphabet{\mathptmx}{OML}{ztmcm}{m}{it}
\parskip 1ex
\begin{document}
\centering
$(\nsa\R) ~ (\nsa V) ~ (\nsa X) ~ (\nsa A) ~ (\nsa M)$

vs.

$(^*\R) ~ (^*V) ~ (^*X) ~ (^*A) ~ (^*M)$

\hrulefill

Other cases requiring EDIT to \textbackslash nsa:

$(x_n)_{n\in\nsa{\mathbb N}}$. 

$\bigcup_{\substack{U\subseteq X\\ \nsa U\subseteq \mathrm{Fin}(\nsa X)}}$
\end{document}

enter image description here

ORIGINAL APPROACH (ptmx math)

This tries to align the * approximately where the right end of an f might be. The first row shows the kerning I was trying to emulate (the model); the second row shows the implemented macro; while the third row shows how the macro succeeds at its goal (the method, with * overlaying right end of f)

\documentclass{article}
\usepackage{amssymb,mathptmx,stackengine,xcolor}
\stackMath
\def\nsa#1{\def\stackalignment{r}\def\stacktype{L}%
  \mkern-1mu\stackon[0pt]{\mkern-2mu\phantom{f}#1}{^*\mkern-1.7mu\phantom{#1}}}
\def\R{\mathbb{R}}
\begin{document}
$ f\R ~fV ~fX ~fA$ The model

$\nsa\R ~ \nsa V ~ \nsa X ~ \nsa A$ The macro

\def\nsa#1{\def\stackalignment{r}\def\stacktype{L}%
  \mkern-1mu\stackon[0pt]{\color{cyan}\mkern-2mu f#1}{^*\mkern-1.7mu #1}}
$\nsa\R ~ \nsa V ~ \nsa X ~ \nsa A$ The method
\end{document}

enter image description here


Here's a LuaLaTeX-based solution, which sets up a Lua function that adjusts the space between the asterisk and the subsequent letter, where the adjustment amount depends on the shape of the letter.

The code defines LaTeX macro named \nsx (short for "nonstandard extension") that prefixes an asterisk to the macro's argument -- typically an uppercase letter; the default spacing adjustment between the asterisk and the letter is -4mu. (A negative thinspace, \!, equals -3mu.) The code next sets up a Lua function that overrides the default adjustment amount for selected letters.

See the table below for the adjustment amounts I've been able to come up with for the 26 uppercase letters of the Latin alphabet as well as for \mathbb{R} and \Gamma. Note that these adjustment amounts are optimized for the "Computer/Latin Modern" math fonts. Other font families will probably require different adjustment amounts.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\newcommand\nsx[2][4]{{}^{*}\mkern-#1mu#2}  % default neg. space: 4mu

\usepackage{amsfonts,array,booktabs} % just for this example

\usepackage{luacode,luatexbase}
\begin{luacode}
function adjust_ns ( line )
    if string.find ( line, "\\nsx" ) then
       line = string.gsub ( line, "\\nsx{([AJ])}", "\\nsx[6.5]{%1}" )
       line = string.gsub ( line, "\\nsx{([X])}", "\\nsx[4.5]{%1}" )
       line = string.gsub ( line, "\\nsx{([SZ])}", "\\nsx[3.5]{%1}" )
       line = string.gsub ( line, "\\nsx{([CGOQUVW])}", "\\nsx[2.5]{%1}" )
       line = string.gsub ( line, "\\nsx{([Y])}", "\\nsx[1.5]{%1}" )
       line = string.gsub ( line, "\\nsx{([T])}", "\\nsx[1]{%1}" )
       line = string.gsub ( line, "\\nsx{\\mathbb{R}}", "\\nsx[1.5]{\\mathbb{R}}" )
       line = string.gsub ( line, "\\nsx{\\Gamma}", "\\nsx[2]{\\Gamma}" )
    end
    return  line
end
luatexbase.add_to_callback ( "process_input_buffer", adjust_ns, "adjust_ns" )
\end{luacode}

\begin{document}

\noindent
\begin{tabular}{@{} >{$}l<{$} l @{}}
$Letter$ & Adjustment (in ``mu'')\\
\midrule
\nsx{B}\nsx{D}\nsx{E}\nsx{F}\nsx{H}\nsx{I}\nsx{K}\nsx{L}\nsx{M}\nsx{N}\nsx{P}
\nsx{R} & 4 (default)\\
\nsx{A}\nsx{J} & 6.5\\
\nsx{X} & 4.5\\
\nsx{S}\nsx{Z} & 3.5\\
\nsx{C}\nsx{G}\nsx{O}\nsx{Q}\nsx{U}\nsx{V}\nsx{W} & 2.5 \\
\nsx{Y} & 1.5 \\
\nsx{T} & 1 \\
\nsx{\mathbb{R}} & 1.5 \\
\nsx{\Gamma} & 2 \\
\end{tabular}
\end{document}