赞
踩
如果要制作彩色的文档,里面引用的代码就得用彩色进行语法高亮了,这个功能是原始LaTex和fancyvrb不支持的。Listings的语法高亮主要分为关键字、注释、id、基本字符、字符串。在listset中分别使用keywordstyle、commentstyle、identifierstyle、basicstyle、stringstyle来配置。
要设置颜色,必须用到color或xcolor包,这样才可以使用color命令。仿照开发工具的语法高亮,我写了一个例子:
\documentclass[utf8]{paper}
\usepackage{ctex}
\usepackage{listingsutf8}
\usepackage{lstautogobble}
\usepackage{xcolor}
\lstset{
tabsize=4
}
\begin{document}
\title{Heap算法}
\author{醒过来摸鱼}
\maketitle
\abstract{Heap算法的Python实现}
\lstset{
basicstyle=\color{white},
keywordstyle=\color{yellow},
identifierstyle=\color{white},
stringstyle=\color{green},
backgroundcolor=\color{black!70},
commentstyle=\color{gray}
}
\begin{lstlisting}[language=python,autogobble]
def recursive(array, last, result):
if last == 0:
result.append(array[:])
return
for i in range(0, last + 1):
recursive(array, last - 1, result)
before = array[:]
if last & 1 == 0:
array[0], array[last] = array[last], array[0]
else:
array[i], array[last] = array[last], array[i]
# all permutations
def permutations(array):
result = []
recursive(array, len(array) - 1, result)
return result
if __name__ == '__main__':
result = permutations(['A', 'B', 'C', 'D'])
for x in result:
print(x)
\end{lstlisting}
\end{document}
编译效果如图:
字体设置和颜色设置一样,不过需要注意的是字体设置分命令和定义。什么意思呢?以text开头的,比如\textit是命令,而对应的\itshape这种就是定义。最好是用定义不要用命令。我举个例子:
\documentclass[UTF8,a4paper]{article}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\lstset{
basicstyle=\color{white}\rmfamily,
keywordstyle=\color{yellow}\slshape,
identifierstyle=\color{white},
stringstyle=\color{green}\bfseries,
backgroundcolor=\color{black!70},
numberstyle=\color{green!50},
commentstyle=\color{gray}\itshape
}
\begin{document}
\begin{lstlisting}[language=Python,autogobble]
def combinations(input):
# 队列元素是集合加上索引的元组
queue = [([], 0)]
result = []
while len(queue) > 0:
e = queue.pop(0)
result.append(e[0])
# 计算children
subarray = input[e[1]:]
for i, child in enumerate(subarray):
x = (e[0] + [child], e[1] + i + 1)
queue.append(x)
return result
if __name__ == '__main__':
print(combinations(['A', 'B', 'C']))
\end{lstlisting}
\end{document}
编译效果如下,从图中可以看到斜体加粗等均生效了:
用numbers=left来在左边显示行号,使用numberstyle来指定行号的样式,如下例:
\documentclass[UTF8,a4paper]{article}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\lstset{
basicstyle=\color{white}\rmfamily,
keywordstyle=\color{yellow}\slshape,
identifierstyle=\color{white},
stringstyle=\color{green}\bfseries,
backgroundcolor=\color{black!70},
numberstyle=\color{black!50},
commentstyle=\color{gray}\itshape
}
\begin{document}
\begin{lstlisting}[language=Python,autogobble,numbers=left]
def combinations(input):
# 队列元素是集合加上索引的元组
queue = [([], 0)]
result = []
while len(queue) > 0:
e = queue.pop(0)
result.append(e[0])
# 计算children
subarray = input[e[1]:]
for i, child in enumerate(subarray):
x = (e[0] + [child], e[1] + i + 1)
queue.append(x)
return result
if __name__ == '__main__':
print(combinations(['A', 'B', 'C']))
\end{lstlisting}
\end{document}
编译效果,在左边以灰色显示了行号:
有时候,我们需要每五行显示一个行号,而不是每行都显示行号,这样更优雅一些,那listings能不能做到呢?肯定是可以的啊。这个时候需要两个重要的参数stepnumber=5,firstnumber=1就可以实现了,我举个例子:
\documentclass[UTF8,a4paper]{article}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\lstset{
basicstyle=\color{white}\rmfamily,
keywordstyle=\color{yellow}\slshape,
identifierstyle=\color{white},
stringstyle=\color{green}\bfseries,
backgroundcolor=\color{black!70},
numberstyle=\color{black!50},
commentstyle=\color{gray}\itshape
}
\begin{document}
多项式字符串展示代码:
\begin{lstlisting}[language=python,autogobble,numbers=left,stepnumber=5,firstnumber=1]
@staticmethod
def poly_str(polynomial):
result = ''
first = True
n = len(polynomial)
for i, coefficient in enumerate(polynomial):
if coefficient == 0:
continue
if first:
first = False
else:
result += '+'
if coefficient != 1:
result += str(coefficient) + '*'
order = n - i - 1
if order > 0:
result += 'x'
else:
result += '1'
if order > 1:
result += '**' + str(order)
return result
\end{lstlisting}
\end{document}
编译效果如下:
有些时候,需要为代码块加上边框。listting提供了多种边框,主要有以下几种:
1. shadowbox,将代码块整个包围起来,在底部和右边提供阴影,如以下代码:
\documentclass[UTF8,a4paper]{article}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\begin{document}
冰雹猜想:
\begin{lstlisting}[frame=shadowbox,language=python,autogobble]
def collatz(num):
step = 0
while num != 1:
num = num // 2 if num & 1 == 0 else 3 * num + 1
step += 1
return step
\end{lstlisting}
\end{document}
显示效果:
2. 字母组合,t代表top,r代表right,b代表bottom,l代表left。大写的,则是T代表双线top,R代表双线right,B代表双线bottpm,L代表双线left。我随便举个例子就可以了:
\documentclass[UTF8,a4paper]{article}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\begin{document}
冰雹猜想:
\begin{lstlisting}[frame=lRTb,language=python,autogobble]
def collatz(num):
step = 0
while num != 1:
num = num // 2 if num & 1 == 0 else 3 * num + 1
step += 1
return step
\end{lstlisting}
\end{document}
效果就是右边和顶部是双线条,左边和底部是单线条,如图:
边框圆角的设置是frameround,取值是四个t或f,t代表true,f代表false,从右上角按顺时针旋转定义,比如tftf代表右上角圆角,右下角不是圆角,左下角圆角,左上角不是圆角。我举个例子:
\documentclass[UTF8,a4paper]{article}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\begin{document}
冰雹猜想:
\begin{lstlisting}[frame=RBLT,frameround=tftf,language=python,autogobble]
def collatz(num):
step = 0
while num != 1:
num = num // 2 if num & 1 == 0 else 3 * num + 1
step += 1
return step
\end{lstlisting}
\end{document}
tftf的效果如图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。