I would like to format some Python code using lstlistings and have function and class definitions in bold, i.e. anything that follows the word class
or def
. I don't want to individually define the keywords however, as when the functions are called they shouldn't be bold.
MWE:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[lighttt]{lmodern} % bold and italic ttfamily
\usepackage{color}
\usepackage{listings}
\usepackage{setspace}
\definecolor{Code}{rgb}{0.1,0.1,0.1}
\definecolor{Decorators}{rgb}{0.7,0,0.7}
\definecolor{Numbers}{rgb}{0.5,0,0}
\definecolor{MatchingBrackets}{rgb}{0.25,0.5,0.5}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{self}{rgb}{.87,.46,0}
\definecolor{Strings}{rgb}{0,0.66,0}
\definecolor{Comments}{rgb}{0.388,0.4705,0.518}
\definecolor{Backquotes}{rgb}{0,0,0}
\definecolor{Classname}{rgb}{0,0,0}
\definecolor{FunctionName}{rgb}{0,0,0}
\definecolor{Operators}{rgb}{0,0,0}
\definecolor{Background}{rgb}{0.99,0.99,0.99}
\lstset{
language=Python,
numbers=left,
numberstyle=\footnotesize,
numbersep=1em,
xleftmargin=3em,
linewidth=23cm,
framextopmargin=1em,
framexbottommargin=1em,
showspaces=false,
showtabs=false,
showstringspaces=false,
frame=l,
tabsize=4,
% Basic
basicstyle=\ttfamily\small,
backgroundcolor=\color{Background},
% Comments
commentstyle=\color{Comments}\slshape,
% Strings
stringstyle=\color{Strings},
morecomment=[s][\color{Strings}]{"""}{"""},
morecomment=[s][\color{Strings}]{'''}{'''},
% keywords
keywordstyle={\color{Keywords}\bfseries},
% additional keywords
keywordstyle={[2]\color{Decorators}},
emph={self},
emphstyle={\color{self}\slshape},
}%
\begin{document}
\lstset{language=Python, breaklines=true}
\begin{lstlisting}
class MyClass:
def __init__(self):
print('do a thing')
foo = MyClass()
\end{lstlisting}
\end{document}
This produces
I would like the first instance of MyClass
(following keyword class
) and __init__
(following def
) to be bold, but the second instance of MyClass should be as shown. How would I go about doing that?
Note that only the word between the keyword and the : or the ( should be bold.
EDIT: I've tried adding
\lstset{moredelim=[s][\bfseries]{class }{:},
moredelim=[s][\bfseries]{def }{(},}
with no luck
답변1
You shouldn't reload the language in optional argument. This will overwrite some of your settings, see the second example.
You can try the following but be aware that it also prints the colon in bold. Imho it would be rather difficult to avoid it.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[lighttt]{lmodern} % bold and italic ttfamily
\usepackage{color}
\usepackage{listings}
\usepackage{setspace}
\definecolor{Code}{rgb}{0.1,0.1,0.1}
\definecolor{Decorators}{rgb}{0.7,0,0.7}
\definecolor{Numbers}{rgb}{0.5,0,0}
\definecolor{MatchingBrackets}{rgb}{0.25,0.5,0.5}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{self}{rgb}{.87,.46,0}
\definecolor{Strings}{rgb}{0,0.66,0}
\definecolor{Comments}{rgb}{0.388,0.4705,0.518}
\definecolor{Backquotes}{rgb}{0,0,0}
\definecolor{Classname}{rgb}{0,0,0}
\definecolor{FunctionName}{rgb}{0,0,0}
\definecolor{Operators}{rgb}{0,0,0}
\definecolor{Background}{rgb}{0.99,0.99,0.99}
\lstdefinestyle{mypython}{
language=Python,
numbers=left,
numberstyle=\footnotesize,
numbersep=1em,
xleftmargin=3em,
linewidth=23cm,
framextopmargin=1em,
framexbottommargin=1em,
showspaces=false,
showtabs=false,
showstringspaces=false,
frame=l,
tabsize=4,
% Basic
basicstyle=\ttfamily\small,
backgroundcolor=\color{Background},
% Comments
commentstyle=\color{Comments}\slshape,
% Strings
stringstyle=\color{Strings},
morecomment=[s][\color{Strings}]{"""}{"""},
morecomment=[s][\color{Strings}]{'''}{'''},
%% keywords
keywordstyle={\color{Keywords}\bfseries},
% additional keywords
keywordstyle={[2]\color{Decorators}},
emph={self},
emphstyle={\color{self}\slshape},
moredelim=**[s][\bfseries]{class }{:},
moredelim=**[s][\bfseries]{def }{:},
moredelim=**[s][\mdseries]{(}{)},
}%
\begin{document}
\begin{lstlisting}[style=mypython]
class MyClass:
def __init__(self):
print('do a thing')
foo = MyClass()
\end{lstlisting}
%wrong
\begin{lstlisting}[style=mypython,language=Python]
class MyClass:
def __init__(self):
print('do a thing')
foo = MyClass()
\end{lstlisting}
\end{document}