5

I have a document that takes text from source file via pandoc (that doesn't matter anyway) and inserts it instead of $body$. The source document contains the only one symbol that's shown in hex-editor as "C2 AD". As I discovered, it is a UTF-8 character "U+00AD" — soft hyphen. I have tried to handle it with \DeclareUnicodeCharacter command, just this way:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\DeclareUnicodeCharacter{00AD}{\-}

\begin{document}
$body$
\end{document}

but Xelatex still returned an error: "! Package inputenc Error: Keyboard character used is undefined (inputenc) in inputencoding `utf8'".

When I tried to use utf8x instead of utf8:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[T2A]{fontenc}
\DeclareUnicodeCharacter{00AD}{\-}

\begin{document}
$body$
\end{document}

it returned: "! LaTeX Error: Missing \begin{document}".

What I am doing wrong?

1

2 Answers 2

4

\usepackage[utf8]{inputenc} or \usepackage[utf8x]{inputenc} are needed for TeX engines that do not support UTF-8. Then TeX sees the two bytes C2 and AD and utf8.def or utf8x.def make C2 active to catch AD to print the symbol or execute \- or whatever.

In XeTeX, the bytes C2 AD become the "big" character AD. "Big" character means that character with character codes >= 256 are possible. Then you can make the character active and give it the meaning you requested:

% XeTeX or LuaTeX
\catcode`\^^ad=\active
\let^^ad=\-
0
5

I wouldn't recommend using utf8x; with the package option utf8x to either inputenc or inputenx, but it works also with XeLaTeX and LuaLaTeX, you can use newunicodechar:

\documentclass{article}

%%% Optional part if you want to use multiple engines
\usepackage{ifxetex,ifluatex}

\newif\ifxetexorluatex
\ifx\ifxetex\ifluatex % see http://tex.stackexchange.com/a/140164/
  \xetexorluatextrue
\fi

\ifxetexorluatex\else
  \usepackage[utf8]{inputenx}
\fi
%%% End of optional part

%%% Use just the following line if you only compile with pdflatex
%\usepackage[utf8]{inputenx}

\usepackage{newunicodechar}
\newunicodechar{­}{\-} % the first argument contains U+00AD
2
  • I wonder why this isn’t the default behaviour. At least with fontspec or xunicode.
    – bodo
    Feb 20, 2014 at 17:19
  • @canaaerus U+00AD is a character; the concept of soft hyphen is quite different for (Xe|Lua)TeX, where it is a particular node in the horizontal list.
    – egreg
    Feb 20, 2014 at 17:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .