5
\documentclass{article}

\newcommand{\nptime}{\ensuremath{\sf NP}}
\newcommand{\bnptime}{\ensuremath{\sf\bf NP}}

\begin{document}
  \nptime\\
  \bnptime
\end{document}

I'm trying to make a macro to typeset "NP" in boldface sans-serif font, but as demonstrated by my MWE, it's not working. BF seems to force a serif font.

Alternatively, I'd be happy to find an opposite to \ensuremath, but I can't seem to find one. I tried \text{} to no avail.

The following similar MWE does the same thing.

\documentclass{article}

\newcommand{\nptime}{{\sf NP}}
\newcommand{\bnptime}{{\sf\bf NP}}

\begin{document}
  \nptime\\
  \bnptime
\end{document}
Moriambar
  • 11,466
Mirrana
  • 2,601

1 Answers1

10

There's no need of \ensuremath: just use

\newcommand{\nptime}{\textsf{NP}}
\newcommand{\bnptime}{\textsf{\textbf{NP}}}

so that \nptime and \bnptime work both in text and math mode, even scaling when used in subscript or superscript if the amsmath package is used.


You discovered why \sf, \bf and all two letter font change commands are deprecated (and they have been since more than 10 years): the combination \sf\bf is simply equivalent to \bf, just like \sf\it would produce normal italic, not sans serif, and \it\sf would produce upright sans serif.

egreg
  • 1,121,712
  • It's not a good idea to use \text** for math, though. With this code, \bnptime{X} will appear as upright bold sans X if the surrounding text is upright, but as slanted bold sans X otherwise (as in a theorem). Assuming the question is about a math symbol, it would be safer to use \boldsymbol{\mathsf{X}} instead. – Silvio Levy Apr 26 '15 at 05:01
  • @SilvioLevy In case this can be a problem, a wrapping \textnormal or \textup can be used. Also \bm{\mathsf{X}} can be used, of course. – egreg Apr 26 '15 at 09:42
  • Regarding "a wrapping \textnormal or \textup can be used"

    Yes, but why should it? Why use \text** for math? Is there some secret advantage we're not aware of?

    The whole business of \bf, \sf, \rm, \it being a problem only arose because, after several years of NFSS not supporting these macros in math because of their fuzzy semantics, it was decided to support them again. And now we have to advise people not to use them, and point them to the alternatives. Why point to an alternative that is also semantically problematic?

    – Silvio Levy Apr 27 '15 at 01:39
  • @SilvioLevy I'm not sure I follow. These macros aren't intended to display mathematics, they're intended to display text (which can be inserted into mathematics, but follows very different typographical rules). Using \text…{…} seems quite natural to me. – Sean Allred Apr 27 '15 at 02:04
  • @SilvioLevy The two letter font switches are a problem independently of whether they work in maths or not. They are a problem when used in text. – cfr Apr 27 '15 at 02:40
  • @SeanAllred: the OP wants commands for use in math. This is clear since his first attempt included \ensuremath. So macros that aren't intended to display mathematics seem out of place. – Silvio Levy Apr 27 '15 at 05:53
  • @SilvioLevy Actually, I see \ensuremath as an indication that the macro will be used in text. – egreg Apr 27 '15 at 06:35
  • Well, "NP" should be typeset using textual conventions. It's an acronym, not the product of variables N and P. – Daira-Emma Hopwood Mar 02 '17 at 02:18
  • @DairaHopwood Isn't it what my answer says? – egreg Mar 02 '17 at 06:59
  • @egreg Yes, I was replying to SilvioLevy (but forgot to @). – Daira-Emma Hopwood Mar 13 '17 at 03:47