listing使用方法前面博文已经写了很多了有:LaTeX技巧32:LaTeX程序代码排版,Listings宏包以及 LaTex技巧35:listings的范例程序还有制作 matlab样式的代码如:LaTeX技巧57:使用mcode宏包在LATEX制作m文件效果LaTeX技巧466:如何利用listing宏包制作专业的C++输出代码? LaTeX技巧450:Latex中插入彩色java代码

下面我们看看python代码如何制作呢?
我们给出了一个语法文件(选自这里) 其效果图如下:

代码如下:

\documentclass{article}
\usepackage{amsfonts}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{setspace}
\usepackage{palatino}

\renewcommand{\lstlistlistingname}{Code Listings}
\renewcommand{\lstlistingname}{Code Listing}
\definecolor{gray}{gray}{0.5}
\definecolor{green}{rgb}{0,0.5,0}

\lstnewenvironment{python}[1][]{
\lstset{
language=python,
basicstyle=\ttfamily\small\setstretch{1},
stringstyle=\color{red},
showstringspaces=false,
alsoletter={1234567890},
otherkeywords={\ , \}, \{},
keywordstyle=\color{blue},
emph={access,and,break,class,continue,def,del,elif ,else,%
except,exec,finally,for,from,global,if,import,in,i s,%
lambda,not,or,pass,print,raise,return,try,while},
emphstyle=\color{orange}\bfseries,
emph={[2]True, False, None, self},
emphstyle=[2]\color{green},
emph={[3]from, import, as},
emphstyle=[3]\color{blue},
upquote=true,
morecomment=[s]{"""}{"""},
commentstyle=\color{gray}\slshape,
emph={[4]1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
emphstyle=[4]\color{blue},
literate=*{:}{{\textcolor{blue}:}}{1}%
{=}{{\textcolor{blue}=}}{1}%
{-}{{\textcolor{blue}-}}{1}%
{+}{{\textcolor{blue}+}}{1}%
{*}{{\textcolor{blue}*}}{1}%
{!}{{\textcolor{blue}!}}{1}%
{(}{{\textcolor{blue}(}}{1}%
{)}{{\textcolor{blue})}}{1}%
{[}{{\textcolor{blue}[}}{1}%
{]}{{\textcolor{blue}]}}{1}%
{<}{{\textcolor{blue}<}}{1}%
{>}{{\textcolor{blue}>}}{1},%
framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox, rulesepcolor=\color{blue},#1
}}{}
\begin{document}

We can see in Code Listing~\ref{ex1} that... And compared to Code Listing~\ref{ex2} something else.

\begin{python}[moreemph={[4]42},caption={Simple python example No. 1},label=ex1]
\#!/usr/bin/env python3

class MyClass(MyOtherClass):
"""
Some Docstring ()
"
""
a = 3
b5 = -42
q = (3, 5, j)
SIGNAL('clicked()'),
"A String"
\end{python}

\begin{python}[moreemph={[4], 46, 48},caption={Simple python example No. 2},label=ex2]
\#!/usr/bin/env python
\#-*- coding: utf-8 -*-

class MyClass(MyOtherClass):
"""
Some Docstring
"
""
def __init__(self):
fruitbowl = ["pear""peach""banana"]
for fruit in fruitbowl:
print fruit
if fruit is "pear":
continue
else:
pass
if fruitbowl is not None:
var = True
else:
var = False
a = 3
b = -46
c = 3.48
someDict = {'one':1, 'two':2, "three":3}
aTuple = (1, 2, 3, 4, 5)
string = "one-two=3"
name = "Username: "
self.button1.connect(self.button1,
SIGNAL('clicked()'),
self.login)
return
\end{python}
\begin{equation}
a^2
\end{equation}
\lstlistoflistings

\end{document}

这样文档代码太长,可以拆分文件,为pythonlisting.tex + 主文件。语法文件这里下载:
http://cid-3cfe530319707374.office.live.com/self.aspx/ChinaTeX/pythonlisting.tex
那上面代码可以写为:

\documentclass{article}
\include{pythonlisting}
\begin{document}
We can see in Code Listing~\ref{ex1} that... And compared to Code Listing~\ref{ex2} something else.

\begin{python}[moreemph={[4]42},caption={Simple python example No. 1},label=ex1]
\#!/usr/bin/env python3

class MyClass(MyOtherClass):
"""
Some Docstring ()
"""

a = 3
b5 = -42
q = (35, j)
SIGNAL('clicked()'),
"A String"
\end{python}

\begin{python}[moreemph={[4], 4648},caption={Simple python example No. 2},label=ex2]
\#!/usr/bin/env python
\#-*- coding: utf-8 -*-

class MyClass(MyOtherClass):
"""
Some Docstring
"""

def __init__(self):
fruitbowl = ["pear""peach""banana"]
for fruit in fruitbowl:
print fruit
if fruit is "pear":
continue
else:
pass
if fruitbowl is not None:
var = True
else:
var = False
a = 3
b = -46
c = 3.48
someDict = {'one':1'two':2"three":3}
aTuple = (12345)
string = "one-two=3"
name = "Username: "
self.button1.connect(self.button1,
SIGNAL('clicked()'),
self.login)
return
\end{python}

\lstlistoflistings


\end{document}

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部