[Tex/LaTex] Switching LaTeX files from latin-1 to utf-8: “Keyboard character used is undefined”

input-encodingspdftex

My .tex and .sty files were in encoding latin-1. I've converted
them [using Emacs] to utf-8.

In my sty file, I replaced

\usepackage[latin1]{inputenc}

with

\usepackage[utf8]{inputenc}

:: The problem: My style file has commands

\newcommand{°}{\ensuremath{^\circ}}
\newcommand{½}{\ensuremath{\frac{1}{2}}}

and the like.

When I run command `pdflatex' from the command line, I
get this error message:

! Package inputenc Error: Keyboard character used is undefined
(inputenc)                in inputencoding `utf8'.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.86 \newcommand{°}{\ensuremath{^\circ}}

which is showing the character as

°

Is there a way to get these characters processed in
utf-8, or should I switch back to latin-1?

============
[I'm not sure the stackexchange method to Ulrike Fischer's
suggestion, so I'm editing my original question.

Alas, neither solution is working on my system [which is
"This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live
2010)"], or I may be mis-applying your suggestion.

Running `pdfTeX' on

\documentclass{article}

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

\DeclareUnicodeCharacter{00B0}{\ensuremath{^\circ}}

\begin{document}

Test: $ ½ $

\end{document}

Produces

This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live 2010)
 restricted \write18 enabled.
entering extended mode
(./de.testing.latex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2009-06-19, ngerman-x-2009-06-19, ancientgreek, ibycus, arabic, armenian
, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danish, dutch, u
kenglish, usenglishmax, esperanto, estonian, farsi, finnish, french, galician, 
german, ngerman, swissgerman, monogreek, greek, hungarian, icelandic, assamese,
 bengali, gujarati, hindi, kannada, malayalam, marathi, oriya, panjabi, tamil, 
telugu, indonesian, interlingua, irish, italian, kurmanji, lao, latin, latvian,
 lithuanian, mongolian, mongolianlmc, bokmal, nynorsk, polish, portuguese, roma
nian, russian, sanskrit, serbian, slovak, slovenian, spanish, swedish, turkish,
 turkmen, ukrainian, uppersorbian, welsh, loaded.
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/fontenc.sty
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/t1enc.def))
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/inputenc.sty
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/utf8.def
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/omsenc.dfu)))
(/Users/squash/Texdir/Pkgs/newunicodechar.sty) (./de.testing.aux)

! Package inputenc Error: Unicode char \u8:½ not set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.11 $½
        $
? 

This happens whether or not the "\usepackage[T1]{fontenc}"
is present, and whether or not the
"\usepackage{newunicodechar}" is present.

Best Answer

Ulrike's comment works like a charm, e.g. package newunicodechar:

\documentclass{article}

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

\newunicodechar{½}{\ensuremath{\frac{1}{2}}}

\begin{document}

Test: $ ½ $

\end{document}

Result

The hexadecimal Unicode number needs to be known without package newunicodechar:

\DeclareUnicodeCharacter{00BD}{\ensuremath{\frac{1}{2}}

The font encoding TS1, loaded by package textcomp contains a slot for this symbol and uses the text mode character \textonehalf:

\documentclass{article}

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

\begin{document}

Test: ½

\end{document}

Result