Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
This a starting point tip for you: tex.stackexchange.com/questions/24528/… –  Malipivo Apr 8 at 12:15

3 Answers 3

up vote 6 down vote accepted

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

share|improve this answer
    
You should specify which engine you used. Is it XeLaTeX? –  Jubobs Apr 8 at 13:20
    
Thank you very much! –  Scorpius Xia Apr 8 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 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}

enter image description here

share|improve this answer

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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