63

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.

MvG
  • 2,643

6 Answers6

63

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}
  • 10
    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
  • 3
    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
  • 1
    Plain text rendering doesn't work with LuaTex. – sphakka Sep 11 '15 at 09:20
  • All that just to get the degree symbol? An easier way when using static formulas is to do 90^o. – Jules Manson Jun 06 '19 at 05:03
32

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}

enter image description here

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}
  • 3
    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
  • 3
    With siunitx I believe one should used \ang{90} here. – cgnieder Nov 27 '12 at 14:13
  • Sure, it doesn't fit the needs of the OP... However, it is semantically very clean. That's why I'd prefer it, anyway. – cgnieder Nov 27 '12 at 15:00
  • As a physicist I would say that this is (currently) the best of all answers! Nevertheless I think, it is defined via \textdegree. – strpeter May 17 '14 at 17:34
15

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.

Gizm0
  • 179
  • 3
    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
  • 3
    relevant to the OP's question or not, it helped me get something that works as degree sign, so +1 –  Aug 10 '15 at 14:21
  • 2
    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 07:16
  • This circle is actually larger than a degree symbol, and this looks odd when compared to the text mode symbol. – Ruslan Aug 10 '21 at 11:38
7

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}
hpesoj626
  • 17,282
  • 6
    I'm not sure that I'd recommend utf8x; for instance it's incompatible with biblatex – egreg Nov 27 '12 at 12:15
  • 1
    @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
  • 1
    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
5

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}
MvG
  • 2,643
0

Define new command \degree

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

Use it

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

etc.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036