
LaTeX 문서에 삽입된 일부 Python 코드에 몇 가지 키워드를 추가하고 싶습니다. True
&를 노란색으로 표시 하고 싶고 False
일부 모듈 호출도 빨간색으로 ttk
표시하고 싶습니다. morekeywords 호출을 사용해 보았지만 작동하지 않습니다. 다음은 내 LaTeX 코드입니다.
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self},
keywordstyle=\ttb\color{deepblue},
morekeywords={ttk}
emph={MyClass,__init__},
emphstyle=\ttb\color{deepred},
stringstyle=\color{deepgreen},
showstringspaces=false
}}
답변1
당신이 사용할 수있는
keywords=[<number>]{<list of keywords>}
다른 키워드 세트를 추가한 다음
keywordstyle={[<number>]<style commands>},
이 새로운 세트에 스타일을 부여합니다.
색상이나 \ttb 명령에 대한 원래 정의가 없기 때문에 아래 예에서는 몇 가지 자체 설정을 사용했지만 사용자는 자신의 설정을 쉽게 사용할 수 있습니다. 이 예에서는 코드가 무작위로 선택되었습니다.
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{bera}% optional; just for the example
\lstset{
language=Python,
basicstyle=\ttfamily,
otherkeywords={self},
keywordstyle=\ttfamily\color{blue!90!black},
keywords=[2]{True,False},
keywords=[3]{ttk},
keywordstyle={[2]\ttfamily\color{yellow!80!orange}},
keywordstyle={[3]\ttfamily\color{red!80!orange}},
emph={MyClass,__init__},
emphstyle=\ttfamily\color{red!80!black},
stringstyle=\color{green!80!black},
showstringspaces=false
}
\begin{document}
\begin{lstlisting}
def under_attack(col, queens):
left = right = col
for r, c in reversed(queens):
left, right = left - 1, right + 1
if c in (left, col, right):
return True
return False
print 'This generation has {0} babies'.format(babies)
ttk.Button(buttonframe,width = 3,
textvariable=guivars["%s %s" %(current, item)],
command=lambda: remoteButton(current, item))
\end{lstlisting}
\end{document}
"ttk"도 요청한 대로 빨간색이 포함된 키워드로 처리됩니다.