107

Can anyone describe how one can type Chinese in LaTeX? When I compile this:

\documentclass{article}
\begin{document}
 你好吗?
\end{document}

the resulting document shows nothing. For reference my IME is iBus 1.3.9 on Fedora 13.

Paweł
  • 23
  • 5
805801
  • 1,391

7 Answers7

111

The easiest way is (for Simplified Chinese document only):

% UTF-8 encoding
% Compile with latex+dvipdfmx, pdflatex, xelatex or lualatex
% XeLaTeX is recommanded
\documentclass[UTF8]{ctexart}
\begin{document}
文章内容。
\end{document}

or

\documentclass{article}
\usepackage[UTF8]{ctex}
...

It is designed for Chinese typesetting. Font sizes, indentation, name translation, line spacing, ... everything is set.

For the latest version of ctex bundle (v2.x), XeLaTeX is well tested and supports Windows/Mac/Linux. The proper fonts preinstalled in the OS should be selected automatically.


If you just want to typeset only a few Chinese characters, you can use CJK with pdfLaTeX or xeCJK with XeLaTeX.

% Compile with xelatex
% UTF-8 encoding
\documentclass{article}
\usepackage{xeCJK}
\setCJKmainfont{SimSun}
\begin{document}
文章内容
\end{document}

or

% UTF-8 encoding, pdflatex or latex+dvipdfmx
% Simplified Chinese fonts should be installed
\documentclass{article}
\usepackage{CJKutf8}
\AtBeginDvi{\input{zhwinfonts}}
\begin{document}
\begin{CJK*}{UTF8}{zhsong}
文章内容。
\clearpage\end{CJK*}
\end{document}

or

% UTF-8 encoding
% bad-looking fonts (CJKfonts package)
% latex+dvips, latex+dvipdfm(x) or pdflatex
\documentclass{article}
\usepackage{CJKutf8}
\begin{document}
\begin{CJK*}{UTF8}{gbsn}
文章内容。
\clearpage\end{CJK*}
\end{document}
ViHdzP
  • 123
