我正在使用 listings 包编写一份报告,报告我使用的一些 Python 代码。我对 listings 不太熟悉,遇到了几个问题。具体来说:
- 我希望数字为蓝色(例如)。以下是一些示例https://tex.stackexchange.com/我可以改变它们的颜色。但是,有些变量里面有可以改变颜色的数字代码。
- 有一些单词(即方法参数的名称)我想改变颜色。例如,product=、latitude=、longitude=、band=、km_above_below=、km_left_right=,我希望它们变成橙色。我真的不知道该怎么做。
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{codeorangered}{RGB}{239,134,64}
\renewcommand{\lstlistingname}{Codice}
\lstdefinelanguage{Python}
{
keywords={from, import, def, return, as, for, if, in, len},
keywordstyle=\color{magenta},
keywords=[2]{product=, latitude=, longitude=, band=, km_above_below=, km_left_right=}
keywordstyle=[2]{\color{codeorangered}},
commentstyle={\color{codegreen}},
morecomment=[l]{\#},
morestring=[b]",
alsodigit={-},
alsoletter={&}
}
\lstdefinestyle{mystyle}{
language=Python,
backgroundcolor={\color{backcolour}},
commentstyle={\color{codegreen}},
keywordstyle={\color{magenta}},
keywordstyle=[2]{\color{codeorangered}},
numberstyle={\tiny\color{codegray}},
stringstyle={\color{codepurple}},
basicstyle={\ttfamily\footnotesize},
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
%otherkeywords={product=, latitude=, longitude=, band=, km_above_below=, km_left_right=},
%keywords=[2]{product=, latitude=, longitude=, band=, km_above_below=, km_left_right=},
%frame=single,
tabsize=2
}
\lstset{
style=mystyle
}
%
\newcommand\digitstyle{\color{blue}}
\makeatletter
\newcommand{\ProcessDigit}[1]
{%
\ifnum\lst@mode=\lst@Pmode\relax%
{\digitstyle #1}%
\else
#1%
\fi
}
\makeatother
\lstset{literate=
{0}{{{\ProcessDigit{0}}}}1
{1}{{{\ProcessDigit{1}}}}1
{2}{{{\ProcessDigit{2}}}}1
{3}{{{\ProcessDigit{3}}}}1
{4}{{{\ProcessDigit{4}}}}1
{5}{{{\ProcessDigit{5}}}}1
{6}{{{\ProcessDigit{6}}}}1
{7}{{{\ProcessDigit{7}}}}1
{8}{{{\ProcessDigit{8}}}}1
{9}{{{\ProcessDigit{9}}}}1
{<=}{{\(\leq\)}}1,
morestring=[b]",
morestring=[b]',
morecomment=[l]//,
}
\begin{document}
\begin{lstlisting}[language=Python, caption=Snippet example. ]
from nasawebservice import *
# Create a MODIS data provider
provider = NasaWebServiceAPIProvider(base_url="https://modis.ornl.gov")
# APPLICATION 1: retraive all data in the NASA's archive
archive = provider.async_execute_all(
product=Products.MOD13Q1,
latitude=44.04780019, longitude=10.35481917,
band=Bands.MOD13Q1.NDVI,
km_above_below=0, km_left_right=0
)
for item in archive: print(item)
# APPLICATION 2: get the last data if present
if provider.is_present(Products.MOD13Q1, 44.04780019, 10.35481917, "A2022081"):
last = provider.last_product(
product=Products.MOD13Q1,
latitude=44.04780019, longitude=10.35481917,
band=Bands.MOD13Q1.NDVI
km_above_below=0, km_left_right=0
)
print(last)
\end{lstlisting}
\end{document}