# How to select a font for the main Chinese character and another font for its Furigana?

The objective is to make a list of Japanese vocabularies with Kanji stroke order for the main characters, its furigana (small kana characters placed above the main characters), and its meaning (in English).

I have KanjiStrokeOrders font installed in my machine. I download it from this site (click).

\documentclass[border=12pt,12pt,preview,varwidth]{standalone}

\usepackage{xcolor}
\usepackage{fontspec}
%\usepackage{xeCJK}
\usepackage{ruby}
\renewcommand\rubysep{.05ex}

\AtBeginDocument{\fontsize{20}{20}\selectfont}

\let\temp\ruby
\renewcommand\ruby[2]{\temp {\fontsize{60}{60}\fontspec{KanjiStrokeOrders}\selectfont#1}{#2}}

\def\mean#1{: \textcolor{red}{#1}}

\begin{document}
\begin{enumerate}
\item \ruby{会}{かい}\ruby{社}{しゃ}　\mean{company}
\item \ruby{朝}{あさ} \mean{morning}
\end{enumerate}
\end{document}


## without xeCJK

Without xeCJK the main characters appears but their furigana disappear.

## with xeCJK

With xeCJK the furiganas appear but the main characters no longer use KanjiStrokeOrder fonts.

## Question:

How should I select the font to achieve my objective mentioned above?

-
I use xelatex but I actually want to use pdflatex if possible. –  stalking is prohibited May 17 at 15:35
Without xecjk you must select a suitable font for your furigana too -- the default font selected by fontspec don't have this chars. With xecjk the problem is that xecjk automatically switch to a specific font when it encounter the first cjk-character. And so it overwrites your font selection. Imho you should stick to xelatex or lualatex. With pdflatex writing cjk is quite complicated -- and setting up cjk-fonts for pdflatex is quite difficult. –  Ulrike Fischer May 17 at 17:19
@UlrikeFischer: How do I know the name of font to be used for the furigana? –  stalking is prohibited May 17 at 17:26
Look at the answer of Malipivo. –  Ulrike Fischer May 17 at 18:15

This is my first experiment without xeCJK.

% run: xelatex mal-furigana.tex
\documentclass[border=12pt,12pt,preview,varwidth]{standalone}
\usepackage{xcolor}
\usepackage{fontspec}
\newfontfamily\kanji{KanjiStrokeOrders_v3.001.ttf}
\newfontfamily\furigana{FandolSong-Regular}
\usepackage{ruby}
\renewcommand\rubysep{.05ex}
\AtBeginDocument{\fontsize{20}{20}\selectfont}
\let\temp\ruby
\renewcommand\ruby[2]{\temp
{\fontsize{60}{60}\kanji#1}%
{\furigana#2}
}% end of \ruby...
\def\mean#1{: \textcolor{red}{#1}}
\begin{document}
\begin{enumerate}
\item \ruby{会}{かい}\ruby{社}{しゃ}　\mean{company}
\item \ruby{朝}{あさ} \mean{morning}
\end{enumerate}
\end{document}


Update 1: This is the experiment with xeCJK using a new font family.

% run: xelatex mal-furigana.tex
%\documentclass[border=12pt,12pt,preview,varwidth]{standalone}
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{xeCJK}
\usepackage{ruby}
\renewcommand\rubysep{.05ex}
\setCJKmainfont{KanjiStrokeOrders}
\newCJKfontfamily\furigana{FandolSong-Regular}
\AtBeginDocument{\fontsize{20}{20}\selectfont}
\let\temp\ruby
\renewcommand\ruby[2]{\temp
{\fontsize{60}{60}#1}%\selectfont\setmainfont{KanjiStrokeOrders}
{\fontsize{20}{20}\furigana#2}
}% end of \ruby...
\def\mean#1{: \textcolor{red}{#1}}
\begin{document}
\begin{enumerate}
\item \ruby{会}{かい}\ruby{社}{しゃ}　\mean{company}
\item \ruby{朝}{あさ} \mean{morning}
\end{enumerate}
\end{document}


Update 2: This is the promised version without xeCJK and ruby. I am placing furigana by means of tikz package. We can run xelatex and lualatex. Setting up a TTF/OTF font for pdflatex engine is possible (but we are limited to 256 glyphs per font file), but it's out-of-date these days. I enclose the code and its preview.

% run: xelatex or lualatex mal-furi-general.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\parindent=0pt
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{tikz}
\newfontfamily\kanji{KanjiStrokeOrders_v3.001.ttf}
\newfontfamily\furigana{FandolSong-Regular}
\def\mkanji{\fontsize{60}{60}\kanji}
\def\mfurigana{\fontsize{15}{15}\furigana}
\def\mruby#1#2{%
\begin{tikzpicture}[baseline=(temp.base)]
\node(temp){\mkanji#1};
\node[anchor=center,yshift=0.5ex]at(temp.north){\mfurigana#2};
\end{tikzpicture}%
}% End of \mruby...
\def\mean#1{ \textcolor{red}{#1}}
\newcount\malc \malc=0
\def\mterm{\par
\the\malc. %
}% End of \mterm...
\begin{document}
\fontsize{20}{20}\selectfont
\mterm \mruby{会}{かい}\mruby{社}{しゃ}\mean{company}
\mterm \mruby{朝}{あさ}\mean{morning}
\end{document}


-
what is the pros and cons of with and without xeCJK? –  stalking is prohibited May 17 at 18:35
I think it doesn't matter much in these small examples with several glyphs, but I am not the right person to answer your question. However, I believe we could even prepare a version without xeCJK and ruby. –  Malipivo May 17 at 18:39
Fandol-Song is actually a Chinese font, but we should use a Japanese font here. IPAMincho or IPAexMincho are suiltable Japanese fonts preinstlled in TeX Live. –  Leo Liu May 18 at 8:11
@LeoLiu Are you sure it's needed in this case (I'm using font used by the ruby package)? The kana/glyphs looks fine, I like them. That font is used for displaying furigana (hiragana+katakana) only, not for the kanjis. –  Malipivo May 18 at 8:25
@MoneyOrientedProgrammer: A document which contains one or two complete sentences will need xeCJK for proper line breaking; for longer documents automatical font switching via xeCJK makes life easier. But in your example xeCJK is not necessary and you can typeset the document a little faster. –  Leo Liu May 18 at 9:23

If you want to know how to select font for Chinese characters, you should know which font do you want to use for every character, and you need to know the basic functions of xeCJK if you use it.

## The fonts

In your example, you should to use a special font KanjiStrokeOrders for 漢字 (kanji), and use another Japanese font, which is NOT properly specified in your document, for 振り仮名 (furigana).

If you don't set a proper font, you cannot typeset any CJK characters at all, because the default Latin Modern fonts do not contain CJK glyphs.

In contrast, if you use v3.2.10 or later version of xeCJK, Chinese fonts Fandol will be loaded with a warning:

*************************************************
* xeCJK warning: "fandol"
*
* Fandol is being set as the default font for CJK text.
* Please make sure it has been properly installed.
*************************************************


That's why the furiganas appear with xeCJK in your document even if you didn't specify a Japanese font.

## Usage of xeCJK

The document of xeCJK v3.x is only available in Chinese, I'm sorry.

The main purposes of xeCJK are

• To select different fonts for CJK characters and Latin characters automatically;
• To get proper line breaking;
• To do punctation kerning.

The basic function is font switching, whose syntax is the same as fontspec package:

• \setCJKmainfont\setmainfont
• \setCJKsansfont\setsansfont
• \setCJKmonofont\setmonofont
• \newCJKfontfamily\newfontfamily
• \CJKfontspec\fontspec

## Solution

% !TeX encoding = UTF-8
% !TeX program = XeLaTeX
\documentclass[border=12pt,12pt,preview,varwidth]{standalone}

\usepackage{xcolor}

\usepackage{xeCJK}
\setCJKmainfont{ipaexm.ttf} % For furigana and other Japanese characters
\newCJKfontfamily\strokefont{KanjiStrokeOrders_v3.001.ttf} % For stroke order

\usepackage{ruby}
\renewcommand\rubysep{.05ex}
\renewcommand\rubysize{0.2}
\let\oldruby\ruby
\renewcommand\ruby[2]{{\fontsize{60}{60}\selectfont\oldruby{\strokefont#1}{#2}}}

\def\mean#1{: \textcolor{red}{#1}}

\AtBeginDocument{\fontsize{20}{20}\selectfont}

\begin{document}
\begin{enumerate}
\item \ruby{会}{かい}\ruby{社}{しゃ}　\mean{company}
\item \ruby{朝}{あさ} \mean{morning}
\end{enumerate}
\end{document}


-
How do you know the filename of the font you want to import? Is it necessary to know the filename rather than just the font name? I have no documentation to read about these questions. –  stalking is prohibited May 18 at 9:30
I'm the one to be blamed. You are fine in xelatex using font names and filenames, but when I'm using lualatex+fontspec I'm having difficulties to load a font using font name containing spaces. Therefore I tend to use filenames instead (it's easy to find the file among system fonts). –  Malipivo May 18 at 9:36
@MoneyOrientedProgrammer: I know that there is a ipaex Japanese font package installed in TeX distributions, so I can search it. You can access a font via its filename if it is installed in TeX distribution (e.g. TeX Live) and can be searched via kpsewhich library. You can access a font via is family name if it is installed in your OS and can be searched via fontconfig library (for Windows and Linux) or OS API (for Mac). For Windows and Linux, fc-list tells you the fonts can be used via family name. –  Leo Liu May 18 at 9:36
@MoneyOrientedProgrammer: This may help: How to get a list of all available (TTF-) Fonts with XeTeX? Actually you can also get the filename via fc-list. For LuaTeX, see How can I list fonts available to LuaTeX in ConTeXt (TeX Live 2013)? –  Leo Liu May 18 at 9:44
Thanks you. :-) –  stalking is prohibited May 18 at 9:45