[Tex/LaTex] How to the \textmu symbol be used in math mode and resize correctly

boxesgreekmath-mode

I wish to use an upright mu character in various places in my document, and so I've put the following in my preamble:

% textcomp, for its upright mu
\usepackage{textcomp}
% some user macros
\newcommand{\murm}{\hbox{\textmu}}

This is fine in both text and math mode, except when I try to place the mu in sub/superscript positions, and its size remains unchanged. Is there any way to resize it to fit its environment within math mode?

Best Answer

You can use \mathchoice to give four different definitions of the macro dependent if it is in display, in-text, script or scriptscript mode. (See also \mathpalette)

\newcommand{\murm}{%
    \mathchoice
        {\hbox{\normalsize\textmu}}
        {\hbox{\normalsize\textmu}}
        {\hbox{\scriptsize\textmu}}
        {\hbox{\tiny\textmu}}%
}

The whole expression should actually be wrapped into \ensuremath (amsmath package) or use \ifmmode manually to avoid errors if used in text-mode:

\newcommand{\murm}{%
  \ifmmode
    \mathchoice
        {\hbox{\normalsize\textmu}}
        {\hbox{\normalsize\textmu}}
        {\hbox{\scriptsize\textmu}}
        {\hbox{\tiny\textmu}}%
  \else
    \textmu
  \fi
}
Related Question