【LaTex】6.4 listings高亮设置

颜色设置

  如果要制作彩色的文档,里面引用的代码就得用彩色进行语法高亮了,这个功能是原始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的效果如图:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醒过来摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值