listings 是专用于代码排版的 LaTeX宏包(及使用xltxtra进行中文支持)

listings 是专用于代码排版的 LaTeX
宏包,可对关键词、注释和字符串等使用不同的字体和颜色或颜色,也可以为代码添加边框、背景等风格。

1 基本用法

下面给出一份用于排版 C 语言 HelloWorld 程序代码的完整的 LaTeX 文档:

\documentclass{article}
\usepackage{listings}

\begin{document}

\begin{lstlisting}[language=C]
int main(int argc, char ** argv) { printf("Hello world!\n");

return 0; }
\end{lstlisting}

\end{document}

注意,要使用 listings 宏包提供的语法高亮,需要 xcolor 宏包支持。

4 添加边框

listings
宏包为代码边框提供了很多风格,大体可分为带有阴影的边框与圆角边框。这里仅仅给出一个阴影边框的示例,至于其它边框风格,可查阅
listings 宏包文档,里面给出了一些示例。

下面 LaTeX 源文档将为代码添加阴影边框,并将阴影设置为浅灰色:

\begin{lstlisting}[language={[ANSI]C}, keywordstyle=\color{blue!70}, commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox, rulesepcolor=\color{red!20!green!20!blue!20}]
int main(int argc, char ** argv) { printf("Hello world!\n");

return 0; }
\end{lstlisting}

5 添加行号

很多时候需要对文档中的代码进行解释,只有带有行号的代码才可以让解释更清晰,因为你只需要说第
x
行代码有什么作用即可。如果没有行号,那对读者而言就太残忍了,他们不得不从你的文字叙述中得知行号信息,然后去一行一行的查到相应代码行。

listings 宏包通过参数 numbers 来设定行号,该参数的值有两个,分别是
left 与
right,表示行号显示在代码的左侧还是右侧。下面为带有边框的代码添加行号,并设置行号字体为
\tiny:

\begin{lstlisting}[language={[ANSI]C}, numbers=left, numberstyle=\tiny, keywordstyle=\color{blue!70}, commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox, rulesepcolor=\color{red!20!green!20!blue!20}]
int main(int argc, char ** argv) { printf("Hello world!\n");

return 0; }
\end{lstlisting}

6 全局设置

上面所给的各个示例中,lstlisting
环境后面尾随了很多参数,要是每使用一次 lstlisting
环境就要设置这么多参数,那就没什么意思了。

可以使用 \lstset 命令在 LaTeX 源文档的导言区设定好 lstlisting
环境所用的公共参数,如下:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}

\lstset{numbers=left, numberstyle=\tiny,
keywordstyle=\color{blue!70},
commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox,
rulesepcolor=\color{red!20!green!20!blue!20} }

\begin{lstlisting}[language={[ANSI]C}]
int main(int argc, char ** argv) { printf("Hello world!\n");

return 0; }
\end{lstlisting}

\end{document}

7 显示中文

listings 宏包默认是不支持包含中文字串的代码显示的,但是可以使用
“逃逸” 字串来显示中文。

在 \lstset
命令中设置逃逸字串的开始符号与终止符号,推荐使用的符号是左引号,即
“ `”

\lstset{numbers=left, numberstyle=\tiny,
keywordstyle=\color{blue!70},
commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox,
rulesepcolor=\color{red!20!green!20!blue!20}, escapeinside=``}

……

\begin{lstlisting}[language={[ANSI]C}]
int main(int argc, char ** argv) { printf("`我爱中文`!\n");

return 0; }
\end{lstlisting}
另一种方法:
    \documentclass[11pt,a4paper]{article}
\usepackage{xltxtra}
\usepackage{fontspec}
\usepackage{float}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{color}
\usepackage[top=0.8in,bottom=0.8in,left=1.2in,right=0.6in]{geometry}
\lstset{
  language=[ANSI]c,
  basicstyle=\small,
  numbers=left,
  keywordstyle=\color{blue},
  numberstyle={\tiny\color{lightgray}},
  stepnumber=1, %行号会逐行往上递增
  numbersep=5pt,
  commentstyle=\small\color{red},
  backgroundcolor=\color[rgb]{0.95,1.0,1.0},
  showspaces=false,
  showtabs=false,
  frame=shadowbox, framexleftmargin=5mm, rulesepcolor=\color{red!20!green!20!blue!20!},
% frame=single,
%  TABframe=single,
  tabsize=4,
  breaklines=tr,
  extendedchars=false %这一条命令可以解决代码跨页时,章节标题,页眉等汉字不显示的问题
}


%字体
\setmainfont[BoldFont={SimHei},ItalicFont={KaiTi}]{SimSun}
\setsansfont[BoldFont=SimHei]{KaiTi}
\setmonofont{NSimSun}

%%设置中文
%中文断行
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt

\begin{document}
此页故意留作空白
\newpage
\subsection{LaTex汉字测试}
测试文件

\lstinputlisting{src/function.h}
\lstinputlisting{src/function.c}
\begin{lstlisting}
int main(int argc, char ** argv)
{
/* 测试汉字注释 */
printf("Hello world!\n");

return 0;
}
\end{lstlisting}

\end{document}


8 调整一下边距

listings
的代码框的宽度默认是与页芯等宽的,其上边距也过于小,可根据自己的审美观念适度调整一下。我通常是将代码框的左右边距设置为
2em,上边距为 1em,下边距采用默认值即可,所作设定如下:

\lstset{numbers=left, numberstyle=\tiny,
keywordstyle=\color{blue!70},
commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox,
rulesepcolor=\color{red!20!green!20!blue!20},escapeinside=``,
xleftmargin=2em,xrightmargin=2em, aboveskip=1em}

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值