[Tex/LaTex] CJK inside a math environment

cjkfontspecmath-modexetex

I'm trying to display CJK characters as part of math formulae in XeLaTeX, and it's not working well. I've tried a number of options, and all have failed:

  • Defining a \jp font command via fontspec and then using \text{\jp{character}}. This gives me an undefined control sequence error.
  • Using fontspec's \setmathfont. This also gives me an undefined control sequence error.
  • Using the unicode-math package with \setmathfont[range="4E00-"2FA1F]{font} – depending on the range, CJK characters either still display as the 'unavailable character' box or simply disappear

A minimal failing example:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{CMU Serif}
\newfontfamily\jp{TakaoPGothic}
\usepackage{unicode-math}
\setmathfont[range="96E2]{TakaoPGothic} % the code for this particular character
\setmathfont{CMU Serif}
\begin{document}

$離$
\jp{離}

\end{document}

This produces a missing-character box on the first line, and rightly outputs 離 on the second.

Is there any good way to do this?

Best Answer

Usually, you should not use unicode-math package to specify the font of non-predefined unicode block. The package is not intended to set CJK fonts for math formula. Moreover, you should not use CMU Serif as a math font, since it lacks most of the math glyphs and the opentype math table.

To typeset Japanese in XeTeX, it is preferred to use xeCJK package or zxjatype package based on xeCJK. See also How to write Japanese with LaTeX?.

Japanese in math formula is actually some text. In LaTeX, we use \text from amsmath package to typeset text in math. Say, you should use:

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec}
\setmainfont{CMU Serif}
\usepackage{xeCJK}
\setCJKmainfont{IPAexMincho} % or some other Japanese font
\begin{document}

$\text{離}$

離

\end{document}

It is also possible to use Japanese characters in math formulas without \text, if you enable CJKmath option of xeCJK:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{CMU Serif}
\usepackage{xeCJK}
\xeCJKsetup{CJKmath=true}
\setCJKmainfont{IPAexMincho} % or some other Japanese font
\begin{document}

$離$

離

\end{document}