3

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:

enter image description here

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}

enter image description here

Zoomed:

enter image description here

tobiasBora
  • 8,684
  • What's the reason for having commands working both in text and math to begin with? – egreg Nov 12 '21 at 17:58
  • Well, they are at the beginning commands that were supposed to work mostly in text mode (because they are names of objects) so I used them as such mostly, but sometimes it is practical to use them in math mode, for instance to add arguments to it, like in $\abcd_{42}$. And \ensuremath sounds 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
  • \textsf works in math mode why do you need \ensuremath ?(but usually it better to use \mathsf – David Carlisle Nov 12 '21 at 18:23
  • How is this question related to the memoir class? – daleif Nov 12 '21 at 18:52
  • @daleif \sf was the only solution that worked in articles, but\sf fails with memoir class – tobiasBora Nov 12 '21 at 20:17
  • @DavidCarlisle because I have superscripts/subscripts. – tobiasBora Nov 12 '21 at 20:18
  • yes but that's more or less independent of the sans serif. you can have \textsf{x}^{\textsf{y}} wherever you can have x^y so 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:23
  • 1
    note x^{\textsf{y}} will be the right size if you use amsmath package (actually teh amstext package that it loads. – David Carlisle Nov 12 '21 at 20:25
  • @DavidCarlisle Well when I wrote that question I also got confused, I was expecting \textsf{x^y} to work exactly like \textsf{x}^\textsf{y}, so I though that the error was saying that \textsf was simply unusable in math mode. – tobiasBora Nov 12 '21 at 20:25
  • no it's saying ^ is unusable in text mode – David Carlisle Nov 12 '21 at 20:26
  • \sf has 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
  • \sf will work with the memoir class if you use the oldfontcommands class 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

1 Answers1

5

I see no reason for having a command that works in text and in math. See When not to use \ensuremath for math macro? and other threads.

If you really want it, which I doubt, do

\newcommand{\abcd}{\ensuremath{\mathsf{ab}^{\mathsf{cd}}}}

If you need that the letters obey the surrounding conditions (italic, for instance), then split \textsf:

\newcommand{\abcd}{\ensuremath{\textsf{ab}^{\textsf{cd}}}}

Of course, a more appropriate solution in this case would be

\newcommand{\abcd}{\mbox{\sffamily ab\textsuperscript{cd}}}

By the way, \sf has been a deprecated command for about 30 years.

egreg
  • 1,121,712
  • Thanks for your answer. Just minor remarks: \sfshape does not work, I guess you meant \sffamily. Then, \textsuperscript is not ideal because I also have subscripts and they don't play nicely together. \textsf gives too big text (see my comparison in my edit)... so I guess the best result is \mathsf. Thanks! It's not as good looking as \textsubscript+rlap for texts, but at least it does not fail if subscript is too small. – tobiasBora Nov 12 '21 at 20:22
  • Actually, as pointed out in comments (thanks @DavidCarlisle) I realized that loading amsmath solves the size issue for \textsf. – tobiasBora Nov 12 '21 at 20:29