适合我的 latex 代码的 python 环境

适合我的 latex 代码的 python 环境

我正在寻找乳胶序言以便获得一个好的 Python 脚本。这里我找到了这个

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom 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,
basicstyle=\ttm,
morekeywords={self},              % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false
}}


% Python environment

看起来很棒,但我发现无法将它应用到我的乳胶代码中,我迷失在它的复杂性中,我正在寻找的只是一个示例代码,例如

\begin{document}

here some python codes

\begin{listings}
    \pythonstyle 
import numpy as np
class __init__(self, a, b):
    self.a = a
    self.b = b
\end{listings}
\end{document}

或类似的东西,以便拥有合适的 Python 环境。你能帮帮我吗?

答案1

你做错了两件事:

  1. 包的代码环境listings没有被调用listing,但是lstlisting
  2. \pythonstyle应该去代码环境

所以如果你

\pythonstyle 
\begin{lstlisting}
import numpy as np
class __init__(self, a, b):
    self.a = a
    self.b = b
\end{lstlisting}

它会起作用。

我确实认为定义这样的宏有点奇怪,而不是例如用 定义新的列表样式\lstdefinestyle。请参阅下面的示例。

在此处输入图片描述

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom 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,
literate={-}{-}1 {*}{*}1,% {xxx}{\textrm{   }}1, 
basicstyle=\ttm,
morekeywords={self},              % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false
}}

\lstdefinestyle{pythonstyling}{
language=Python,
basicstyle=\ttm,
morekeywords={self},              % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false
}


% Python 
\begin{document}
here some python codes

\begin{lstlisting}[style=pythonstyling]
import numpy as np
class __init__(self, a, b):
    self.a = a
    self.b = b
\end{lstlisting}

\pythonstyle 
\begin{lstlisting}
import numpy as np
class __init__(self, a, b):
    self.a = a
    self.b = b
\end{lstlisting}
\end{document}

相关内容