源代码
\documentclass[a4paper,10pt]{ctexart} % \usepackage{xeCJK} \usepackage{fancybox} \usepackage{listings} % \usepackage[]{fontspec} % \lstset{ % language=C, % numbers=left, % numberstyle=\tiny, % basicstyle=\small\ttfamily, % identifierstyle=\color{blue}\itshape, % stringstyle=\color{purple}, % keywordstyle=\color{red}\bfseries, % commentstyle=\itshape, % directivestyle=\color{blue}, % frame=shadowbox, % %framerule=0pt, % %backgroundcolor=\color{pink}, % rulesepcolor=\color{red!20!green!20!blue!20}} % %rulesepcolor=\color{brown} % %xleftmargin=2em,xrightmargin=2em,aboveskip=1em \usepackage{xcolor} \usepackage{times} \begin{document} \section{Counting Numbers of Uppercase Characters} \begin{center} \begin{lstlisting}[language=C] /*=================================================== * FileName: counter.c * Author : Shaobin Li <shawpinlee@gmail.com> * Date : 2017-09-13 * Description: count numbers of uppercase * characters from input streams, and output * the numbers respectively. *=================================================*/ #include <stdio.h> #include <stdbool.h> #include <ctype.h> #define SIZE 26 int main (int argc, char *argv[]) { int array[SIZE]; int i; char c; for (i = 0; i < SIZE; i++) array[i] = 0; while ((c = getchar ()) != EOF) { if (isupper (c)) { array[c - 'A']++; } } for (i = 0; i < 26; i++) printf ("%c:%5d\n", (char) ('A' + i), array[i]); return 0; } \end{lstlisting} \end{center} \end{document}
xelatex编译警告
xelatex编译结果
pdflatex编译结果,这是期望得到的
然而这里又遇到了问题,当我在lstlisting环境下加入中文注释时,pdflatex不能编译,而xelatex确可以。下面是加入中文注释后的源代码
\documentclass[a4paper,10pt]{ctexart} % \usepackage{xeCJK} \usepackage{fancybox} \usepackage{listings} % \usepackage[]{fontspec} % \lstset{ % language=C, % numbers=left, % numberstyle=\tiny, % basicstyle=\small\ttfamily, % identifierstyle=\color{blue}\itshape, % stringstyle=\color{purple}, % keywordstyle=\color{red}\bfseries, % commentstyle=\itshape, % directivestyle=\color{blue}, % frame=shadowbox, % %framerule=0pt, % %backgroundcolor=\color{pink}, % rulesepcolor=\color{red!20!green!20!blue!20}} % %rulesepcolor=\color{brown} % %xleftmargin=2em,xrightmargin=2em,aboveskip=1em \usepackage{xcolor} \usepackage{times} \begin{document} \section{Counting Numbers of Uppercase Characters} \begin{center} \begin{lstlisting}[language=C] /*=================================================== * FileName: counter.c * Author : Shaobin Li <shawpinlee@gmail.com> * Date : 2017-09-13 * Description: count numbers of uppercase * characters from input streams, and output * the numbers respectively. *=================================================*/ #include <stdio.h> #include <stdbool.h> #include <ctype.h> #define SIZE 26 int main (int argc, char *argv[])//这是中文注释 { int array[SIZE]; int i; char c; for (i = 0; i < SIZE; i++) array[i] = 0; while ((c = getchar ()) != EOF) { if (isupper (c)) { array[c - 'A']++; } } for (i = 0; i < 26; i++) printf ("%c:%5d\n", (char) ('A' + i), array[i]); return 0; } \end{lstlisting} \end{center} \end{document}
pdflatex的编译错误提示
最后是MWE。其中的副本是加入中文注释后的