Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
Use XeTeX/LuaTeX and unicodemath. –  Martin Schröder Nov 28 '12 at 7:33

5 Answers 5

up vote 19 down vote accepted

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}
share|improve this answer
5  
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
1  
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

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}
share|improve this answer
2  
Or \SI{90}{\degree}. –  percusse Nov 27 '12 at 12:06
    
@percusse Yes. Added now. Thank you. :-) –  Harish Kumar Nov 27 '12 at 12:09
    
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
1  
With siunitx I believe one should used \ang{90} here. –  cgnieder 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. :-) –  Harish Kumar Nov 27 '12 at 14:46

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}
share|improve this answer
4  
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 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.

share|improve this answer
1  
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 at 15:24

Define new command \degree

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

Use it

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

etc.

share|improve this answer
    
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 at 15:37
    
What should be the advantage to use ^\circ in contrast to siunitx? –  strpeter May 17 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.