引言部分
首先导入宏包
1 2
| \usepackage{listings} \usepackage{xcolor}
|
xcolor:这个宏包用于支持使用颜色。它提供了一些命令和选项,可以定义和使用颜色,例如\color命令可以在文本中改变字体颜色,\pagecolor命令可以改变页面背景颜色等。
同时对代码样式做出设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| \lstset{ language=verilog, frame=shadowbox, rulesepcolor=\color{red!20!green!20!blue!20}, keywordstyle=\color{blue!90}\bfseries, commentstyle=\color{red!10!green!70}\textit, showstringspaces=false, numbers=left, numberstyle=\tiny, stringstyle=\ttfamily, breaklines=true, extendedchars=false, escapebegin=\begin{CJK*},escapeend=\end{CJK*}, texcl=true }
|
代码部分
在插入代码块时,使用如下代码(以Verilog为例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| \begin{lstlisting} flowing_light u0( .clock(clock), .reset(reset), .led(led) );
parameter PERIOD = 10;
always #(PERIOD*2) clock=!clock;
initial begin clock=1'b0; reset=1'b0; #(PERIOD*2)reset=1'b1; #(PERIOD*4)reset=1'b0; end \end{lstlisting}
|
同时在插入代码块的地方也可以指定语言,如要插入c语言代码
1 2 3 4 5 6 7 8 9
| \begin{lstlisting}[language=C] // A C program to print "Hello, world!" #include <stdio.h>
int main() { printf("Hello, world!"); return 0; } \end{lstlisting}
|
如果同时在引言部分和插入代码块的地方指定了语言,那么在插入代码块的地方优先级更高。