按照关键字列表格式化字体

按照关键字列表格式化字体

我想使用 lstlistings 格式化一些 Python 代码,并将函数和类定义以粗体显示,即单词class或后面的任何内容def。但是我不想单独定义关键字,因为调用函数时它们不应该以粗体显示。

梅威瑟:

\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}

这产生了

在此处输入图片描述

我希望MyClass(following keyword class) 和__init__(following def) 的第一个实例为粗体,但 MyClass 的第二个实例应如图所示。我该怎么做呢?

请注意,只有关键字和 : 或 ( 之间的单词才应该用粗体表示。

编辑:我尝试添加

\lstset{moredelim=[s][\bfseries]{class }{:},
    moredelim=[s][\bfseries]{def }{(},}

没有运气

答案1

您不应在可选参数中重新加载语言。这将覆盖您的某些设置,请参阅第二个示例。

您可以尝试以下操作,但请注意,它还会以粗体显示冒号。恕我直言,避免这种情况相当困难。

\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}

在此处输入图片描述

相关内容