# Fontspec/xeCJK AutoFakeBold and copyable Chinese characters in PDF

I'm writing a document in mixed English and Chinese (with the xeCJK package. I want to add some fake bold headlines using the font Kaiti SC. When I do this using the AutoFakeBold option, this makes it unable to copy the characters in the resulting PDF. How can I fix this?

It seems like xeCJK is loading fontspec behind the curtains, so the issue might be related to it.

Here's a MWE.

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[12pt, a4paper]{article}

\usepackage{xeCJK}
\setCJKmainfont[Scale=1.2, AutoFakeBold=true]{Kaiti SC} % removing the bold makes the output copyable

\begin{document}

\textbf{你好}

\end{document}


Malipivo provided a solution using \pdfliteral, and that technique works for XeTeX too. This technique is used in zhmCJK package. You can read the documented source code of zhmCJK if you are interested in it.

However, it is quite tricky to apply this technique properly in xeCJK:

% !TeX program = XeLaTeX
% !TeX encoding = UTF-8
\documentclass{article}
\usepackage{xeCJK}

\setCJKmainfont{SimSun}

% value > 0
\def\xeCJKembold{0.4}

% hack into xeCJK, you don't need to understand it
\def\saveCJKnode{\dimen255\lastkern}
\def\restoreCJKnode{\kern-\dimen255\kern\dimen255}

% save old definition of \CJKsymbol and \CJKpunctsymbol for CJK output
\let\CJKoldsymbol\CJKsymbol
\let\CJKoldpunctsymbol\CJKpunctsymbol

% apply pdf literal fake bold
\def\CJKfakeboldsymbol#1{%
\special{pdf:literal direct 2 Tr \xeCJKembold\space w}%
\CJKoldsymbol{#1}%
\saveCJKnode
\special{pdf:literal direct 0 Tr}%
\restoreCJKnode}
\def\CJKfakeboldpunctsymbol#1{%
\special{pdf:literal direct 2 Tr \xeCJKembold\space w}%
\CJKoldpunctsymbol{#1}%
\saveCJKnode
\special{pdf:literal direct 0 Tr}%
\restoreCJKnode}
\newcommand\CJKfakebold[1]{%
\let\CJKsymbol\CJKfakeboldsymbol
\let\CJKpunctsymbol\CJKfakeboldpunctsymbol
#1%
\let\CJKsymbol\CJKoldsymbol
\let\CJKpunctsymbol\CJKoldpunctsymbol}

\begin{document}

\end{document}


Note that the non-CJK characters are unchanged in \CJKfakebold command. This is a feature by design. You should use \textbf or \bfseries as usual for English.

And note that the code above is not at all complete for xeCJK, puctuation kerning is wrong for example. It should be implemented more carefully (maybe in a very different way) if we add this feature into the package.

• Interesting. I'm not making a professional document so the kerning is not so important. Is there a way to combine this with the AutoFakeBold setting or should I just adjust the \xeCJKembold value manually? – pg-robban May 26 '14 at 16:22
• @pg-robban: Well, I think you can set it manually. – Leo Liu May 26 '14 at 17:06
• Alright, I have one more question before I mark the question as answered: Suppose I have a macro which does something with bold text, currently I do this with \textbf{#1} on that part. Is there any way I can find out if the argument consists of only Chinese characters so that I can apply the correct bold function (\textbf vs \CJKfakebold) or do I need to make separate macros? – pg-robban May 26 '14 at 18:48
• @pg-robban: \textbf does not change Chinese characters, if you don't set BoldFont feature. And maybe you didn't notice that \CJKfakebold does not change Latin characters, as is shown above. You can use \CJKfakebold{\textbf{正确的粗体 Bold}} safely. – Leo Liu May 27 '14 at 10:45
• Aha, I got it now :) 麻烦您了，thank you so much. – pg-robban May 27 '14 at 13:51

This is a know XeTeX bug, I don’t think there is currently a way to fix it.