4

Why does pdflatex (but not lualatex) write to toc-file \cyr-commands even with utf8 document encoding instead of normal cyrillic letters? How to prevent it? I interested in solution with redefining book.cls commands. I redefined the \addcontentsline{toc}{part}{\unexpanded\expandafter{{#1}}}% but it has no result.

% !TeX program = pdflatex
% !TeX encoding = utf8
% !TeX spellcheck = uk_UA
% !BIB program = bibtex8

\documentclass[12pt]{book}

\usepackage[a4paper]{geometry}

\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}
\usepackage[ukrainian]{babel}

\usepackage{indentfirst}
\usepackage{misccorr}

\makeatletter
\def\@part[#1]#2{%
    \thispagestyle{empty}
    \addcontentsline{toc}{part}{\unexpanded\expandafter{{#1}}}%
    {\centering
        \interlinepenalty \@M
        \normalfont
        \Huge \sffamily\bfseries #1\par}%
    \@endpart
}
\makeatother

\begin{document}
\tableofcontents
\part{Перша}

іва
\part{Друга}
іфавіфав
\end{document}

Toc looks like

\select@language {ukrainian}
\contentsline {part}{{\IeC {\CYRP }\IeC {\cyre }\IeC {\cyrr }\IeC {\cyrsh }\IeC {\cyra }}}{3}
\contentsline {part}{{\IeC {\CYRD }\IeC {\cyrr }\IeC {\cyru }\IeC {\cyrg }\IeC {\cyra }}}{7}
1
  • Note that \unexpanded\expandafter{{#1}} does nothing useful with the \expandafter, which tries to expand {. You can use \unexpanded{\unexpanded{#1}} instead, but I'm not sure why bother: the auxiliary files are not for being human readable.
    – egreg
    Mar 26 '16 at 18:19
2

You can try with \detokenize.

For use with hyperref: one needs both of \texorpdfstring and \phantomsection in the patch of \@part.

\documentclass[12pt]{book}

\usepackage[a4paper]{geometry}

\usepackage[T2A,T1]{fontenc}

\usepackage[utf8]{inputenc}

\usepackage[ukrainian]{babel}

\usepackage{indentfirst}
\usepackage{misccorr}

% update for hyperref compatibility

\makeatletter
\def\@part[#1]#2{%
    \thispagestyle{empty}%
    \phantomsection
    \addcontentsline{toc}{part}{\texorpdfstring{\detokenize{#1}}{#1}}%
    {\centering
        \interlinepenalty \@M
        \normalfont
        \Huge \sffamily\bfseries #1\par
    }%
    \@endpart
}
\makeatother

\usepackage[pdfencoding=auto]{hyperref}

\begin{document}
\tableofcontents
\part{Перша}

іва
\part{Друга}
іфавіфав
\end{document}

Contents of toc file:

\select@language {ukrainian}
\contentsline {part}{Перша}{3}
\contentsline {part}{Друга}{7}

and with hyperref:

\select@language {ukrainian}
\contentsline {part}{Перша}{3}{section*.2}
\contentsline {part}{Друга}{7}{section*.3}
6
  • thanx. But I got problem with hyperref package, the bookmarks now become unreadabl. Mar 26 '16 at 14:44
  • @sergiokapone see update.
    – user4686
    Mar 26 '16 at 15:01
  • @sergiokapone there is a problem with hyperlinks, though.
    – user4686
    Mar 26 '16 at 15:19
  • with your updates it works fine Mar 26 '16 at 15:23
  • I needed to add \phantomsection to your \@part patch, else the hyperlinks either from bookmarks of from TOC did not work with me.
    – user4686
    Mar 26 '16 at 15:35
2

In principle at least different parts of the document can be using different input encodings so the mechanism was designed to always write auxiliary files in the encoding neutral LICR ascii markup.

In practice, especially of you are using UTF-8, a single encoding is used for the whole document, but UTF-8, and UTF-8 support on LaTeX are some years younger than the design of the encoding support for toc files.

2
  • As I understand you, there is no solution due to restrictions of pdaflatex? Mar 26 '16 at 13:08
  • 1
    @sergiokapone no, you could write out and read back in utf8 but you'd want to disable almost all of the inputenc mechanism which is designed to convert input into the markup form, which can then be used for file writing or different font encodings. So to do a proper job of it it is a bigger change than is worth doing for a toc file which is just an internal file. It is more important to be able to write utf8 when making terminal output (or pdf bookmarks) where tex macro syntax is less than ideal. But your question asked why is it that way, so I addressed the "why" not how to change it. Mar 26 '16 at 13:17

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.