语法高亮
实际应用中,我们都会遇到插入代码的需求,前面的博文我介绍了verbatim环境和fancyvrb包的使用,但是这两种都没法语法高亮。而代码不同语法块,使用不同的颜色与字体进行高亮是毋庸置疑的。因为语法高亮后,看起来更加舒服,读者的注意力,可以更加集中。
LaTex的语法高亮,使用listings包。用法比较简单,如以下代码:
\documentclass[utf8]{paper}
\usepackage{ctex}
\usepackage{listings}
\usepackage{xeCJK}
\begin{document}
\title{Heap算法}
\author{醒过来摸鱼}
\maketitle
\abstract{Heap算法的Python实现}
\begin{lstlisting}[language=python]
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]
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}
但是编译后的效果并不好,如下图:
控制缩进
从上面的效果看,缩进得太厉害了,整体排版效果太难看了。这时候就需要加一个包了,lstautogobble,然后在选项里加上autogobble,如以下代码:
\documentclass[utf8]{paper}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\begin{document}
\title{Heap算法}
\author{醒过来摸鱼}
\maketitle
\abstract{Heap算法的Python实现}
\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]
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}
这样编译,效果就非常好了:
支持中文
我们中国人写代码,代码及注释中有中文在所难免,但是LaTex代码段中有中文就无法编译,怎么办呢?如下面代码,注释里有中文:
\documentclass[utf8]{paper}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\begin{document}
\title{Heap算法}
\author{醒过来摸鱼}
\maketitle
\abstract{Heap算法的Python实现}
\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[:]
# 奇偶区别处理,是Heap算法的核心
if last & 1 == 0:
array[0], array[last] = array[last], array[0]
else:
array[i], array[last] = array[last], array[i]
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}
编译就不通过了,报错信息很多:
这个时候需要配置编译器为XeLaTex,如下图所示,打开菜单栏里的Options->Configure TexStudio:
再编译就好了,效果如下:
代码样式
仔细看还是有问题的,虽然整体缩进改了,但是内部的缩进太大了。这时候就需要修改一下了。利用lstset指令可以修改代码内部的缩进,如以下例子:
\documentclass[utf8]{paper}
\usepackage{ctex}
\usepackage{listings}
\usepackage{lstautogobble}
\lstset{
tabsize=4
}
\begin{document}
\title{Heap算法}
\author{醒过来摸鱼}
\maketitle
\abstract{Heap算法的Python实现}
\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]
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}
编译效果,可以看到缩进再也不是8个字符了,代码漂亮多了: