# Unicode degree symbol ° in math formulas: \textdegree invalid in math mode

I'd like to be able to simply input a degree symbol as ° in my source code. German keyboards have a key for this, and it makes the source so much more readable. I'm using pdflatex from TeXLive 2012, and this minimal reproducing example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\begin{document}
Degree symbol in plain text works: 90° is a right angle.
Degree in math won't work: $\cos(90°) = 0$.
\end{document}


Compiling this document, I get the message

LaTeX Warning: Command \textdegree invalid in math mode on input line 11.


printed twice. Looking at the log for this run, I see an additional error message

Missing character: There is no <B0> in font cmr10!


where <B0> actually refers to a single byte that does not form a valid UTF-8 sequence, so is not properly printable on my console.

I have a rough idea where all of this comes from, but little idea on how to solve this. I'd like to either make the \textdegree symbol work in math mode as well, or have the engine substitute some different command in place of my unicode code point. Ideally, that command would choose the right form depending on whether or not the current mode is math mode, which I don't know how to check (yet). For the final result, it would be nice if the symbol looked the same in both text and math modes, so I'd probably aim for a solution that dies switch to text mode to output that symbol.

But perhaps there is an easier way to do all this. Perhaps there even is a package for this. Searching through previous posts here, I found there is a package unicode-math, but that does not work with pdfLaTeX, and switching the engine does not appear to be a reasonable requirement for most of my applications.

• Use XeTeX/LuaTeX and unicodemath. – Martin Schröder Nov 28 '12 at 7:33
• @Martin: apparently, in some distributions (f.i. texlive-2014 on Gentoo) the correct name of the package is unicode-math! – sphakka Sep 11 '15 at 9:26

The code below should work:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{gensymb}
\begin{document}
Degree symbol in plain text works: 90° is a right angle.
Degree in math now work: $\cos(90\degree) = 0$.
\end{document}

• If you want to use the ° character also in math mode, do \usepackage{newunicodechar} and \newunicodechar{°}{\degree} – egreg Nov 27 '12 at 12:06
• @egreg: Nice package, good to know. For obvious reasons I get a Warning: Redefining Unicode character. But this appears to be harmless enough, and the combination of gensymband newunicodechar really looks like what I'm trying to achieve. – MvG Nov 27 '12 at 12:26
• @MvG I decided not to suppress such warnings; better safe than sorry. :) – egreg Nov 27 '12 at 12:39
• For reference: As I found no easy way to undefine the character in order to avoid the warning, I decided to use \DeclareUnicodeCharacter{00B0}{\degree} in at least one project. I don't want to get into a habit of ignoring warnings, and that particular prelude is often used but rarely edited, so actually looking up the codepoint once isn't that much work. – MvG Nov 27 '12 at 12:46
• Plain text rendering doesn't work with LuaTex. – sphakka Sep 11 '15 at 9:20

That is why you have the siunitx package:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{siunitx}
\begin{document}
Degree symbol in plain text works: 90\si{\degree} (or \SI{90}{\degree}) is a right angle.
Degree in math would work: $\cos(90\si{\degree}) = 0$.
\end{document}


As noted by cgnieder, \ang can also be used for this purpose:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{siunitx}
\begin{document}
Degree symbol in plain text works: \ang{90} is a right angle.
Degree in math would work: $\cos(\ang{90}) = 0$.
\end{document}

• Or \SI{90}{\degree}. – percusse Nov 27 '12 at 12:06
• Perhaps I wasn't clear enough, but I'm specifically interested in solutions that allow me to enter ° without having to use any macro instead or in addition to that. – MvG Nov 27 '12 at 12:11
• With siunitx I believe one should used \ang{90} here. – clemens Nov 27 '12 at 14:13
• @cgnieder: That is the correct usage. But here it becomes only a formality given what the OP wants. Any how added to the answer. Thank you. :-) – user11232 Nov 27 '12 at 14:46
• Sure, it doesn't fit the needs of the OP... However, it is semantically very clean. That's why I'd prefer it, anyway. – clemens Nov 27 '12 at 15:00

I use ^{\circ} in math mode to get the degree symbol. It's worked every time. I have run into compatibility issues with some other code/package combinations.

• That's all well and good, but it's not really relevant to the question of how to use the nice easy to read inline degree sign. – Chris H Jan 17 '14 at 15:24
• relevant to the OP's question or not, it helped me get something that works as degree sign, so +1 – user84482 Aug 10 '15 at 14:21
• This approach doesn't actually produce a degree symbol -- take a look at copy-paste text from your final PDF. – jdpipe Aug 12 '18 at 7:16

If you don't need textcomp package, removing it and changing utf8 option of inputenc to utf8x might work for you as it did for me.

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage{textcomp}
\begin{document}
Degree symbol in plain text works: 90° is a right angle.
Degree in math won't work: $\cos(90°) = 0$.
\end{document}

• I'm not sure that I'd recommend utf8x; for instance it's incompatible with biblatex – egreg Nov 27 '12 at 12:15
• @egreg well, that is a one large drawback but I am still putting this as an option should some people find it useful. – hpesoj626 Nov 27 '12 at 12:16
• I still get the warning, but the symbol is there in the output. Nice. Biblatex doesn't bother me for now. It's good to know about that option (it isn't particularly prominent in the docs), to decide on a per-case basis. – MvG Nov 27 '12 at 12:22
• It does emit a warning, not an error, but it does that even with the code exactly as you posted it. – MvG Nov 27 '12 at 12:36

I ended up using the gensymb package from Papiro's answer to get the \degree command, combined with a line to bind that to the ° symbol which was inspired by a comment to that answer:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{gensymb}
\DeclareUnicodeCharacter{00B0}{\degree}
\begin{document}
Degree symbol in plain text still works: 90° is a right angle.
Degree in math now works as well: $\cos(90°) = 0$.
\end{document}


Define new command \degree

\newcommand{\degree}{\ensuremath{^\circ}}


Use it

right angle: $90\degree$
hot water: $99.8\degree C$


etc.

• As noted in comments on some of the other answers, I think the OP is looking to use the Unicode symbol directly. – Joseph Wright May 14 '14 at 15:37
• What should be the advantage to use ^\circ in contrast to siunitx? – strpeter May 17 '14 at 17:09

## protected by Community♦Mar 31 '15 at 23:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).