Leo Liu
  • 77,365
  • 18
    Note: I am one of the developers of ctex bundle and xeCJK. – Leo Liu May 07 '11 at 03:06
  • I am lucky to find your answer. Thank you very much for the examples! I tried the second example on TeXnicCenter with MikTeX 2.9, it works great with all of the other packages I have been using. I do have two more questions: 1. I can only change fonts to {UTF8}{zhsong},{UTF8}{zhhei},{UTF8}{zhkai} and {UTF8}{zhfs}, but how do I use zhli or zhiyou? 2. Is there a way I can use WinEdt, which still does not support UTF yet? – ltxsun Jan 22 '12 at 05:26
  • @ltxsun: 1. zhli and zhyou are available in zhmetrics bundle. However, make sure you have install MS Office to have the two necessary actual fonts (LiSu and YouYuan). 2. WinEdt 5.5 and later partial supports UTF8 encoding if you have Chinese locale, and furthermore the latest WinEdt (7.0 test version, unpublished) supports full two bytes Unicode. – Leo Liu Jan 29 '12 at 16:06
  • Thanks, Leo. I guess I don't have those two fonts then. I searched my installations, either Office XP or Office 2007, and did not find simyou.ttf. – ltxsun Feb 03 '12 at 16:18
  • @ltxsun: A full installation does have those fonts. You can also find more information on Microsoft's website to check which version you should have. – Leo Liu Feb 03 '12 at 21:31
  • Thanks, Leo. I found that the font files simyou.ttf and simli.ttf on my Mac can be installed to my Windows 7 (but not sure if there is a copyright issue). After that, I can use zhyou and zhli just as any other fonts. If it were not your tip, I would have not known I had those fonts missing. May I ask you another question? How do I typeset Chinese in section title? I have been following your last second example with zhwinfonts and article class. And, what about verbatim, e.g., Chinese characters in a listing environment by package listings? – ltxsun Feb 04 '12 at 05:28
  • 1
    @ltxsun: I make a mistake, zhli and zhyou are in zhmetrics bundle. Please don't leave more comments here. You may ask a new question for that. And I think it is much better to ask at bbs.ctex.org for pure chinese questions. – Leo Liu Feb 04 '12 at 07:59
  • Anyone knows how to do this with beamer? The CJKutf8 package seems to have a conflict there (working under ubuntu), –  May 15 '12 at 01:24
  • @Franknij: In beamer everything is similar. Both CJKutf8 and xeCJK works for me. – Leo Liu May 15 '12 at 10:03
  • 1
    Great. I am using xeCJK now. But there is an indentation of paragraphs at the beginning of a section. I tried to read the manual but it is in chinese. I found an option, but this option does not seem to do the trick. – Stefan Müller Feb 16 '13 at 09:03
  • 1
    @StefanMüller: indentfirst=false would fix this. I'm sorry but my English is poor and I don't have much time to translate the manual now. – Leo Liu Feb 20 '13 at 14:00
  • Worked well for me on Ubuntu after running sudo apt-get install ttf-wqy-zenhei and using \setCJKmainfont{WenQuanYi Zen Hei}. Thanks! – Joe Corneli Jul 09 '13 at 18:54
  • Can you explain how to install ctexart? Is it included in package ctex? I am a newbie using TexMaker and MikTex on Linux. – Yan King Yin Mar 08 '15 at 17:27
  • @YanKingYin: ctexart is a part of the ctex bundle. It should have been installed if you have installed ctex bundle, which can be managed by the Package Manager of MiKTeX. – Leo Liu Mar 08 '15 at 17:45
  • Thanks, I succeeded with: sudo apt-get install texlive-lang-cjk and then manually install the fonts simhei.ttf and simsun.ttf – Yan King Yin Mar 08 '15 at 19:01
  • The CJK package also works in a similar way as CJKutf8 compiled with PDFLaTeX. – Xiaodong Qi Apr 29 '17 at 21:29
  • @Leo Liu, I tried the second example for the "just want to typeset only a few Chinese charecter" case, but what I got is mess code. http://imgur.com/a/yMMuz Really need your help. please also see my post https://tex.stackexchange.com/questions/385586/quote-a-dictum-in-chinese-in-a-new-page-before-my-thesis-in-english. I use gusbrs' answer, also gives me mess code... – jsxs Aug 10 '17 at 01:44
  • @Leo Liu I tried the second example for the "just want to typeset only a few Chinese character" case, but it gives me a blank page. I use XeLaTeX – jsxs Aug 12 '17 at 08:58
  • I tried the first two examples but I get error "! Critical Package ctex Error: CTeX fontset `fandol' is unavailable in current (ctex) mode." I'm running texmaker on macOS catalina. Can someone point me to the fix for this? – liyuan Apr 11 '20 at 01:20
20

I use Chinese under XeLaTeX and the XeCJK package, which allow the use of CJK together with your own fonts. It's extremely convenient.

First, take a look at the explanation at Chou Pai-hsiang's website. This should get you started.

There are other important comments here on tex.SE about the use of fontspec and getting the full Chinese character set from two or more fonts, but you can worry about those matters later.

16

I recently ran into this on Fedora 16. You're seeing nothing due to not selecting the correct Chinese font, which is different on Windows, Mac and Linux.

The trick to finding the right font for a language on Linux is:

$ fc-list :lang=zh   

the default Chinese font on Fedora 16 is:

WenQuanYi Zen Hei,文泉驛正黑,文泉驿正黑:style=Regular

and on your Fedora 13 system it's likely to be:

AR PL UMing TW:style=Light
AR PL UMing CN:style=Light

