0%

LaTeX 之代码高亮

本文介绍用 listings 宏包设计代码高亮样式,包括 MATLAB 代码高亮样式和 Python 代码高亮样式。

相关代码及例子在 GitHub 仓库:code-latex-style,可以直接下载。

MATLAB 代码高亮 1

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% mcode-style1.tex
% !Tex Program = xelatex

\documentclass[11pt]{article}
%\usepackage{ctex}

\usepackage[left=1.25in,right=1.25in,top=1in,bottom=1in]{geometry}
\usepackage{listings,color}
\usepackage{fontspec} % for special fonts

%---------- matlab code style ----------
% set Matlab highlight color
%\definecolor{mBasic}{RGB}{248,248,242} % default
\definecolor{mKeyword}{RGB}{0,0,255} % bule
\definecolor{mString}{RGB}{160,32,240} % purple
\definecolor{mComment}{RGB}{34,139,34} % green
\definecolor{mBackground}{RGB}{245,245,245} % lightgrey
\definecolor{mNumber}{RGB}{128,128,128} % gray

\lstset{
language=Matlab, % choose the language of the code
xleftmargin=25pt,
xrightmargin=15pt,
frame=tlbr,framesep=4pt,framerule=0pt, % sets the frame style
%frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
basicstyle=\small\ttfamily,
keywordstyle={\color{mKeyword}}, % sets color for keywords
stringstyle={\color{mString}}, % sets color for strings
commentstyle={\color{mComment}}, % sets color for comments
backgroundcolor=\color{mBackground}, % choose the background color
title=\lstname, % show the filename of files included with \lstinputlisting;
keywords={break,case,catch,classdef,continue,else,elseif,end,for,
function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while},
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
numberstyle=\tiny\color{mNumber},
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
%numbersep=5pt, % how far the line-numbers are from the code
}

\lstset{basicstyle=\footnotesize\fontspec{Consolas}}

\title{MATLAB highlighting in LaTeX documents}
\author{Liutao~Tian \\ \texttt{andy123t@163.com}}
\date{}

\begin{document}
\maketitle

\section{MATLAB code demo}
\begin{lstlisting}[title={Euler method}]
% Euler method for the ODE model
% u'(x)=x^2+x-u, x in [0,1]
% Initial condition: u(0)=0 ;
% Exact solution: u(x)=-exp(-x)+x^2-x+1.
clear all; clf
h=0.1;
x=0:h:1;
N=length(x)-1;
u(1)=0; % initial value
fun=@(t,u) t.^2+t-u; % RHS

for n=1:N
u(n+1)=u(n)+h.*fun(x(n),u(n));
end

ue=-exp(-x)+x.^2-x+1; % exact solution
plot(x,ue,'b-',x,u,'r+','LineWidth',1)
legend('Exact','Numerical','location','North')
%title('Euler method','fontsize',12)
set(gca,'fontsize',12)
xlabel('x','fontsize',16), ylabel('u','fontsize',16,'Rotation',0)
\end{lstlisting}

\end{document}

排版效果

main-matlab.png

MATLAB 代码高亮 2

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
% mcode-style2.tex
% !Tex Program = xelatex

\documentclass[11pt]{article}
%\usepackage{ctex}

\usepackage[left=1.25in,right=1.25in,top=1in,bottom=1in]{geometry}
\usepackage{listings,color}
\usepackage{fontspec} % for special fonts

%---------- matlab code style ----------
% set Matlab highlight color
%\definecolor{mBasic}{RGB}{248,248,242} % default
\definecolor{mKeyword}{RGB}{0,0,255} % bule
\definecolor{mString}{RGB}{160,32,240} % purple
\definecolor{mComment}{RGB}{34,139,34} % green
\definecolor{mBackground}{RGB}{245,245,245} % lightgrey
%\definecolor{mNumber}{RGB}{128,128,128} % gray
\definecolor{mNumber}{RGB}{134,145,148} % gray
\definecolor{mNumberbg}{RGB}{237,240,241} % lightgrey

