How to typeset Japanese using (Xe)LaTeX

March 22, 2013 – tagged Howto, Japan

Typing Japanese on a Western keyboard is rather easy to setup, be it on Linux, Mac OS or Windows. One can usually type the Romaji version, such as “kyou”, and will obtain “きょう” if the input method is set up properly. Katakana/Kanji versions can usually be obtained by hitting the [Space] key multiple times, and small letters (e.g., “small tsu”) by prefixing them with “l” (lowercase “L”); for example, type “pa-teli-[SPACE]” to get “パーティー”. But how is it possible to typeset Japanese text with TeX?

Using XeTeX/XeLaTeX, which supports UTF-8 input encoding and OpenType fonts, it has become very easy to get Japanese text into your TeX documents – just write it into the document as described above. For example, a minimal document that can be compiled using xelatex looks as follows:

\setmainfont[Mapping=tex-text]{Kozuka Gothic Pro M}
hello = こんにちは

There is nothing special/Japanese-specific happening here, except that you have to select a font that contains all the characters you want to use. The font used above, “Kozuka Gothic Pro”, is from Adobe and can be downloaded for free, for example at Fontpark.

Now if you want to use TeX for anything serious, you will probably have more advanced requirements when typesetting Japanese, like:

  • vertical text,
  • different fonts for Western and Japanese sections,
  • proper alignment on a square grid,
  • Furigana (Hiragana above/below Kanji characters),
  • etc.

The following example combines information from (for xeCJK options) and (for general XeTeX information and vertical text in particular). It shows how to use a custom OTF font for regular (Western) text, a different OTF font for Japanese characters, and how to switch to vertical typesetting.

To whet your appetite, the result looks as follows:

An advanced example of typesetting Japanese with XeLaTeX

And here is the documented source code:


\usepackage{xltxtra, setspace}

%% fonts

% xeCJK options from <>:

% main font for CJK input from <>
\setCJKmainfont[Script=CJK]{Kozuka Gothic Pro M}
% \setCJKmainfont[Script=CJK]{Kozuka Mincho Pro}

% version of the above font with rotated characters for vertical text,
%  font needs to come with ``vrt2'' feature
\setCJKfamilyfont{cjk-vert}[Script=CJK,RawFeature=vertical]{Kozuka Gothic Pro M}
% \setCJKfamilyfont{cjk-vert}[Script=CJK,RawFeature=vertical]{Kozuka Mincho Pro}

% main font for non-CJK input from <>
\setmainfont[Mapping=tex-text, SmallCapsFont={Delicious SmallCaps}]{Delicious-Roman}

%% Furigana

% use \ruby{kanji}{kana} to set Furigana
% position of Furigana: below/right
% increase line space for Furigana

%% alignment

% also from <>:
\XeTeXlinebreaklocale "ja"
\XeTeXlinebreakskip=0em plus 0.1em minus 0.01em
% we also drop paragraph indentation in order to get proper alignment


\section*{Regular English Text}

This is \textbf{English} text and should be \textit{set} in Delicious
(or \textsc{Delicious SmallCaps}).

\section*{Regular Japanese Text}

きのう、ラースは \ruby{}{やま}\ruby{}{もと}を あいました。 
「こんにちは、\ruby{}{やま}\ruby{}{もと}さん。おげんき ですか。」

Note the nice alignment of the letters, in spite of the 「」 brackets.
(Some combinations of punctuation might break the alignment, though.)

\section*{Vertical Japanese Text}

\onehalfspacing % needs to be repeated here (?!)
きのう、\textbf{ラース}は \ruby{}{やま}\ruby{}{もと}を あいました。 
「こんにちは、\ruby{}{やま}\ruby{}{もと}さん。おげんき ですか。」

That's quite a nice alignment of the letters, in spite of the 「」 brackets
and the (artificially created) bold letters. (Some combinations of punctuation
might break the alignment, though.)