3

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.

  • 4
    Use either the newunicodechar package or \DeclareUnicodeCharacter{00B0}{\ensuremath{^\circ}} (after inputenc). – Ulrike Fischer Sep 20 '17 at 16:36
  • Neither solution working on my system. I've edited my question putting in a minimal-example. – gentsquash Sep 22 '17 at 16:31
  • @UlrikeFischer's suggestion is to do \newunicodechar{°}{\ensuremath{^\circ}} and \newunicodechar{½}{\ensuremath{\frac{1}{2}}} and it does work. When you load a package you should give a quick look at its documentation: texdoc newunicodechar. – user4686 Sep 22 '17 at 21:37
  • Thanks jfbu. Running `texdoc newunicodechar' was the first thing I did. On my system, it gave: "Sorry, no documentation found for newunicodechar." (I did eventually get the doc by hand.) – gentsquash Sep 24 '17 at 1:42
1

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

  • Ah ha! I need to use \newunicodechar rather than \newcommand. Thank you. – gentsquash Sep 24 '17 at 1:39
0

Thank you Heiko and Ulrike. I indeed had already tried

\usepackage{newunicodechar}

without using \DeclareUnicodeCharacter, but I had kept the commands as they were, e.g

\newcommand{·}{\cdot}

This caused an error that was obscure to me. Replacing the latter with

\newunicodechar{·}{\cdot}

made more of my style-files load.

It turned out that while my former latin-1 setup accepted expressions such as

$ \boldsymbol· $

they now cause an error. My current UTF8 setup only works if I put in braces,

$ \boldsymbol{·} $

That is, it seems that when characters

¼  ±  ·

and the like, are passed as arguments to a macro, they must be in braces. Or perhaps I have mis-set-up something.

In any case, my files are compiling now after a bunch of global search-replacements. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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