In his answer michal.h21 has identified the problem and found the problem in the way, UTF-8 characters are written, if \usepackage[utf8]{inputenc}
is used:
\IeC{<LICR>}% LICR = LaTeX Internal Character Representation
Since package inputenc
makes the 8-bit bytes active, they can be redefined to print themselves instead of the \IeC
stuff.
Also \index
uses verbatim category codes for its argument. LaTeX does not include 8-bit characters in its verbatim category codes, because it has to map the UTF-8 byte sequence to a character slot of a font encoding to get the correct character.
The following example, based on michal.h21's example, patches \index
that it does not write expanded UTF-8 characters:
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{makeidx}
\makeindex
\usepackage{etoolbox}
\makeatletter
\patchcmd\index{\@sanitize}{\@sanitize\index@sanitize}{}{%
\errmessage{Patching \noexpand\index failed}%
}
\let\index@sanitize\@empty
\begingroup
\count@=127
\@whilenum\count@<255 \do{%
\advance\count@\@ne
\lccode`\*=\count@
\lccode`\~=\count@
\lowercase{%
\expandafter
\g@addto@macro\expandafter\index@sanitize\expandafter{%
% verbatim catcode
\expandafter\@makeother\csname *\endcsname
% active character expands to non-expandable itself
\def~{*}%
}%
}%
}
\endgroup
\makeatother
\begin{document}
This kind of index in text:
\index{Säker plats|textbf}Säker plats
foo\index{Aäö}\index{Aäö}
bar\index{äö}\index{aø}\index{ohne}\index{øæ}
\textbf{foo\index{Aäö}\index{Aäö}}
\printindex
\end{document}
The following .idx
file is written:
\indexentry{Säker plats|textbf}{1}
\indexentry{Aäö}{1}
\indexentry{Aäö}{1}
\indexentry{äö}{1}
\indexentry{aø}{1}
\indexentry{ohne}{1}
\indexentry{øæ}{1}
\indexentry{Aäö}{1}
\indexentry{Aäö}{1}