2013-10-20

UTF8 encoded Japanese in LaTeX

Slightly off topic, but anyway... notes on getting Japanese text working in LaTeX under Mac OS X using TeX Live. Once I finally got it to work it is quite easy, but first I explored a lot of dead ends and distractions (in the end I could ignore LaTeX Omega, XeLaTeX, etc). I'm just using pdflatex with the LaTeX Chinese, Japanese, Korean (CJK) package, here's an example from the PDF output:




Using Unicode UTF8 in LaTeX


Here's the LaTeX source to the example using kanji, hiragana and katakana, and a choice of three fonts:

% !TEX encoding = UTF-8 Unicode
\documentclass{letter}
\usepackage{CJKutf8}
\begin{document}

The following three Japanese fonts should work:

\begin{CJK}{UTF8}{min}
明朝 Mincho (\texttt{min}), \\
e.g. 私の名前はピーターです。
\end{CJK}

\begin{CJK}{UTF8}{goth}
ゴシック Gothic (\texttt{goth}), \\
e.g. 私の名前はピーターです。
\end{CJK}

\begin{CJK}{UTF8}{maru}
丸ゴシック Maru Gothic (\texttt{maru}), \\
e.g. 私の名前はピーターです。
\end{CJK}

\end{document}

This just says the font names and repeats the phrase "watashi no namae wa pi-ta- desu" ("My name is Peter"). As long as you have configured Japanese input within Mac OS X, then you can just type the Japanese text into your LaTeX file as normal.

Assuming the relevant packages are installed (see below) compile it as usual, via the LaTeX button within TeXShop, or in the terminal:

$ pdflatex example.tex
...
Output written on example.pdf (1 page, 63529 bytes).
Transcript written on example.log.

The special encoding line at the start can be inserted within TeXShop via the menu "Macros", "Encoding", "UTF-8 Unicode". However, it does not seem to be essential.


Notice that you can use normal English characters inside the CJK environment, which is perfect for bits of English. If you are mostly writing in Japanese with occasional English, it seems quite natural to use a single CJK environment for the entire document.


LaTeX Package Installation


I'm using TexShop as my LaTeX editor on Mac OS X 10.8 Mountain Lion, specifically I am currently using v3.25 which also supports Lion and the imminent update Mavericks. On my MacBook Air due to the small 64GB SSD, I'm not using the standard all inclusive Tex Live setup, but instead the smaller BasicTeX package (i.e. mactex-basic.pkg).

Here's how to update your TeX/LaTeX system using the TeX Live Manager (tlmgr) command line tool:

$ sudo tlmgr update -self -all

Since I did not install the full TeX Live, every so often I find I am missing a useful package. For instance, if a LaTeX file fails to compile because it is missing fullpage.sty, first find out which package provides that file, then install it:

$ tlmgr search --global --file fullpage.sty
tlmgr: package repository http://mirror.ox.ac.uk/sites/ctan.org/systems/texlive/tlnet
preprint:
texmf-dist/tex/latex/preprint/fullpage.sty

You need admin rights to actually install the package:

$ sudo tlmgr install preprint

Notice this automatically picked a local mirror,

$ tlmgr repository list
List of repositories (with tags if set):
http://mirror.ctan.org/systems/texlive/tlnet (main)

For this Japanese text example I had to install the LaTeX Chinese, Japanese, Korean (CJK) package:

$ sudo tlmgr install cjk

No comments:

Post a Comment