# Automatically replacing \º with $^\circ$

I don't like the º-symbol, that LateX is typesetting, because it has this strange underline.
So I wanted to make a new-command, that's replacing \º with $^\circ$ and i tried to define it like this in the preambel:

\newcommand{\º}{$^\circ$}


Unfortunatly what happened, was not what I expected:
My document got extended by a new first page showing the symbol $^\circ$ and my header, but nothing else. And the position where i actually used the command, completly ignored it, and output just 30 instead of 30º. What am I doing wrong? EDIT: MWE

\documentclass[12pt, a4paper, twoside, %openright,
toc=listof, BCOR=5mm, bibliography=totoc, parskip=half]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\newcommand{\º}{$^\circ$}
\begin{document}
30º this will give me the output shown in the image.
30\ºthis is supposed to output 30$^\circ$, but doesn't. Instead it produces a new first page showing the symbol $^\circ$.
\end{document}

• Your first error is that you don't give a complete example ... (the code depends beside others on the file encoding and the engine) – Ulrike Fischer Oct 13 '15 at 9:28
• I would use search and replace my favourite editor. Better to use a semantic macro with package siunitx. – Johannes_B Oct 13 '15 at 9:30
• @UlrikeFischer I'll add a complete example. @Johannes_B yes, i could do that. And how do I define the macro correctly using siunitx? – Max Oct 13 '15 at 9:35
• \SI{30}{\degree} from siunitx – user31729 Oct 13 '15 at 9:35
• º is not the degree symbol (°), but the "masculine ordinal". They look similar in some fonts, but in others the ordinal has the underscore you noticed. The best approach is to use siunitx package. – JLDiaz Oct 13 '15 at 9:37

You can just use \protected\def instead of \newcommand:

\documentclass[
12pt,
paper=a4,
twoside,
%openright,
toc=listof,
BCOR=5mm,
bibliography=totoc,
%parskip=half,
]{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\protected\def\º{\ensuremath{^\circ}}

\begin{document}

30º this will give me the output shown in the image.

30\º this is supposed to output 30$^\circ$, and it does.

\end{document}


However, you're using the wrong symbol for the degree, because º is U+00BA MASCULINE ORDINAL INDICATOR.

Here's a better way to do what you want:

\documentclass[
12pt,
paper=a4,
twoside,
%openright,
toc=listof,
BCOR=5mm,
bibliography=totoc,
%parskip=half,
]{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{newunicodechar}
\newunicodechar{°}{\ensuremath{^\circ}}

\begin{document}

30° is the right way to go.

\end{document}


• I tried the 2nd solution that you provided and it works perfectly. Thank you very much. I still replaced the actual unicode-degree-character with the "masculine ordinal indicator", because that's the one, my keyboard provides, which makes it easier for me. I'm using a MacBook and it doesn't seem to have the real degree-character on its keyboard – Max Oct 13 '15 at 10:40
• But the \def solution will only work once for every starting utf8 byte. An additional \protected\def\«{blub} would break it. – Ulrike Fischer Oct 13 '15 at 11:06
• @UlrikeFischer Yes, that's true; but I said that \º is the wrong way to go. – egreg Oct 13 '15 at 11:14
• @Max Degree character on mac keyboard is ⌥⇧8. – Random832 Oct 13 '15 at 12:15
• @Random832 i gotta correct myself: i have an american-macbook using german keyboard layout. shift+alt+8 will return this ˜ and alt+j returns º, which apparently is the masculine ordinal indicator – Max Oct 13 '15 at 13:49

With xelatex the following command definition works out of the box. The siunitx approach is not restricted to xelatex of course.

\documentclass{article}

%\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}
\usepackage{siunitx}

\newcommand{\°}{\si{\degree}}

\begin{document}
30\° or \SI{30}{\degree}
\end{document}

• I have no idea what \usepackage[T1]{fontenc} and \usepackage[utf8]{inputenc} do, but i have to use them, because my institute says so. Can you provide a solution, that works with these and PDFLaTeX? – Max Oct 13 '15 at 9:45
• @Max: Nope, \° does not work with utf8 (see Ulrike's answer). You could have written the restriction to inputenc etc. in your post, now my efforts are useless :-( – user31729 Oct 13 '15 at 9:47
• well sorry, i couldn't know, that those are so important, so i didn't actually mention it explicitly. – Max Oct 13 '15 at 9:52

You can't define \º as º is not a single symbol in utf8. But you can redefine the output of º

\documentclass{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{newunicodechar}
\newunicodechar{º}{XXXX}
\begin{document}
30º
\end{document}


## Edit

And to show that it works with siunitx too:

\documentclass{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{newunicodechar}
\usepackage{siunitx}
\newunicodechar{º}{\si{\degree}}
\begin{document}
30º
\end{document}


• this actually looks like the solution, but doesn't work unfortunatly. If i use it latex will still output a plain 30 instead of 30º – Max Oct 13 '15 at 9:51
• My example works. Did you try it exactly as it is? – Ulrike Fischer Oct 13 '15 at 9:53
• no, i replaced the XXXX with \degree and added \usepackage{siunitx} as you mentioned it before. – Max Oct 13 '15 at 9:57
• You should really learn how to ask questions. Do you think it is polite to change an answer and then claim that it doesn't work without showing what you did differently?. \newunicodechar{º}{\si{\degree}} should work fine if you load siunitx. – Ulrike Fischer Oct 13 '15 at 10:01
• Yes sorry, my bad! Nevertheless the code still doesn't work after changing it. – Max Oct 13 '15 at 10:14

The \textdegree command is available in TS1 encoding without using math mode so:

\documentclass[12pt, a4paper, twoside, %openright,
toc=listof, BCOR=5mm, bibliography=totoc, parskip=half]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[utf8]{inputenc}
%\newcommand{\º}{$^\circ$}

\begin{document}
30º this will give me the output shown in the image.

30° this will give me thhis.

command names may only use ascii characters, so you can not have a command \verb|\º| in classic latex.

\end{document}


Using a different approach, I have found that

\usepackage{gensymb}


generates a nice degree symbol, all you have to type in the file is

\degree

• This doesn't answer the question, which is to "automatically replace a Unicode character with a macro." – Werner Oct 13 '15 at 14:47
• I know, but I thought it might be a simpler solution in some cases. – nicolejane33 Oct 15 '15 at 7:12