\lstset{language=Matlab, % choose the language of the code
%frame=tlbr,
xleftmargin=30pt,
xrightmargin=10pt,
frame=l,
framesep=15pt,%framerule=0pt, % sets the frame style
%frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
basicstyle=\small\ttfamily,
keywordstyle={\color{mKeyword}}, % sets color for keywords
stringstyle={\color{mString}}, % sets color for strings
commentstyle={\color{mComment}}, % sets color for comments
backgroundcolor=\color{mBackground}, % choose the background color
title=\lstname, % show the filename of files included with \lstinputlisting;
keywords={break,case,catch,classdef,continue,else,elseif,end,for,
function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while},
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
framexleftmargin=5pt,
fillcolor=\color{mNumberbg},
rulecolor=\color{mNumberbg},
numberstyle=\tiny\color{mNumber},
numbersep=9pt, % how far the line-numbers are from the code
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
}

\lstset{basicstyle=\footnotesize\fontspec{Consolas}}

\title{MATLAB highlighting in LaTeX documents}
\author{Liutao~Tian \\ \texttt{andy123t@163.com}}
\date{}

\begin{document}
\maketitle

\section{MATLAB code demo}

\begin{lstlisting}[title={Euler method}]
% Euler method for the ODE model
% u'(x)=x^2+x-u, x in [0,1]
% Initial condition: u(0)=0 ;
% Exact solution: u(x)=-exp(-x)+x^2-x+1.
clear all; clf
h=0.1;
x=0:h:1;
N=length(x)-1;
u(1)=0; % initial value
fun=@(t,u) t.^2+t-u; % RHS

for n=1:N
u(n+1)=u(n)+h.*fun(x(n),u(n));
end

ue=-exp(-x)+x.^2-x+1; % exact solution
plot(x,ue,'b-',x,u,'r+','LineWidth',1)
legend('Exact ','Numerical','location','North')
%title('Euler method','fontsize',12)
set(gca,'fontsize',12)
xlabel('x','fontsize', 16), ylabel('u','fontsize',16,'Rotation',0)
\end{lstlisting}

\end{document}

排版效果

main-matlab2.png

MATLAB 与 Python 代码高亮 1

这里定义了代码的 style,可选 matlab 或 python 来设置相应样式。

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
% mpcode-style1.tex
% !Tex Program = xelatex
% matlab code font - ttfamily
% python code font - Consolas

\documentclass[11pt]{article}
%\usepackage{ctex}

\usepackage[left=1.25in,right=1.25in,top=1in,bottom=1in]{geometry}
\usepackage{listings,color}
\usepackage{fontspec} % for special fonts

%---------- matlab and python code style ----------
% Matlab highlight color settings
%\definecolor{mBasic}{RGB}{248,248,242} % default
\definecolor{mKeyword}{RGB}{0,0,255} % bule
\definecolor{mString}{RGB}{160,32,240} % purple
\definecolor{mComment}{RGB}{34,139,34} % green
\definecolor{mBackground}{RGB}{245,245,245} % lightgrey
\definecolor{mNumber}{RGB}{128,128,128} % gray

% Python highlight color settings
%\definecolor{pBasic}{RGB}{248, 248, 242} % default
\definecolor{pKeyword}{RGB}{228,0,128} % magenta
\definecolor{pString}{RGB}{148,0,209} % purple
\definecolor{pComment}{RGB}{117,113,94} % gray
\definecolor{pIdentifier}{RGB}{166, 226, 46} %
\definecolor{pBackground}{RGB}{245,245,245} % lightgrey
\definecolor{pNumber}{RGB}{128,128,128} % gray

\lstdefinestyle{matlab}{
language=matlab, % choose the language of the code
xleftmargin=25pt,
xrightmargin=15pt,
frame=tlbr,framesep=4pt,framerule=0pt, % sets the frame style
%frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
basicstyle=\small\ttfamily,
keywordstyle={\color{mKeyword}}, % sets color for keywords
stringstyle={\color{mString}}, % sets color for strings
commentstyle={\color{mComment}}, % sets color for comments
backgroundcolor=\color{mBackground}, % choose the background color
%title=#1, % \lstname show the filename of files
keywords={break,case,catch,classdef,continue,else,elseif,end,for,
function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while},
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
numberstyle=\tiny\color{mNumber},
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
%numbersep=5pt, % how far the line-numbers are from the code
}

