9

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

7

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}

mwe

|improve this answer|||||
  • You should specify which engine you used. Is it XeLaTeX? – jub0bs Apr 8 '14 at 13:20
  • @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
5
\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}

enter image description here

|improve this answer|||||
0

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.

enter image description here

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

\usepackage{xcolor}
\usepackage{listings}
\lstset{commentstyle=\color{blue},morecomment=[l]{//}}

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

\begin{document}

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

\end{document}

And here is the solution, using xeCJK:

enter image description here

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

\usepackage{xcolor}
\usepackage{listings}
\lstset{commentstyle=\color{blue},morecomment=[l]{//}}

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

\begin{document}

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

\end{document}
|improve this answer|||||

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.