3

Can I define a macro \foo so that $\foo$ produces:

  • bold sans-serif "foo", if the surrounding text is bold
  • normal sans-serif "foo" otherwise.

In particular, "foo" should never be typeset in italics.

MWE that does not quite work:

\documentclass[a4paper]{article}
\usepackage{amsmath,amsthm}
\newtheorem{theorem}{Theorem}
\newcommand{\foo}{\textsf{foo}}
\begin{document}
\section{This should be bold: $\foo$, \boldmath$2^\foo$}
This should be normal: $\foo$, $2^\foo$.
\begin{theorem}This should be normal: $\foo$, $2^\foo$.\end{theorem}
\end{document}

Here I get slanted text in the theorem environment; otherwise it looks good. If I try e.g.:

\newcommand{\foo}{\textnormal{\textsf{foo}}}

then it is always in normal font, not bold.

Can I just somehow "turn off italics" without "turning off bold"?

0

3 Answers 3

6

you can add one more level to the font specification for \foo:

\newcommand{\foo}{\textsf{\upshape foo}}

\textup would also work, but requires the "argument" form, \textup{foo}. both are defined in "basic" latex, so no extra packages are needed.

edit: as pointed out by Jukka in a comment, this is the best of all possibilities:

\text{\upshape\sffamily foo}

it will size correctly if used in sub- or superscripts, and it will avoid a (meaningless) font warning that is produced by both \textup and \upshape when used in this manner.

edit 2: Bernard provides a variation for the typing minimalists that works as well:

\textup{\sffamily foo}
4
  • It seems that this version gives a missing font warning (just like my original MWE). I guess it would be better to first use \upshape and only after that \sffamily to avoid the warning, e.g., \text{\upshape\sffamily foo}? Commented Sep 24, 2014 at 16:14
  • @JukkaSuomela -- yes. thank you for pointing that out. i'll add this to the exposition. Commented Sep 24, 2014 at 16:18
  • 2
    Or even (less typing) \textup{\sffamily foo}?
    – Bernard
    Commented Sep 24, 2014 at 16:24
  • @Bernard -- okay. that works too. Commented Sep 24, 2014 at 16:26
4

You can use \textsf{\textup{foo}}. But it looks odd that you are using your command always in math. In this context I would use \mathsf:

\documentclass[a4paper]{article}
\usepackage{amsmath,amsthm}
\newtheorem{theorem}{Theorem}
\newcommand{\foomath}{\mathsf{foomath}}
\newcommand\foo{\textsf{\textup{foo}}}
\begin{document}
\section{This should be bold: \mathversion{bold}$\foo \foomath$}
This should be normal: $\foo\foomath$.
\begin{theorem}This should be normal: $\foo\foomath$.\end{theorem}
\end{document}
4
  • I like this alternative, even if it does not do exactly what I wanted. :) Commented Sep 24, 2014 at 16:18
  • 1
    Well it depends on the meaning of foo: If it is a "math variable" then it should be set with math font commands. If you want to avoid to have to insert the \mathversion{bold} you can add it to the section definition. Commented Sep 24, 2014 at 16:23
  • Is there a painless way to make \mathsf{foo} look like \textsf{foo} if I am using \usepackage[charter]{mathdesign}\renewcommand{\sfdefault}{phv}, i.e., Charter as serif font and Helvetica as sans-serif font? Commented Sep 24, 2014 at 17:07
  • 1
    Sure. \DeclareMathAlphabet {\mathsf}{OT1}{phv}{m}{n} \SetMathAlphabet\mathsf{bold}{OT1}{phv}{bx}{n} Commented Sep 24, 2014 at 18:07
2

By putting it in an \mbox with \upshape, it dispenses with the italics. However, it won't rescale to subscripted math styles and the like, as is.

\documentclass[a4paper]{article}
\usepackage{amsmath,amsthm}
\newtheorem{theorem}{Theorem}
\newcommand{\foo}{\mbox{\upshape\textsf{foo}}}
\begin{document}
\section{This should be bold: $\foo$}
This should be normal: $\foo$.
\begin{theorem}This should be normal: $\foo$. still italic here\end{theorem}
\end{document}

However, with just a little extra work (and also taking Joseph's suggestion), it can be made to scale with the math size:

\documentclass[a4paper]{article}
\usepackage{amsmath,amsthm,scalerel}
\newtheorem{theorem}{Theorem}
\newcommand{\foo}{\scaleto{\mbox{\upshape\sffamily foo}}{1.6\LMex}}
\begin{document}
\section{This should be bold: $\foo$}
This should be normal: $\foo$ and $\foo_{\foo}$.
\begin{theorem}This should be normal: $\foo$. still italic here\end{theorem}
\end{document}

enter image description here

And as Jukka points out in the comment, a simpler definition (requiring amsmath but not requiring the scalerel package) that also scales with math size is

\newcommand{\foo}{\text{\upshape\sffamily foo}}
2
  • 2
    I'd have gone for \upshape\sffamily as there is no real need for an argument-based approach here :-)
    – Joseph Wright
    Commented Sep 24, 2014 at 16:08
  • 1
    Why not just use \text instead of \mbox; then it would also scale correctly? Commented Sep 24, 2014 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .