Steveire
January 5th, 2007, 01:56 AM
I am using LaTeX to write my thesis, which requires some python code. I
found the listings package some time ago, to do code highlighting, and
I've attempted to configure it to look like the highlighting produced
by the KDE text editor Kate. My attempts have been largely successful,
but I've had to use a work-around to get digits and special characters
highlighted.
As a result, the highlighting is not as I'd like it. It highlights the
digit '1' in button1, and all digits and specified 'keywords' in
strings and the shebang line. If I could use the emph command on the
digits and braces/brackets etc that would be a lot better because it
would handle comments and strings as I expect it to. I have been
unsuccessful in trying to use the emph command on these characters
however. I even tried using character codes like chr(53), but to no
effect.
Any pointers on getting this right are welcome.
Here is a sample:
\documentclass[11pt]{report}
\usepackage{palatino}
\usepackage{listings} % Gives syntax highlighting for python code.
\usepackage{color} % Used for syntax highlighting.
\usepackage{textcomp} % Used for syntax highlighting.
% This gives syntax highlighting in the python environment
\renewcommand{\lstlistlistingname}{Code Listings}
\renewcommand{\lstlistingname}{Code Listing}
\definecolor{gray}{gray}{0.5}
\definecolor{key}{rgb}{0,0.5,0}
\lstnewenvironment{python}[1][]{
\lstset{
language=python,
basicstyle=\ttfamily\small,
otherkeywords={1, 2, 3, 4, 5, 6, 7, 8 ,9 , 0, -, =, +, [, ], (, ), \{, \}, :, *, !},
keywordstyle=\color{blue},
stringstyle=\color{red},
showstringspaces=false,
emph={class, pass, in, for, while, if, is, elif, else, not, and, or,
def, print, exec, break, continue, return},
emphstyle=\color{black}\bfseries,
emph={[2]True, False, None, self},
emphstyle=[2]\color{key},
emph={[3]from, import, as},
emphstyle=[3]\color{blue},
upquote=true,
morecomment=[s]{"""}{"""},
commentstyle=\color{gray}\slshape,
framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox,
rulesepcolor=\color{blue},#1
}}{}
\begin{document}
\lstlistoflistings
\clearpage
Code listing \ref{svntoolsource} shows a sample.
\begin{python}[caption={SvnTool source code},label=svntoolsource]
#!/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
elif fruit is "banana":
break
else:
pass
if fruitbowl is not None:
var = True
else:
var = False
a = 3
b = -4
string = "one-two"
name = "Username: "
self.button1.connect(self.button1,
SIGNAL('clicked()'),
self.login)
return
\end{python}
\end{document}
I posted this on comp.text.tex but it has so far been overlooked.
found the listings package some time ago, to do code highlighting, and
I've attempted to configure it to look like the highlighting produced
by the KDE text editor Kate. My attempts have been largely successful,
but I've had to use a work-around to get digits and special characters
highlighted.
As a result, the highlighting is not as I'd like it. It highlights the
digit '1' in button1, and all digits and specified 'keywords' in
strings and the shebang line. If I could use the emph command on the
digits and braces/brackets etc that would be a lot better because it
would handle comments and strings as I expect it to. I have been
unsuccessful in trying to use the emph command on these characters
however. I even tried using character codes like chr(53), but to no
effect.
Any pointers on getting this right are welcome.
Here is a sample:
\documentclass[11pt]{report}
\usepackage{palatino}
\usepackage{listings} % Gives syntax highlighting for python code.
\usepackage{color} % Used for syntax highlighting.
\usepackage{textcomp} % Used for syntax highlighting.
% This gives syntax highlighting in the python environment
\renewcommand{\lstlistlistingname}{Code Listings}
\renewcommand{\lstlistingname}{Code Listing}
\definecolor{gray}{gray}{0.5}
\definecolor{key}{rgb}{0,0.5,0}
\lstnewenvironment{python}[1][]{
\lstset{
language=python,
basicstyle=\ttfamily\small,
otherkeywords={1, 2, 3, 4, 5, 6, 7, 8 ,9 , 0, -, =, +, [, ], (, ), \{, \}, :, *, !},
keywordstyle=\color{blue},
stringstyle=\color{red},
showstringspaces=false,
emph={class, pass, in, for, while, if, is, elif, else, not, and, or,
def, print, exec, break, continue, return},
emphstyle=\color{black}\bfseries,
emph={[2]True, False, None, self},
emphstyle=[2]\color{key},
emph={[3]from, import, as},
emphstyle=[3]\color{blue},
upquote=true,
morecomment=[s]{"""}{"""},
commentstyle=\color{gray}\slshape,
framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox,
rulesepcolor=\color{blue},#1
}}{}
\begin{document}
\lstlistoflistings
\clearpage
Code listing \ref{svntoolsource} shows a sample.
\begin{python}[caption={SvnTool source code},label=svntoolsource]
#!/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
elif fruit is "banana":
break
else:
pass
if fruitbowl is not None:
var = True
else:
var = False
a = 3
b = -4
string = "one-two"
name = "Username: "
self.button1.connect(self.button1,
SIGNAL('clicked()'),
self.login)
return
\end{python}
\end{document}
I posted this on comp.text.tex but it has so far been overlooked.