38

I wanted to find a simple way to made degree symbol ° and have tried textcomp and gensymb, but none of them works. I wanted to avoid using ^{\circ}, which isn't nice when you have a long document. I thought may be something easy can be achievable, e.g. \degree?

Martin Scharrer
  • 262,582
BrettHarry
  • 3,361
  • 6
  • 29
  • 23

5 Answers5

35

Maybe it’s a good alternative to use siunitx

\documentclass{minimal}
\usepackage{siunitx}
\begin{document}
    Works in text \ang{45} an in math $\ang{2.4}$.
    Even with minutes and seconds: \ang{40;12;08}.
\end{document}

It also gives you the oportunity to set all numbers in the same style an change it globally if it’s requested, e. g. changing from period to comma: 1.234 --> 1,234

Edit: To type radians just use the \radian unit. One may likes to define an own macro.

\documentclass{scrartcl}
\usepackage{siunitx}
\newcommand{\rad}[1]{\SI{#1}{\radian}}
\begin{document}
    \SI{\pi}{\radian}
    \rad{\pi/2}
\end{document}
Tobi
  • 56,353
  • For the sake of completeness, is there a way to insert an angle in radians? – Dror Sep 16 '12 at 05:12
  • 1
    @Dror: How do you mark radians? As far as I know they are simple decimal numbers (\num{3.14}) or multiples of π ($2\pi/3$), aren’t they? – Tobi Sep 16 '12 at 15:47
  • Well, you would like to add the "unit" at the end of the number, as you could just as well have the notion of \pi degrees. Any number can measure either radians or degrees. A more or less standard notation for radians would be something like \mathrm{rad} I guess. – Dror Sep 18 '12 at 13:46
  • @Dror: (Sorry for the late reply …) At my university we usually don’t mark radians we just write the number – normally the the context makes clear that an angle is meant. If you like to ad “rad” you may use siunitx’ \radian as shown in my edit. – Tobi Oct 03 '12 at 18:59
  • 2
    +1, you could also add \SI{20}{\celsius} as an example if the OP is asking for temperature. – quinmars Oct 25 '12 at 21:55
  • Hm … I think the questions asks about angles, but it’s good to have this advice in your comment :-) – Tobi Oct 26 '12 at 00:12
  • :) True I also think the question is about angles. – quinmars Oct 28 '12 at 18:52
28

Anything wrong with textcomp's \textdegree?

lockstep
  • 250,273
  • I updated yesterday (TexShop), and made sure my files were intact after that. In fact, even for the first time that I installed it didn't work. @ Martin - Do add the `\newcomand{}...' in the main '.cls' file? – BrettHarry Jul 01 '11 at 21:33
  • 2
    @BrettHarry: textcomp is not only a package but also provides fonts (I presume) which complicates the installation. I recommend to install it over the package manager of your TeX distribution. – Martin Scharrer Jul 01 '11 at 21:37
  • @Martin: OK! Did you mean I install the package first then add the files to folders of the main package? I only get about 4 .sty files of textcomp. Sometimes I'm do wonder how to install different products but end up piling them in folders when they contain the same suffix .e.g all .sty files. – BrettHarry Jul 01 '11 at 21:44
  • @BrettHarry: Sorry, I can't help you. I never need to install any fonts by myself. – Martin Scharrer Jul 01 '11 at 22:25
  • @BrettHarry You work on a mac, so do you install MacTex or BasicTeX? I recommend MacTeX if you begin with TeX. BasicTeX comes only with latin modern font. – Alain Matthes Jul 02 '11 at 05:39
  • @Martin @ lockstep @ Altermundus: Thanks! Your ideas worked....I corrected my installations of MacTex and used \degree. @ Tobi: great idea! Never thought that's possible. – BrettHarry Jul 02 '11 at 07:58
  • I landed here because in textcomp manual (version 29-octobre-2014 here) there is no mention of \textdegree... ;-) – Rmano Jun 12 '17 at 11:07
6

I prefer this solution

\renewcommand{\deg}{\ensuremath{^{\circ}}\xspace}

because it also works in math mode. (At least for me, \textdegree does not work in math mode, i.e.

$> 30\textdegree$

does not work, but

$> 30\deg$

does.

cgnieder
  • 66,645
Gab
  • 233
4

Using answers from this question, I've had good results with the following code:

\usepackage[utf8]{inputenc} % interpret input as unicode
\usepackage[T1]{fontenc}    % choose main font encoding (Cork)
\usepackage{textcomp}       % additional symbols using companion encoding TS1
\usepackage{gensymb}        % provides macro \degree which works in text and math
\DeclareUnicodeCharacter{00B0}{\degree} % Allow entering ° instead of \degree

As your original question states that gensymb didn't work for you, I assume that either the fontenc or the textcomp lines were missing in your setup, or some part of your TeX installation was missing, as these packages are usually shipped with your TeX distribution. The gensymb package goes to great length to provide its symbols from various sources, so it will behave differently depending on which other packages are loaded. See its documentation for details.

MvG
  • 2,643
1

You can cover math and text in one command using \ifmmode:

\DeclareDocumentCommand{\degC}{}{%
   {\ifmmode\text{\textdegree C}%
    \else\textdegree C\fi}\xspace%
}

where \textdegree could be instead be \symbol{"00B0} if you favor unicode.

It may be worth mentioning that \textdegree and $^\circ$ produce different glyphs. The first uses \textdegree (or \symbol{"00B0}) and the second $^\circ$.

The first uses <code>\textdegree</code> (or <code>\symbol{"00B0}</code>) and the second <code>$^\circ$</code>

John
  • 2,400