\lstdefinestyle{python}{
language=python, % choose the language of the code
xleftmargin=25pt,
xrightmargin=15pt,
frame=tlbr,framesep=4pt,framerule=0pt, % sets the frame style
%frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
%basicstyle=\small\ttfamily, % sets font style for the code
basicstyle=\footnotesize\fontspec{Consolas},
keywordstyle=\color{pKeyword}, % sets color for keywords
stringstyle=\color{pString}, % sets color for strings
commentstyle=\color{pComment}, % sets color for comments
backgroundcolor=\color{pBackground}, % choose the background color
%title=#1, % \lstnames how the filename of files
emph={format_string,eff_ana_bf,permute,eff_ana_btr},
emphstyle=\color{pIdentifier}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
numberstyle=\tiny\color{pNumber},
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
%numbersep=5pt, % how far the line-numbers are from the code
}

\title{MATLAB and Python highlighting in LaTeX documents}
\author{Liutao~Tian \\ \texttt{andy123t@163.com}}
\date{}

\begin{document}
\maketitle

\begin{lstlisting}[style=matlab,title={MATLAB code}]
% Euler method for the ODE model
% u'(x)=x^2+x-u, x in [0,1]
% Initial condition: u(0)=0 ;
% Exact solution: u(x)=-exp(-x)+x^2-x+1.
clear all; clf
h=0.1;
x=0:h:1;
N=length(x)-1;
u(1)=0; % initial value
fun=@(t,u) t.^2+t-u; % RHS

for n=1:N
u(n+1)=u(n)+h.*fun(x(n),u(n));
end

ue=-exp(-x)+x.^2-x+1; % exact solution
plot(x,ue,'b-',x,u,'r+','LineWidth',1)
legend('Exact','Numerical','location','North')
%title('Euler method','fontsize',12)
set(gca,'fontsize',12)
xlabel('x','fontsize',16), ylabel('u','fontsize',16,'Rotation',0)
\end{lstlisting}

\begin{lstlisting}[style=python,title={Python code}]
#PythonDraw.py
import turtle as t
t.setup(650, 350, 200, 200)
t.penup()
t.fd(-250)
t.pendown()
t.pensize(25)
t.pencolor("purple color")
t.seth(-40)
for i in range(4):
t.circle(40, 80)
t.circle(-40, 80)
t.circle(40, 80/2)
t.fd(40)
t.circle(16, 180)
t.fd(40 * 2/3)
t.done()
\end{lstlisting}

\end{document}

排版效果

MATLAB 代码高亮和第一个一样,下面只展示 Python 代码高亮。

main-python1.png

MATLAB 与 Python 代码高亮 2

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
% mpcode-style2.tex
% !Tex Program = xelatex
% matlab code font - ttfamily
% python code font - Consolas

\documentclass[11pt]{article}
%\usepackage{ctex}

\usepackage[left=1.25in,right=1.25in,top=1in,bottom=1in]{geometry}
\usepackage{listings,color}
\usepackage{fontspec} % for special fonts

%---------- matlab and python code style ----------
% Matlab highlight color settings
%\definecolor{mBasic}{RGB}{248,248,242} % default
\definecolor{mKeyword}{RGB}{0,0,255} % bule
\definecolor{mString}{RGB}{160,32,240} % purple
\definecolor{mComment}{RGB}{34,139,34} % green
\definecolor{mBackground}{RGB}{245,245,245} % lightgrey
\definecolor{mNumber}{RGB}{134,145,148} % gray

\definecolor{Numberbg}{RGB}{237,240,241} % lightgrey

