How to make LaTeX (teTeX) handle unicode and CJK (in MacOS X)
The latest version of LaTeX (TexLive 2013 as of this writing) includes full support for unicode. You just need to use a few packages and macros and it will work. If you are running an older version, however, you may have to do a few tricks. They are included here for legacy purposes.
TeXLive 2013
here is what you can do:
\documentclass{article}
\usepackage{CJKutf8}
\usepackage{ucs}
\usepackage[encapsulated]{CJK}
\newcommand{\myfont}{bsmi} % or {stheiti}, etc
\begin{document}
\begin{CJK}{UTF8}{\myfont}
Your CJK text here
\end{CJK}
\end{document}
Before TeXLive 2013 (or 2011)
If you have an older version of TeX distribution that does not support
Unicode or CJK, I recommend XeTeX, which is more robust than many other
hacks, including some in the second half of this page. I would not recommend
the hacks unless (1) you have no choice but to run an even older version of
LaTex that needs to be retrofitted (2) you need to typeset CJK text
vertically. In these cases, you can follow the instructions below after the
XeTeX section.
XeTeX
XeTeX works with unicode and the PDF fonts on your system. If you use one of the existing setups, it is usually pretty easy, but you need to define the font name that is available on your system.
\usepackage{fontspec}
\usepackage{xeCJK}
- Depending on your platform, you need to set the main font names. For Roman text, you can do
- \setmainfont{Times}
- For CJK Text, you should pick a font name that is present on your system. This may lose cross-platform compatiblity but is easy to fix.
- For Windows, you can use, for example,
\setCJKmainfont{標楷體}
% for brush stroke on Windows - For MacOSX, the equivalent one would be
\setCJKmainfont{BiauKai}
% for brush stroke on MacOSX - Note: if you use STKaiti, some of your traditional characters (e.g., 角) might look wrong (i.e, with a 用 below)
- For Windows, you can use, for example,
- For line breaking, do
\XeTeXlinebreaklocale "zh"\XeTeXlinebreakskip = 0pt plus 1pt
(pdf)LaTeX
This document assumes you know what LaTeX, pdflatex, etc. are, and want to typeset non-Roman text by hacking your existing installation without XeTeX. Another possible use is if you want to typeset CJK text vertically, XeTeX currently does not quite work, but the hacks here supports the CJKvert package. The steps here works on MacOSX and should work for other platforms, though I have no tested them.
- Installation of UCS Package
- Download “ucs” package from :
http://www.unruh.de/DniQ/latex/unicode/unicode.tgz
(actually you’ll get unicode.gtar)
- uncompress/untar the file
- move the whole “ucs” directory to an appropriate directory on your local
TeX-tree (I move it to the path
I-Installer:
/usr/local/teTeX/share/texmf.local/tex/latex/ucs,
orFink:
/sw/share/texmf-local/tex/latex/ucs/) - Pai’s note: most of the time you need to sudo the
mkdir
and
mv
command, if part of the path does not exist. for example, if you don’t have a “latex” directory along the path, then you may have to do this:
%
sudo mkdir
/usr/local/teTeX/share/texmf.local/tex/latex%
sudo mv
ucs /usr/local/teTeX/share/texmf.local/tex/latex
- Download “ucs” package from :
- Installation of CJK Package
- download “cjk” package from :
http://cjk.ffii.org/cjk-4.8.1.tar.gz
(previously, it was http://cjk.ffii.org/cjk-4.5.2.tar.gz)
- uncompress/untar the “cjk-4.8.1.tar.gz” file
- move the whole directory to an appropriate directory on your local TeX-tree (I move it to the path
I-Installer:
/usr/local/teTeX/share/texmf.local/tex/latex/cjk/
orFink:
/sw/share/texmf-local/tex/latex/cjk/)
- download “cjk” package from :
- Installation of Unicode True Type font (Cyberbit).
- download “Cyberbit.ZIP” file from :
ftp://ftp.netscape.com/pub/communicator/extras/fonts/windows/Cyberbit.ZIP
Actually there are several .zip files in that directory. uncompress them and you’ll get .ttf fonts. But I think all you need is just one.
- Rename
Cyberbit.ttf
to lower case (cyberbit.ttf) or else LaTeX might get confused because it is case sensitive.
- Rename
- Install the
.tfm
and
.enc
files.- There are two ways to get those files:
- Generate the file yourself:
- download “Unicode.sfd” file from :
- make sure “Cyberbit.ttf” file and “Unicode.sfd” files are in the same directory
- from the terminal, go to that directory and run
%
ttf2tfmcyberbit.ttf
-w cyberbit@Unicode@
, in order to create “.tfm” and “.enc” files.
- Download: If you cannot run
ttf2tfm
,
you can download the
.tfm
and
.enc
files that I generated.
- Generate the file yourself:
- move all the “.tfm” files to an appropriate directory on your local TeX-tree (I put them in I-
Installer:
/usr/local/teTeX/share/texmf.local/fonts/tfm/bitstream/cyberbit/,
orFink:
/sw/share/texmf-local/fonts/tfm/bitstream/cyberbit/) - move all the “.enc” files to an appropriate directory on your local
TeX-tree (I put them in
I-Installer:
/usr/local/teTeX/share/texmf.local/pdftex/enc/;
orFink:
/sw/share/texmf-dist/fonts/enc/pdftex/)
- There are two ways to get those files:
- Install “cyberbit.map” file
- download from :
http://delloye.free.fr/cyberbit.map - move it to an appropriate directory on your local TeX-tree (I put it in
I-Installer:
/usr/local/teTeX/share/texmf.local/pdftex/config/;
orFink:
/sw/share/texmf-dist/fonts/map/pdftex/updmap/
- download from :
- Install “c70cyberbit.fd” (and optionally
.fdv)- file from :
http://delloye.free.fr/c70cyberbit.fd
Pai’s comment: If you want vertical typesetting, you need a file named
c70cyberbit.fdv.
— use this at your own risk! I basically pasted
c00bsmi.fdv
into the
c70cyberbit.fd
file to create it. - move it (or both) to an appropriate directory on your local TeX-tree (I put it in
I-Installer:
/usr/local/teTeX/share/texmf.local/tex/misc/,
orFink:
/sw/share/texmf-local/tex/misc/)
- file from :
- move the “Cyberbit.ttf” (should be renamed
cyberbit.ttf
— lower case!!) to an appropriate directory on your local TeX-tree(I put it in I-Installer:
/usr/local/teTeX/share/texmf.local/fonts/truetype/,
or Fink:
/sw/share/texmf-local/fonts/truetype/)Pai’s comment: The following should not be a concern, but if it doesn’t work:
(check in your
I-Installer:
/usr/local/teTeX/share/texmf/web2c/texmf.cnf;
orFink:
/sw/share/texmf/web2c/texmf.cnffile the “%TrueType outline fonts” line and the “TTFONTS” value to make sure it includes the path where your
cybebrbit.ttf
file is located.)
- download “Cyberbit.ZIP” file from :
- Declaration of the new font to pdfTeX
- open the “pdftex.cfg” file
(I-Installer:
/usr/local/teTeX/share/texmf.local/pdftex/config/pdftex.cfg;
orFink:
/sw/share/texmf-local/pdftex/config/pdftex.cfg
— though I think if you use Fink, you should use the alternative instruction in part
4.b
instead) and add this linemap +cyberbit.map
at the end of the file.
Note: You may have permission problems saving this file. If you know how to use the
vi
(or
pico
,
probably easier, or
emacs
)
text editor from the command line, your best bet is to
cd
into that directory and
%
sudo vi
pdftex.cfg
Otherwise, copy this file to a directory you can save, edit, and
sudo mv
it back to its directory. - If you are running TeXLive 2004, you might have to do the following step:
- (sudo) edit the file
/usr/local/teTeX/share/texmf.local/web2c/updmap.cfg- add the following line
Map cyberbit.map
to the end of the file, save;
If you are running Fink’s distribution, add
Map Cyberbit.map
to the file/sw/share/texmf-dist/web2c/updmap.cfg
- add the following line
- type the command
%
sudo updmap
This should re-read all the map files and generate the output files for ps2pk, dvips, pdftex, dvipdfm and run texhash for you. (so you can skip step 4.c)
- (sudo) edit the file
- Update the
ls-R
files by the command
%
sudo mktexlsr
- open the “pdftex.cfg” file
- Using the new environment in LaTeX
You just need to include the three following lines in the preamble :
\usepackage[encapsulated]{CJK}
\usepackage{ucs}
\usepackage[utf8]{inputenc}
(for
TexLive 2004, use
[utf8x]
instead of
[utf8])It may also be convenient to create a new command :
\newcommand{\cntext}[1]{\begin{CJK}{UTF8}{cyberbit}#1\end{CJK}}
Now, when you want to type CJK text, you just have to use the command :
\cntext{enter your CJK text here}
To do vertical typesetting, just do
\usepackage
{CJKvert}
and CJK characters will be rotated 90 degrees for the vertical orientation.
Pai’s comment: Make sure you use an editor such as TeXShop which can save in UTF-8 encoding. Vim also works if you do
:set encoding=utf-8
inside vim or in the file “.vimrc” (without colon).
The TrueType unicode font will be embedded for a very smooth looking PDF document.
%
pdflatex
yourfile.tex
Troubleshooting
- Q:
I get the error“mktexpk: don’t know how to create bitmap font for cyberbit59.” (or something like that)
what do I do?
- A:
It is most likely your
/usr/local/teTeX/share/texmf.local/pdftex/config/pdftex.cfgfile was not
updated
correctly, or your
cyberbit.map
file is not there. If you have another stale copy of
pdftex.cfg
in your current directory (or another directory searched before this one), then pdfTeX looks at it instead. To fix it, remove all stale copies of
pdftex.cfg.By the way, “pk” files are for bitmap fonts, but cyberbit is a TrueType font. This is an indication that your TrueType font file was not found. - Q:
I am running TeXLive 2004 but the truetype font can no longer be found, so it keeps trying to make pk font. How do I fix it? - A: You probably skipped the new step (4.b) above.
- Then, just run xelatex instead of pdflatex and it will generate the PDF file.