# high and low CJK codepoints in a single XeLaTeX document

I often require a combination of CJK characters with both low and high codepoints in the same line of text. (Here "low" means "in the Basic Multilingual Plane" or BMP, with codepoint lower than hex 10000.) I do not own a single typeface that contains all the CJK characters in forms I like, so I use two typefaces that look the same and together cover (apparently) all the CJK characters.

In a line of text containing both types of character, TeX requires explicit instructions as to which typeface to use: if a particular typeface does not contain a desired character, the only way to display the corresponding glyph is to specify a different typeface for that character.

My practice has been to assign my normal (BMP) font with \setCJKmainfont and then a secondary font, which contains all of the high-codepoint characters, with \setCJKsansfont. The nuisance is that in composing my document, I must manually assign the CJK sans font to the high-codepoint characters.

My question is whether there is a simple procedure that would choose the correct typeface, depending on which of them actually contains the required characters — without my manually changing the typeface.

-

We are sorry that xeCJK does not have this feature now. You can use zhspacing instead for this purpose. Maybe we'll add this feature to xeCJK in the future, but it requires quite a lot of code.

Using zhspacing:

\documentclass{article}
\usepackage{zhspacing}
\zhspacing
\newfontfamily\zhfont{SimSun}
\newfontfamily\zhcjkextbfont{SimSun-ExtB}
\begin{document}

% 𣴑 is \symbol{146075}
\end{document}


You can also use xeCJK with a simplified font switching syntax, if you do use xeCJK (in ctex, for example):

\documentclass[nofonts]{ctexart}
\setCJKmainfont{SimSun}

\setCJKfamilyfont{songextb}{SimSun-ExtB}
\catcode\"=\active
\def"#1"{\begingroup\CJKfamily{songextb}#1\endgroup}

\begin{document}

\end{document}


New solution, April 20:

\documentclass[nofonts]{ctexart}
\setCJKmainfont{SimSun}
\newfontfamily\songextb{SimSun-ExtB}
\def\CJKsymbol#1{%
\iffontchar\font#1%
#1%
\else
{\songextb#1}%
\fi}

\begin{document}

\end{document}


It is inspired by http://d.hatena.ne.jp/zrbabbler/20110220/1298209702 . You can also use zxjafbfont.sty directly.

I would add the feature to xeCJK using this method some time later, with better user interface.

May 4, 2011

Now new option and user command is available. (will be updated at CTAN soon)

\documentclass{article}

\usepackage[fallback]{xeCJK}[2011/05/01] % ver 2.3.19
\setCJKmainfont{SimSun}
\setCJKfallbackfamilyfont{rm}{SimSun-ExtB}
% NOTE: "rm" for \setCJKmainfont, "sf" for \setCJKsansfont, "tt" for \setCJKmonofont
%       and others for \setCJKfamilyfont.

\begin{document}

\end{document}

-
Thanks. I'm very grateful for everything that xeCJK has made possible for me, and will make do in the mean time. –  brannerchinese Apr 18 '11 at 16:57
Note: one may need to install a Unicode CJK ExtB font to display the source code in the browser and editor. –  Leo Liu Apr 18 '11 at 16:59
@Leo is it possible to use XeTeXintercharclass and XeTeXinterchartoks to do the switch? (Or is this the "lots of code" solution you have in mind?) –  Alan Munn Apr 18 '11 at 17:16
@Alan: xeCJK and zhspacing both use \XeTeXintercharclass and \XeTeXinterchartoks heavily. In xeCJK, there are already 7 different character classes. Thus it requires a lot of code to add a new class. –  Leo Liu Apr 18 '11 at 17:38
I add a fallback option to xeCJK v2.3.19, one can checkout latest version at code.google.com/p/ctex-kit/source/checkout before it is updated at CTAN. –  Leo Liu May 4 '11 at 5:29