# How can the \textmu symbol be used in math mode and resize correctly?

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?

-

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
}

-
Is there a way to use \ensuremath or similar to choose whether in math mode or not, and issue the \mathchoice command if so, and the regular command if not? – Statto Mar 29 '11 at 13:36
@Statto: Yes, like \ensuremath you can use \ifmmode to check if you are in mathmode: \ifmmode \mathchoice ... (above code) \else \textmu \fi – Martin Scharrer Mar 29 '11 at 13:40
Thanks, that's perfect! – Statto Mar 29 '11 at 13:47
@Statto: You are welcome. Also note that you have now over 15rep points and are able to up-vote answers. – Martin Scharrer Mar 29 '11 at 13:51
@Martin: The AMS \text macro does exactly this. \newcommand*{\murm}{\text{\textmu}} will do the right thing – Danie Els Mar 30 '11 at 5:06

Why don't you use \upmu from the upgreek package? It will resize automatically in math mode.

-
Because the \upmu command doesn't render in the same font as the rest of the text. Since mu is the upright Greek character I'll be using most, it would be nice to get it right... – Statto Mar 29 '11 at 13:37