(I'm from Taiwan so I use the Traditional Chinese variant ending in TW. You want "AR PL UMing CN" for simplified Chinese.) To install Chinese fonts etc on Fedora, run:

yum groupinstall 'Chinese Support'

Change your LaTeX source to:

\documentclass{article}
\setromanfont{AR PL UMing CN}
\begin{document}
你好吗?
\end{document}

and you should start seeing Chinese.

The above snippet was to help you diagnose the Chinese font problem. For real work I second the recommendation for the xeCJK package. My personal setup is documented here.

The inability to typeset the same Chinese, Japanese etc LaTeX source across Windows, Mac and Linux due to them having different fonts is a real pain but is not specific to XeTex. It's painful when using latex + dvipdfmx or pdflatex as well.

scottt
  • 261
12

For ConTeXt users, this is simple.

First, use \mainlanguage[cn] and \setscript[hanzi] in the preamble to set the default language to Chinese. If you want only certain parts of the document to use Chinese, you can use \language[cn]{你好} within the document. If you want to place a little bit of English in the document, use \language[en]{hello} to ensure that the hyphenations appear correctly.

Next, the default fonts usually do not have Chinese characters, so you will need to create a typescript with Chinese fonts, then use \setupbodyfont to select that font. For details, see Getting started with Chinese in ConTeXt.

Village
  • 13,603
  • 23
  • 116
  • 219
2

Just a note for my specific use case: I wanted to use a few Chinese characters in my .bib file (using biber) as Unicode; to compile them like that, I could successfully use package {ctex} - unfortunately, it breaks some of my formatting in the document class I otherwise use. It turns out, the package {CJKutf8} doesn't break for me - but unlike {ctex}, it cannot read unicode chars directly - we have to wrap them in an environment, which is inconvenient to me for a .bib file.

So, since I have only few, I decided to declare unicode characters manually, to use {CJKutf8} to "render" the glyph. Say I want to render only "文章" from the examples above - so first I look up the unicode sequence for them , using my utfinfo.pl:

$ echo 文章 | perl utfinfo.pl
Got 2 uchars
Char: '文' u: 25991 [0x6587] b: 230,150,135 [0xE6,0x96,0x87] n: CJK UNIFIED IDEOGRAPH 6587 [CJK Unified Ideographs]
Char: '章' u: 31456 [0x7AE0] b: 231,171,160 [0xE7,0xAB,0xA0] n: CJK UNIFIED IDEOGRAPH 7AE0 [CJK Unified Ideographs]

knowing that, a working MWE (and compilable with pdflatex test.tex) could be constructed using the guidelines in [Cjk mailing list] Problem with CJKchar:

\documentclass{article}
\usepackage{CJKutf8}

% NOTE: this may require the font simsun.ttc in the same directory as this .tex file!

% [http://lists.ffii.org/pipermail/cjk/2007-November/002045.html [Cjk] Problem with CJKchar]
\newcommand{\Chi}[2]{%
  \csname CJK*\endcsname{UTF8}{zhsong}%
    \CJKchar{#1}{#2}%
  \csname endCJK*\endcsname
}

% 文: 0x6587
% \DeclareUnicodeCharacter{6587}{\begin{CJK*}{UTF8}{zhsong}文\end{CJK*}} % nope
% \DeclareUnicodeCharacter{6587}{\CJKfamily{zhsong}文} %                 % nope
% \DeclareUnicodeCharacter{6587}{\CJKchar[UTF8]{"65}{"87}}               % nope
% \DeclareUnicodeCharacter{6587}{\Unicode{"65}{"87}} %               % cant use
\DeclareUnicodeCharacter{6587}{\Chi{"65}{"87}} % YES!

% 章: 0x7AE0
\DeclareUnicodeCharacter{7AE0}{\Chi{"7A}{"E0}} %

\AtBeginDvi{\input{zhwinfonts}} % MUST have! for \usepackage{CJKutf8}

\begin{document}

% macro test:
\Chi{"65}{"87}\Chi{"7A}{"E0}    % ok

% direct unicode chars:
文章                             % ok
\end{document}

EDIT: also note in the actual doc with bibliography, I had to have at least one \Chi{... in the main body of document, else the bibliography part crashed; putting it in \phantom doesn't work, and I don't like inserting spurious characters in my doc. It turns out, instead of a \Chi, one can "start up" the CJKutf8 environment "manually", like so:

\makeatletter
\let\CJK@ignorespaces\relax % else the below breaks..
\CJK@envStart{}{UTF8}{zhsong}
\makeatother
\printbibliography
sdaau
  • 17,079
2

For Linux users:

  1. You need to install texlive-full to use "ctex" package:
sudo apt install texlive-full
  1. Change editor's compiler to XeLatex.

  2. Allow me to give you a very simple template:

\documentclass[a4paper,10pt]{article}
\usepackage{ctex} % 这里调用 ctex 包

%opening
\title{TITLE}
\author{MY NAME}

\begin{document}
\maketitle
\paragraph{现在应该可以看到中文了}
\end{document}
Carlos
  • 21
  • 1
-1

If the file compiled and the pdf built-in window popped up, only the Chinese characters missing out, here is the answer. The PDF file need to be "rendered" to display properly. In most cases the pdf rendering engine is poppler, no matter whater the built-in pdf reader is (okular, zathura). Rendering engine poppler needs poppler-data (rendering library) to render some language characters correctly. Just install it!

enter image description here

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Sep 26 '22 at 11:09