在latex中显示github diff代码样式

自定义lstlisting样式

显示代码需要使用listings包,若要latex支持代码高亮,需要使用xcolor包:

1
2
\usepackage{listings}
\usepackage[svgnames]{xcolor} % 加上svgnames参数以直接使用预定义颜色名称

使用xcolor包提供的\definecolor{<name>}{<model>}{<color-spec>}来定义颜色,具体请参考colors包的文档

1
2
3
4
5
6
\definecolor{linenumber}{rgb}{0.5,0.5,0.5}
\definecolor{diffstart}{named}{Grey}
\definecolor{diffincl}{named}{Green}
\definecolor{diffrem}{named}{DarkRed}
\definecolor{keyword}{named}{Blue}
\definecolor{comment}{named}{Grey}

使用listings包提供的\lstset设置listing全局环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\lstset{
language=C,
numberstyle=\color{linenumber},
keywordstyle=\color{keyword},
commentstyle=\color{comment},
morecomment=[f][\color{diffstart}]{@},
morecomment=[f][\color{diffincl}]{+},
morecomment=[f][\color{diffrem}]{-},
float=[htb],
showstringspaces=false,
columns=flexible,
basicstyle={\footnotesize\ttfamily\bfseries},
numbers=left,
xleftmargin=4.0ex,
breaklines=true,
breakatwhitespace=true,
tabsize=4,
sensitive = true,
escapeinside={(*@}{@*)}, % 用于内嵌latex语句
captionpos=b,
}

或使用\lstdefinestyle{<style name>}{<key=value>}创建风格:

1
2
3
\lstdefinestyle{stylename} {
...
}

然后就可以用\begin{lstlisting}\end{lstlisting}列出代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\begin{lstlisting}[caption={diff},label={lst:sourcecode},frame=tb] % 如果前面使用\lstdefinestyle定义了风格的话,在[]中加‘style=stylename’
@@ librabbitmq/amqp_openssl.c:180 -85,8 +85,8 @@
#include<stdio.h>
static int amqp_ssl_socket_open(void *base, const char *host, int port, struct timeval *timeout) {
// (*@\dots@*)
+ cert = SSL_get_peer_certificate(self->ssl);
result = SSL_get_verify_result(self->ssl);
- if (X509_V_OK != result) {
+ if (!cert || X509_V_OK != result) {
goto error_out3;
}
// (*@\dots@*)
}
\end{lstlisting}

效果如下图

result

最后附上全部latex代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
\documentclass{article}
\usepackage{listings}
\usepackage[svgnames]{xcolor}
\definecolor{linenumber}{rgb}{0.5,0.5,0.5}
\definecolor{diffstart}{named}{Grey}
\definecolor{diffincl}{named}{Green}
\definecolor{diffrem}{named}{DarkRed}
\definecolor{keyword}{named}{Blue}
\definecolor{comment}{named}{Grey}
\lstset{
language=C,
numberstyle=\color{linenumber},
keywordstyle=\color{keyword},
commentstyle=\color{comment},
morecomment=[f][\color{diffstart}]{@},
morecomment=[f][\color{diffincl}]{+},
morecomment=[f][\color{diffrem}]{-},
float=[htb],
showstringspaces=false,
columns=flexible,
basicstyle={\footnotesize\ttfamily\bfseries},
numbers=left,
xleftmargin=4.0ex,
breaklines=true,
breakatwhitespace=true,
tabsize=4,
sensitive = true,
escapeinside={(*@}{@*)},
captionpos=b,
}
\begin{document}
\begin{lstlisting}[caption={diff},label={lst:sourcecode},frame=tb]
@@ librabbitmq/amqp_openssl.c:180 -85,8 +85,8 @@
#include<stdio.h>
static int amqp_ssl_socket_open(void *base, const char *host, int port, struct timeval *timeout) {
// (*@\dots@*)
+ cert = SSL_get_peer_certificate(self->ssl);
result = SSL_get_verify_result(self->ssl);
- if (X509_V_OK != result) {
+ if (!cert || X509_V_OK != result) {
goto error_out3;
}
// (*@\dots@*)
}
\end{lstlisting}
\end{document}