
I have a macro that I use to write units (so that I can write exponents easily), in math or in text mode.

I would like to change the definition of \mu in \upmu (from the upgreek package) only in this macro (not for standard math). This is what I wrote:

\newcommand{\U}[1]{%
\renewcommand{\mu}{\upmu}
\ensuremath{\mathrm{~#1}}%
}


It works, but I get an error at compilation:

ERROR: Argument of \@caption has an extra }.

--- TeX said ---
<inserted text>
\par
l.117 ... bla 30\U{\mu K} bla bla}

--- HELP ---
From the .log file...

I've run across a }' that doesn't seem to match anything.
For example, \def\a#1{...}' and \a}' would produce
this error. If you simply proceed now, the \par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your }' was spurious, just type 2' and it will go away.


for the incriminated code:

\caption{bla bla 30\U{\mu K} bla bla}

-
Add \protect before \renewcommand. Also add % after {\upmu}. –  karlkoeller Sep 18 '13 at 18:45
Thanks, this works. –  Geoffroy Sep 18 '13 at 18:54
Do you want it as an answer? –  karlkoeller Sep 18 '13 at 18:58
Yes, but if you could explain very briefly (or give a link) what \protect does in this case, it would be useful. –  Geoffroy Sep 18 '13 at 19:01
I've just realized that you're a member of TeX.SX from over a year... You had surely already read our starter guide time ago. I apologize. –  karlkoeller Sep 18 '13 at 19:20

Fragile commands like \renewcommand have to be \protected when used in moving arguments like the one of \caption (see What is the difference between Fragile and Robust commands? for more info).

Moreover, you have to add a trailing % whenever you don't want to insert extra space (see What is the use of percent signs (%) at the end of lines?).

So you should change your definition to

\newcommand{\U}[1]{%
\protect\renewcommand{\mu}{\upmu}%
\ensuremath{\mathrm{~#1}}%
}

-

You want to use

\DeclareRobustCommand{\U}[1]{%
\ensuremath{\let\mu\upmu\,#1}%
}


so you don't have to worry about untimely expansion of \U. Note that the proper spacing is \, rather than ~. Instead of \let\mu\upmu you could use \renewcommand{\mu}{\upmu} that's only less efficient.

However this is still wrong: the K should be upright, assuming it means the kelvin unit.

Here's how you should be doing it:

\documentclass{article}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{textgreek}
\sisetup{
text-micro=\textmu,
math-micro=\text{\textmu},
}

\begin{document}
\listoffigures
\begin{figure}[htp]
\caption{bla bla \SI{30}{\micro\kelvin} bla bla}
\end{figure}

\end{document}


-

I think what you want is something like

\ensuremath{\mathrm{~#1}}\egroup}


When you call this from within \caption, write

\caption{bla bla 30\protect\U{\mu K} bla bla}

-
Is there any adverse effect if one \protects the \renewcommand as suggested by karlkoeller? –  moewe Sep 18 '13 at 18:50
karlkoeller's solution is more convenient as I don't have to think of putting a \protect in front of each usage of my \U command. –  Geoffroy Sep 18 '13 at 18:57