# How can I use ucharclasses to change the font for a special script and then restore to what it was before?

I would like to change the font in XeLaTeX when I enter a new Unicode block with a certain Script (e.g. Devanagari for Hindi languages). The ucharclasses package seems to do exactly that.

However, when I leave the place where the different script is used and go back to what I had before (e.g. latin script) I loose the formatting (e.g. boldface) and the font size.

To illustrate, try this:

\documentclass[10pt,a5paper,DIV12,BCOR8.25mm,twoside,parskip=half]{scrreprt}

\usepackage[Devanagari]{ucharclasses}
\usepackage{xltxtra}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Liberation Serif}
\setsansfont[Mapping=tex-text]{Liberation Sans}
\setmonofont[Mapping=tex-text]{Liberation Mono}
\newfontfamily\hindifont{Siddhanta}
\setTransitionsFor{Devanagari}{\hindifont}{\rm}

\begin{document}
\tableofcontents
\section{A latin script section}
Some latin script
\section{Devanagari: ताजा धनिया के साथ अनायास and so on}
A mixture \textbf{ of normal text and ताजा धनिया के साथ अनायास Devanagari script} in bold
\section{Some more latin script}
Some latin script
\end{document}


The output looks like this:

The \setTransitionTo correctly picks up the Devanagari script and changes the font to Siddhanta. However, when I come back to latin script, the boldface is gone and the fontsize is increased. The table of contents line shows the same problem.

N.B. \setTransitionsFor{...}{...}{...} is what the documentation describes as \setTransitions. The doc seems to be wrong.

-
I don't know if this will solve your problem (and can't test right now, hence not an answer), but you should not use {\bf text}. Use \textbf{text} instead. For reasons as to why, see the l2tabu package. –  Roelof Spijker Oct 21 '11 at 16:01
@whlt3 Thanks for your advice. I've changed the question to use \textbf but that doesn't help. I also added some text to the section title to show the same problem appearing in the table of contents. –  kongo09 Oct 21 '11 at 16:07

You can try something like this (I used the ^^-notation because I don't have an utf8-editor here):

\documentclass[10pt,a5paper,DIV12,BCOR8.25mm,twoside,parskip=half]{scrreprt}
\usepackage[Devanagari]{ucharclasses}
\usepackage{fontspec}

\newfontfamily\hindifont{Arial Unicode MS}
\makeatletter
\setTransitionsFor{Devanagari}%
{\let\curfamily\f@family\let\curshape\f@shape\let\curseries\f@series\hindifont}
{\fontfamily{\curfamily}\fontshape{\curshape}\fontseries{\curseries}\selectfont}
\makeatother

\begin{document} \tableofcontents \section{A latin script section} Some latin script

\section{Devanagari: ^^^^0908 ^^^^0909 and so on} A mixture \textbf{ of normal text and ^^^^0908 ^^^^0909 Devanagari script} in bold
\section{Some more latin script} Some latin script

\end{document}

-
I bow my head and am deeply impressed. Works like a charm! –  kongo09 Oct 21 '11 at 16:55
Would you please add some explanation as to what you did here, for us who are font impaired? –  Yiannis Lazarides Oct 21 '11 at 17:14
@Yiannis: I simply stored the description of the actual font before switching to \hindifont and then called this font when quiting the devanangari block. –  Ulrike Fischer Oct 21 '11 at 17:20
@UlrikeFischer Thanks. –  Yiannis Lazarides Oct 21 '11 at 17:36
I think this actually doesn't work for mixtures of scripts, where you go from latin to script1 then to script2 and then back to latin. This can happen in Japanese, for example, that can go from kanji to katagana to hiragana and even latin script. –  kongo09 Oct 21 '11 at 20:49

I think it's easy:

\def\ResetTransitionTo#1{%
\XeTeXinterchartoks 255 \csname#1Class\endcsname{\relax}}
\setTransitionsFor{Devanagari}
{\begingroup\ResetTransitionTo{Devanagari}\sethindifont}
{\endgroup}


This is what we do in xeCJK. Note that we must reset the \XeTeXinterchartoks to make the group balance. It is a place that ucharclasses is not very well implemented.

Hmm, spaces between scripts are necessary here. It's very boring to define all transitions for all scripts in the document, if there are many.

-
Thanks a lot for your insight and help. –  kongo09 Oct 24 '11 at 8:44