我正在实施我学到的方法这里用于将 Python 代码放入 Latex。一切都很顺利,只是我对注释有点问题。这是我提到的链接中的代码:
\usepackage[utf8]{inputenc}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9} % for normal
% Defining colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\usepackage{listings}
% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
backgroundcolor=\color{white}
basicstyle=\ttm,
otherkeywords={self},
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},
emphstyle=\ttb\color{deepred},
stringstyle=\color{deepgreen},
commentstyle=\color{red}
frame=tb,
showstringspaces=false
}}
% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}
python
我使用代码中定义的以下函数来生成Python
代码:
\begin{python}
class MyClass(Yourclass):
def __init__(self, my, yours):
String = "String"
String2 = '5 1 2 3 4'
print String2
import numpy as np #Comment1
# Comment2
\end{python}
这就是结果。我看到frame
每条评论都有两个词。你知道这里可能出了什么问题吗?我提到的链接在其他方面都运行良好。谢谢。
答案1
你缺少一些逗号:
\newcommand\pythonstyle{\lstset{
language=Python,
backgroundcolor=\color{white}, %%%%%%%
basicstyle=\ttm,
otherkeywords={self},
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},
emphstyle=\ttb\color{deepred},
stringstyle=\color{deepgreen},
commentstyle=\color{red}, %%%%%%%%
frame=tb,
showstringspaces=false
}}