How to create Japanese language documents under GNU/Linux using LaTeX

Mark Alford. Last updated: 2011-Sep-27.

For inputting Japanese characters in Linux, see Viewing and creating Japanese documents under GNU/Linux.
If you just want to create a Japanese text file, see How to create Japanese language documents under GNU/Linux using emacs.

Typesetting of Chinese, Japanese, and Korean is available in LaTeX via the CJK package. These instructions work for the TeX Live distribution, which is part of major Linux distributions such as Fedora (starting at Fedora 9), Ubuntu, etc. For instructions appropriate to the older teTeX distribution, see the previous version of this document.

These are the steps that I followed to get Japanese. They work for CJK-4.8.2 (2008-12-29 version) under Fedora 9, 10, 11, 13.

I did everything as root, because I was adding CJK as a system-wide component, although I put it in a separate tree, /usr/local/share/texmf/, so that it is not mixed up with the main TeX installation and does not get clobbered when TeX is upgraded. But if the sysadmin has set up the TeX search paths correctly, you should be able to install CJK for your personal use by putting everything in $HOME/texmf/ and $HOME/bin/, without being root.

  1. Add Japanese language capability ("CJK") to LaTeX.

    1. At the UK TeX archive you can find CJK and CJK fonts. The latest version of CJK is also supposed to be available at this URL. The only font file you need for Japanese is the kanji one, which should be called something like "kanji48.tar.gz" and be found at this URL. In these instructions I will assume that CJK has been downloaded to ~/cjk-current/, and the kanji fonts to ~/CJK_fonts/kanji48.tar.gz.
    2. Copy the CJK tex input files to a place where tex will find them:
      > cp -r ~/cjk-current/texinput /usr/local/share/texmf/tex/latex/CJK
    3. Install hbf2gf in /usr/local/bin. Under Fedora 9, you will need to have the kpathsea and kpathsea-devel packages installed.
      > cd ~/cjk-current/utils/hbf2gf
      > ./configure --prefix=/usr/local/ \
                    --with-kpathsea-include=/usr/include \
      > make
      > make install
      The with-kpathsea-include dir is wherever kpathsea/kpathsea.h (note: not kpathsea.h) lives. The with-kpathsea-lib dir is wherever libkpathsea.a lives. (Use the locate command, eg locate libkpathsea.a). Make sure that the hbf2gf command is now accessible ("which hbf2gf").
    4. Install Japanese fonts in /usr/local/share/texmf/fonts/. How to do this depends which version of the file kanji48.tar.gz you have. Some versions unpack to give kanji48/texmf/..., and some (more conveniently) give texmf/.... The goal is to move all the stuff in texmf/... to /usr/local/share/texmf. For example,
      > tar zxf kanji48.tar.gz
      > mv  kanji48/texmf/* /usr/local/share/texmf
    5. Tell TeX where to find .hbf files, by modifying /usr/share/texmf/web2c/texmf.cnf:
      MISCFONTS = .;$TEXMF/fonts/misc//;$TEXMF/fonts/hbf//
      For non-root installation it may be possible to set this as an environment variable (see ~/cjk-current/doc/INSTALL and man hbf2gf).
      Important point: Here you have modified a system config file. If you later upgrade your TeX package or your operating system, this change may be overwritten, and you will have to go back and do it again. To avoid that, edit the first line of /usr/share/texmf/web2c/texmf.cnf, replacing the word "original" with something else.
    6. Update tex database
      > mktexlsr  # (same as "texhash")
    7. Now see if it works. Unfortunately, the example files given in recent versions of CJK require more fonts than the ones we have installed, so try my example, (japanese_template.cjk):
      > latex japanese_template.cjk

      You should see output indicating that it found the CJK files:

      Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
      (/usr/local/share/texmf/tex/latex/CJK/ruby.sty) (./japanese_template.aux)
      (/usr/local/share/texmf/tex/latex/CJK/JIS/c40song.fd) [1]
      (./japanese_template.aux) )
      Output written on japanese_template.dvi (1 page, 700 bytes).
      Transcript written on japanese_template.log.

      Now look at the results:

      > xdvi japanese_template

      You should see Japanese!

    8. Debugging:
      1. A useful command for checking the variable settings in your texmf.cnf is kpsewhich. Here are some examples of the output it should give.
        To see if your Japanese fonts are accessible,
        > kpsewhich jsso1202.tfm
        To check if MISCFONTS is set right:
        > kpsewhich -progname=hbf2gf -expand-var='$MISCFONTS'
        where "..." is various other paths in the texmf directory structure. Another check:
        > kpsewhich -progname=hbf2gf -format='misc fonts' kanji48.hbf
      2. You can obtain debugging output from kpathsea by setting the appropriate environment variable: "KPATHSEA_DEBUG=-1; export KPATHSEA_DEBUG" (or in csh, "setenv KPATHSEA_DEBUG -1") before running latex or xdvi or dvips. You will want to redirect output to a file, eg "xdvi japanese_template >& xdvi.log". Search the logfile output for "Couldn't find", "failed", "Error", etc, and then backtrack to see if it was looking in the right places.
  2. Use emacs (mule) to produce LaTeX files that include Japanese.

    If you want to do Japanese LaTeX then you will keep each file in two forms. There will be file.tex, which is encoded however you like (I will use Unicode, but emacs offers many others), and which you edit and work on. And there is file.cjk, which is JIS-encoded, and which can be LaTeXed.

    1. Install the necessary emacs lisp macros in /usr/local/share/emacs/site-lisp (create this dir if necessary).
      > cd ~/cjk-current/utils/lisp
      > cp cjktilde.el /usr/local/share/emacs/site-lisp/
      > cp emacs/cjk-enc.el /usr/local/share/emacs/site-lisp/
    2. Tell your emacs sessions where to find them. Put the following lines into your ~/.emacs file:
      (setq load-path (cons "/usr/local/share/emacs/site-lisp" load-path))
      (load-library "cjk-enc")
      (global-set-key "\C-c\C-w" 'cjk-write-file) ; CTRL C CTRL W writes CJK file
    3. You can get started by using this template latex file (annotated version). Do not try to use the templates in the CJK examples directory, since they require fonts that we haven't installed.
      %-*- coding: utf-8; current-input-method: japanese -*-
      \usepackage[overlap, CJK]{ruby}
      This is in English.
      これは 日本語 です。
      Note that we specify the coding and input method in a TeX comment on the first line. (Alternatively, this can be done by explicit commands to emacs.) When you load the file into emacs, it will automatically detect the coding and display the characters correctly.

    4. To modify the English and Japanese text in the LaTeX file, see How to create Japanese language documents under GNU/Linux using emacs
    5. To save the file, type CTRL-C CTRL-W (see the binding in your .emacs file above). This invokes 'cjk-write-file, which saves file.tex, and also creates the version file.ckj which can be LaTeXed.
      Note (2011-04-21): there is a bug in emacs-23 which causes 'cjk-write-file to produce a file with no Japanese characters in it. You need to downgrade to emacs-22. For Fedora, you need the Fedora 11 packages emacs-22.3-11.fc11.i586.rpm and emacs-common-22.3-11.fc11.i586.rpm.
    6. To latex the file,
      > latex file.cjk   # [NOT file.tex!]
      Then you can use xdvi, dvips, etc to view and print it.

For more general information on Japanese and computing, see Jim Breen's Japanese page.

Please sent comments, corrections, improvements to alford(at) Thanks to those who have done so, including Paul Wyatt of Toshiba and Andrew A. Adams of Reading University.

Mark Alford's IBM Thinkpad GNU/Linux page,

Valid XHTML 1.0!