# error on generating pdf by Latex with CJK support

After adding unicode characters to \title{} micro:

\title{标题}


the following error occurs when running make all-pdf with a .tex file:

Writing index file mybook.idx
No file mybook.aux.
(/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd)
(/usr/share/texmf-dist/tex/latex/psnfss/t1ptm.fd)
(/usr/share/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/share/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/share/texmf-dist/tex/latex/oberdiek/grfext.sty)
(/usr/share/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
(/usr/share/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
(/usr/share/texmf-dist/tex/latex/cjk/texinput/UTF8/UTF8.bdg)
(/usr/share/texmf-dist/tex/latex/cjk/texinput/UTF8/UTF8.enc)
(/usr/share/texmf-dist/tex/latex/cjk/texinput/UTF8/UTF8.chr)
! Extra \else.
\CJK@XXX ...\number #1\endcsname {#2}{#3}\else
\csname u8:\string #1\stri...
l.124 \tableofcontents

?


I have added the follow setting to the .tex file:

\hypersetup{unicode=true}
\usepackage{CJKutf8}
\AtBeginDocument{\begin{CJK}{UTF8}{gbsn}}
\AtEndDocument{\end{CJK}}


If I remove the unicode characters, and replace them with acsii characters, it works well. If anything I forgot to add to the .tex to achieve this?

the minimal .tex file to reproduce the error:

% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt,english]{sphinxmanual}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{00A0}{\nobreakspace}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{times}
\usepackage[Bjarne]{fncychap}
\usepackage{longtable}
\usepackage{sphinx}
\usepackage{multirow}

\usepackage{CJKutf8}
\AtBeginDocument{\begin{CJK}{UTF8}{gbsn}}
\AtEndDocument{\end{CJK}}

\title{title 中文}
\date{June 21, 2013}
\release{1.0.0}
\author{author}
\newcommand{\sphinxlogo}{}
\renewcommand{\releasename}{Release}
\makeindex

\makeatletter

\begin{document}

\maketitle
\tableofcontents

\printindex
\end{document}


the specific error message:

(/usr/share/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/share/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/share/texmf-dist/tex/latex/oberdiek/grfext.sty)
(/usr/share/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
(/usr/share/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
(./mybook.out) (./mybook.out)
(/usr/share/texmf-dist/tex/latex/cjk/texinput/UTF8/UTF8.bdg)
(/usr/share/texmf-dist/tex/latex/cjk/texinput/UTF8/UTF8.enc)
(/usr/share/texmf-dist/tex/latex/cjk/texinput/UTF8/UTF8.chr)
! Extra \else.
\CJK@XXX ...\number #1\endcsname {#2}{#3}\else
\csname u8:\string #1\stri...
l.32 \tableofcontents

?


if I remove the unicode characters from \title{title 中文} into \title{title}, it works well.

-
Please add a full minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Make sure to only include the code that is necessary to reproduce the problem and nothing else. –  doncherry Jun 20 '13 at 11:20
Alternatively, you may just add something like "\usepackage[slantfont,boldfont]{xeCJK} \usepackage{fontspec,xltxtra,xunicode} %\setCJKmainfont{Adobe Fangsong Std} ", and try to use xelatex to compile the tex file. –  Hongying Jun 21 '13 at 8:35

## 1 Answer

Quick fix:

\title{title \unexpanded{中文}}


Your problem is caused by an unusual definition of \maketitle in sphinxmanual class.

In the definition of \maketitle of sphinxmanual.cls, there is

\pdfinfo{
/Author (\@author)
/Title (\@title)
}


It's very dangerous since CJK chracters are actually macros and should not be put into pdf streams directly. So we can use eTeX's primitive \unexpanded to prevent the problem.

For more about Chinese typesetting, see How does one type Chinese in LaTeX? XeLaTeX can also solve the problem.

-
I use python-sphinx to generate the .tex file. Is there any fix for sphinx's conf.py to achieve this? I'd like to finish pdf generation in one step without generating .tex file firstly, then fixing it manually, finally generating .pdf. –  geeker Jun 21 '13 at 5:26
@geeker: Modify sphinxmanual.cls to add \unexpanded. –  Leo Liu Jun 21 '13 at 5:37