Math Mode Macros – Creating New Command for Math Expression $^{\circ}$C

macrosmath-moderenewcommand

I frequently use $^{\circ}$C for writing the degree celsius unit.

Now I would like to make this into a command like \deg, \degree, \celsius, or any suitable command that is not already taken of course.

However, trying \newcommand{\deg}{$^{\circ}$C} does not work. In general, how would you create a command for an expression containing $?

I am aware that there is also the \textdegree command from the textcomp package. Nevertheless, I would like the C added as well. Just anything that makes this into one convenient command.

Any ideas?

Best Answer

You are likely looking for \ensuremath:

\documentclass{article}
\newcommand*{\degreeCelsius}{\ensuremath{{}^{\circ}}C}
\begin{document}
10\,\degreeCelsius
\end{document}

You could use \textdegree for text mode and \circ for math mode, e.g.

\documentclass{article}
\newcommand*{\degreeCelsius}{\relax\ifmmode{}^{\circ}\mathrm{C}\else\textdegree C\fi}
\begin{document}
10\,\degreeCelsius{} and $20\,\degreeCelsius$
\end{document}

As the author of siunitx, I'd of course favour using that package. I get the feeling you want free-standing units:

\documentclass{article}
\usepackage[free-standing-units]{siunitx}
\begin{document}
10\,\degreeCelsius{} and $20\,\degreeCelsius$
\end{document}
Related Question