Latex插⼊代码程序+边框+颜⾊+汇编代码[转]
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}
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}
[转]
-------------------------------------------------------
汇编代码设置
\usepackage{xcolor}
\usepackage{listings}
\lstset{
basicstyle=\small,%
escapeinside=``,%
keywordstyle=\color{red} \bfseries,% \underbar,%
identifierstyle={},%
commentstyle=\color{blue},%
stringstyle=\ttfamily,%
%labelstyle=\tiny,%
extendedchars=false,%
linewidth=\textwidth,%
numbers=left,%
numberstyle=\tiny \color{blue},%
frame=trbl%
}
......
\begin{lstlisting}[language={[x86masm]Assembler}]
DATA SEGMENT
BF DB 3 DUP(0) ;`暂存⼀次输⼊数据`
TABTTL1 DB 'CODE SCORE', '$' ;`输⼊图式`
TABTTL2 DB 'CODE SCORE', 9, 'SEXY', 9, 15, '$' ;`输出格式`
;.....
\end{lstlisting}
----------------------------
设置模板(from: wikipedia)
1 \usepackage{listings}
2 \usepackage{color}
3
4 \definecolor{dkgreen}{rgb}{0,0.6,0}
5 \definecolor{gray}{rgb}{0.5,0.5,0.5}
6 \definecolor{mauve}{rgb}{0.58,0,0.82}
7
8 \lstset{ %
9 language=Octave, % the language of the code
10 basicstyle=\footnotesize, % the size of the fonts that are used for the code
11 numbers=left, % where to put the line-numbers
12 numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers
13 stepnumber=2, % the step between two line-numbers. If it's 1, each line
14 % will be numbered
15 numbersep=5pt, % how far the line-numbers are from the code
16 backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
17 showspaces=false, % show spaces adding particular underscores
18 showstringspaces=false, % underline spaces within strings
19 showtabs=false, % show tabs within strings adding particular underscores
20 frame=single, % adds a frame around the code
21 rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
22 tabsize=2, % sets default tabsize to 2 spaces
23 captionpos=b, % sets the caption-position to bottom
24 breaklines=true, % sets automatic line breaking
25 breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
26 title=\lstname, % show the filename of files included with \lstinputlisting;
27 % also try caption instead of title
28 keywordstyle=\color{blue}, % keyword style
29 commentstyle=\color{dkgreen}, % comment style
30 stringstyle=\color{mauve}, % string literal style
31 escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
32 morekeywords={*,...} % if you want to add more keywords to the set
33 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论