# Can I include Chinese characters in listings? If so, how?

I'd like to include Chinese characters in listings. Can it be done, and, if so, how?

-
This a starting point tip for you: tex.stackexchange.com/questions/24528/… – Malipivo Apr 8 '14 at 12:15

I enclose an example with a help of the listingsutf8 package and the CODE2000 font. I ran xelatex and lualatex, the result looks all the same.

%! {xe|lua}latex mal-cjkv.tex
\documentclass{article}
\pagestyle{empty}
\usepackage{listingsutf8}
\usepackage{fontspec}
\usepackage{filecontents}
\begin{filecontents*}{mal-code.tex}
My source code and Good bye! in Japanese:  さよなら。
A greeting in Chinese: 你怎么样?
\end{filecontents*}
\begin{document}
% http://web.archive.org/web/20101122142710/http://code2000.net/code2000_page.htm
\setmainfont{CODE2000.TTF}
\lstinputlisting{mal-code.tex}
\end{document}


-
You should specify which engine you used. Is it XeLaTeX? – Jubobs Apr 8 '14 at 13:20
Thank you very much! – Scorpius Xia Apr 8 '14 at 13:21
@Jubobs I hope I did that in shebang, I ran both engines (xelatex, lualatex), the output looks all the same. – Malipivo Apr 8 '14 at 13:25
\documentclass{article}
\usepackage{fontspec}
\usepackage{libertine}
\newfontfamily\cjk{Code2000}
\usepackage{fancyvrb}

\begin{document}
Some nonsense text with the  Libertine font.

\begin{Verbatim}[codes=\cjk,numbers=left,frame=single,label=Chinese test]
My source code and Good bye! in Japanese:  さよなら。
A greeting in Chinese: 你怎么样?
\end{Verbatim}

\end{document}


-

listings package is rather special, Malipivo's solution works wrong when

1. We use syntax highlighting;

2. We mix CJK characters and Latin characters.

Here is an example which shows the problem.

% !TeX program = XeLaTeX
% !TeX encoding = UTF-8
\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\usepackage{fontspec}
\setmainfont{FandolSong-Regular.otf}

\begin{document}

\section{Failure}
\begin{lstlisting}
// tomorrow明天
\end{lstlisting}

\end{document}


And here is the solution, using xeCJK:

% !TeX program = XeLaTeX
% !TeX encoding = UTF-8
\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\usepackage{xeCJK}
\setCJKmainfont{FandolSong-Regular.otf}

\begin{document}

\section{Success}
\begin{lstlisting}
// tomorrow明天
\end{lstlisting}

\end{document}

-