3

I successfully can compile with Chinese characters in my documents using CJKutf8. However it makes all my files, that use fancyhdr with pagestyle fanc to throw encoding related errors during compilation.

Minimal example to reproduce (assuming that cjk support is installed on the system and running):

\documentclass{article}
\usepackage{CJKutf8}

\usepackage{fancyhdr}               
%\pagestyle{fancy} % <---- uncomment to throw error in compile

\begin{document}

\begin{CJK*}{UTF8}{gbsn}

\section{前言}

\section{关于数学部分}
数学、中英文皆可以混排。You can intersperse math, Chinese and English (Latin script) without adding extra environments.

\end{CJK*}

\bigskip  %% Just some white space

You can also insert Latin text in your document

\bigskip  %% Just some white space

\begin{CJK*}{UTF8}{bsmi}
這是繁體中文。
\end{CJK*}

\end{document}

Example was taken from here and extended.

Uncommenting \pagestyle{fancy} produces the following error:

! PACKAGE INPUTENC ERROR: UNICODE CHAR Ņ� (U+5173)
(INPUTENC)                NOT SET UP FOR USE WITH LATEX.

SEE THE INPUTENC PACKAGE DOCUMENTATION FOR EXPLANATION.
Type  H <return>  for immediate help.

Now I don't want to use xetex or luatax as workarounds and I also have created some quite complex headers in my documents, which I'd like to keep.

So my question is, if there is a way to resolve this error for example by adding a specific package or configure fancyhdr correctly.

4

These days using luatex or xelatex for non latin scripts is probably recommended but you can re-assert the CJK processing so that it is in scope in the heading. Simpler to hide it in a macro so it is safe from expansion and uppercasing issues.

enter image description here

\documentclass{article}
\usepackage{CJKutf8}

\usepackage{fancyhdr}               
\pagestyle{fancy} % <---- uncomment to throw error in compile

\protected\def\zzzA{\begin{CJK*}{UTF8}{gbsn}前言\end{CJK*}}
\protected\def\zzzB{\begin{CJK*}{UTF8}{gbsn}关于数学部分\end{CJK*}}

\begin{document}

\begin{CJK*}{UTF8}{gbsn}

\section{\zzzA}

\section{\zzzB}
数学、中英文皆可以混排。You can intersperse math, Chinese and English (Latin script) without adding extra environments.

\end{CJK*}

\bigskip  %% Just some white space

You can also insert Latin text in your document

\bigskip  %% Just some white space

\begin{CJK*}{UTF8}{bsmi}
這是繁體中文。
\end{CJK*}

\end{document}
  • 1
    Great simple solution, the macro wrapping works without the need for additional packages. – Jankapunkt Mar 10 at 11:21
1

If you're free to use LuaLaTeX or XeLaTeX and have a suitable system font, i.e., one which has the requisite Chinese characters, you can dispense with switching between CJK* and "ordinary" environments.

The following example compiles equally well under both LuaLaTeX and XeLaTeX on my MacOS system, MacTeX2018, the fontspec package, and the SimSun system font.

enter image description here

\documentclass{article}
\usepackage{fontspec} % for "\setmainfont" macro
\setmainfont{SimSun}  % or some other suitable font
%\usepackage{CJKutf8}  % not needed

\usepackage{fancyhdr}               
\pagestyle{fancy} 

\setlength\parindent{0pt} % just for this example

\begin{document}

%\begin{CJK*}{UTF8}{gbsn} % not needed

\section{前言}

\section{关于数学部分}
数学、中英文皆可以混排。You can intersperse math, Chinese and English 
(Latin script) without adding extra environments.

%\end{CJK*} % not needed

\bigskip  %% Just some white space

You can also insert Latin text in your document

\bigskip  %% Just some white space

%\begin{CJK*}{UTF8}{bsmi} % not needed
這是繁體中文。
%\end{CJK*} % not needed

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.