按照關鍵字清單設定字型格式

按照關鍵字清單設定字型格式

我想使用 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 keywords 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}

在此輸入影像描述

相關內容