I'd like some commands involving \textsf to be usable in both math mode and text-mode in a memoir document class:
Hello \abcd{}, $\abcd{}$.
to produce:
I found a solution involding \sf that works for articles, but unfortunately \sf works only in article and \textsf does not work in \ensuremath. Any idea how I could solve this problem?
\documentclass[]{memoir}
% Fails in both memoir and article
\newcommand\abcd{\ensuremath{\textsf{ab^{cd}}}}
% Fails in memoir
%\newcommand\abcd{\ensuremath{{\sf{ab^{cd}}}}}
\begin{document}
Hello \abcd{}, $\abcd{}$
\end{document}
EDIT
I tried to compare the different propositions of egreg: \textsuperscript does not work nicely with \textsubscript (rlap is dirty and gives bad results often), otherwise \mathfs gives decent results (even if I prefer the look of \textsuperscript for text.
\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\newcommand{\versionA}{\mbox{\sffamily ABC\textsuperscript{can}\textsubscript{sup}}}
\newcommand{\versionB}{\mbox{\sffamily ABC\rlap{\textsuperscript{can}}\textsubscript{sup}}}
\newcommand{\versionC}{\ensuremath{\textsf{ABC}^{\textsf{can}}_{\textsf{sup}}}}
\newcommand{\versionD}{\ensuremath{\mathsf{ABC}^{\mathsf{can}}_{\mathsf{sup}}}}
\newcommand{\versionE}{\ensuremath{\mathsf{ABC^{can}_{sup}}}}
\newcommand{\versionF}{\ensuremath{{\sf ABC^{can}_{sup}}}}
\newcommand{\versionAns}{\mbox{\sffamily ABC\textsuperscript{can}}}
\newcommand{\versionBns}{\mbox{\sffamily ABC\rlap{\textsuperscript{can}}}}
\newcommand{\versionCns}{\ensuremath{\textsf{ABC}^{\textsf{can}}}}
\newcommand{\versionDns}{\ensuremath{\mathsf{ABC}^{\mathsf{can}}}}
\newcommand{\versionEns}{\ensuremath{\mathsf{ABC^{can}}}}
\newcommand{\versionFns}{\ensuremath{{\sf ABC^{can}}}}
\begin{document}
\noindent With subscript:\
\versionA, $\versionA$, \versionAns, $\versionAns$, $\versionAns_\delta$: textsuperscript $\Rightarrow $ not aligned properly\
\versionB, $\versionB$, \versionBns, $\versionBns$, $\versionBns_\delta$: textsuperscript+rlap $\Rightarrow$ good looking, but rlap removes width so bad result if the lower script has a smaller width\
\versionC, $\versionC$, \versionCns, $\versionCns$, $\versionCns_\delta$: textsf $\Rightarrow$ too big\
\versionD, $\versionD$, \versionDns, $\versionDns$, $\versionDns_\delta$: mathsf 3 times $\Rightarrow$ best result, I just find the subscript text to be a bit too low compared to textsuperscript\
\versionE, $\versionE$, \versionEns, $\versionEns$, $\versionEns_\delta$: mathsf 1 time $\Rightarrow$ okish, just not working with unknown subscript.\
\versionF, $\versionF$, \versionFns, $\versionFns$, $\versionFns_\delta$: sf (fail with memoir), otherwise like mathsf\
\end{document}
Zoomed:



\ensuremathsounds like a great solution for that, but I was not expecting such breakages... Now, I have dozen of pages to correct if I can't find a quick fix. – tobiasBora Nov 12 '21 at 18:05\textsfworks in math mode why do you need\ensuremath?(but usually it better to use\mathsf– David Carlisle Nov 12 '21 at 18:23\sfwas the only solution that worked in articles, but\sffails withmemoirclass – tobiasBora Nov 12 '21 at 20:17\textsf{x}^{\textsf{y}}wherever you can havex^yso you are just asking for a superscript form that works in text and math. textsf isn't very related and memoir isn't involved at all. – David Carlisle Nov 12 '21 at 20:23x^{\textsf{y}}will be the right size if you useamsmathpackage (actually tehamstextpackage that it loads. – David Carlisle Nov 12 '21 at 20:25\textsf{x^y}to work exactly like\textsf{x}^\textsf{y}, so I though that the error was saying that\textsfwas simply unusable in math mode. – tobiasBora Nov 12 '21 at 20:25^is unusable in text mode – David Carlisle Nov 12 '21 at 20:26\sfhas been wrong since 1993! (memoir has an option to enable it again, but as others mention it is not the correct way) – daleif Nov 12 '21 at 20:27\sfwill work with thememoirclass if you use theoldfontcommandsclass option to enable the old deprecated font commands which you should not be using. Any chance of you deleting "memoir" from your question's title? – Peter Wilson Nov 13 '21 at 18:34