% Python highlight color settings
%\definecolor{pBasic}{RGB}{248, 248, 242} % default
\definecolor{pKeyword}{RGB}{228,0,128} % magenta
\definecolor{pString}{RGB}{148,0,209} % purple
\definecolor{pComment}{RGB}{117,113,94} % gray
\definecolor{pIdentifier}{RGB}{166, 226, 46} %
\definecolor{pBackground}{RGB}{245,245,245} % lightgrey
\definecolor{pNumber}{RGB}{134,145,148} % gray

\lstdefinestyle{matlab}{
language=Matlab, % choose the language of the code
%frame=tlbr,
xleftmargin=30pt,
xrightmargin=10pt,
frame=l,
framesep=15pt,%framerule=0pt, % sets the frame style
%frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
basicstyle=\footnotesize\fontspec{Consolas}, %\small\ttfamily,
keywordstyle={\color{mKeyword}}, % sets color for keywords
stringstyle={\color{mString}}, % sets color for strings
commentstyle={\color{mComment}}, % sets color for comments
backgroundcolor=\color{mBackground}, % choose the background color
keywords={break,case,catch,classdef,continue,else,elseif,end,for,
function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while},
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
framexleftmargin=5pt,
fillcolor=\color{Numberbg},
rulecolor=\color{Numberbg},
numberstyle=\tiny\color{mNumber},
numbersep=9pt, % how far the line-numbers are from the code
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
}

\lstdefinestyle{python}{
language=python, % choose the language of the code
xleftmargin=30pt,
xrightmargin=10pt,
frame=l,
framesep=15pt,%framerule=0pt, % sets the frame style
%frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
%basicstyle=\small\ttfamily, % sets font style for the code
basicstyle=\footnotesize\fontspec{Consolas},
keywordstyle=\color{pKeyword}, % sets color for keywords
stringstyle=\color{pString}, % sets color for strings
commentstyle=\color{pComment}, % sets color for comments
backgroundcolor=\color{pBackground}, % choose the background color
emph={format_string,eff_ana_bf,permute,eff_ana_btr},
emphstyle=\color{pIdentifier}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
framexleftmargin=5pt,
fillcolor=\color{Numberbg},
rulecolor=\color{Numberbg},
numberstyle=\tiny\color{pNumber},
numbersep=9pt, % how far the line-numbers are from the code
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
}

\title{MATLAB and Python highlighting in LaTeX documents}
\author{Liutao~Tian \\ \texttt{andy123t@163.com}}
\date{}

\begin{document}
\maketitle

\begin{lstlisting}[style=matlab,title={MATLAB code}]
% Euler method for the ODE model
% u'(x)=x^2+x-u, x in [0,1]
% Initial condition: u(0)=0 ;
% Exact solution: u(x)=-exp(-x)+x^2-x+1.
clear all; clf
h=0.1;
x=0:h:1;
N=length(x)-1;
u(1)=0; % initial value
fun=@(t,u) t.^2+t-u; % RHS

for n=1:N
u(n+1)=u(n)+h.*fun(x(n),u(n));
end

ue=-exp(-x)+x.^2-x+1; % exact solution
plot(x,ue,'b-',x,u,'r+','LineWidth',1)
legend('Exact','Numerical','location','North')
%title('Euler method','fontsize',12)
set(gca,'fontsize',12)
xlabel('x','fontsize',16), ylabel('u','fontsize',16,'Rotation',0)
\end{lstlisting}

\begin{lstlisting}[style=python,title={Python code}]
#PythonDraw.py
import turtle as t
t.setup(650, 350, 200, 200)
t.penup()
t.fd(-250)
t.pendown()
t.pensize(25)
t.pencolor("purple color")
t.seth(-40)
for i in range(4):
t.circle(40, 80)
t.circle(-40, 80)
t.circle(40, 80/2)
t.fd(40)
t.circle(16, 180)
t.fd(40 * 2/3)
t.done()
\end{lstlisting}

\end{document}

排版样式

MATLAB 代码高亮和第二个一样,下面只展示 Python 代码高亮。

main-python2.png

可以根据需要自行修改设置文件,比如不显示代码行号。