如何将python代码块中的数字颜色改为蓝色,但同时让注释块中的数字保留绿色?
另外,对于关键字network3
。如何将关键字“network3”更改为蓝色,但在“network3.load_data_shared()”中将“network3”保留为黑色?
以下是代码:
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}
\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\renewcommand\lstlistingname{Python Code}
\lstdefinestyle{pycode}{
language=Python,
numbers=left,
numbersep= 7mm,
numberstyle=\color{Black},
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{Black}\ttfamily,
commentstyle=\color{LimeGreen},
stringstyle=\color{WildStrawberry},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip,
basicstyle=\ttfamily\scriptsize,
classoffset=1,
morekeywords={var, func, extends, import, as,from},
keywordstyle=\color{BurntOrange}\bfseries,
classoffset=2,
morekeywords={network3},
keywordstyle=\color{blue}\bfseries,
}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,listings}
\begin{document}
\begin{lstlisting}[style=pycode]
"""
===============
Many plot types can be combined in one figure to create
powerful and flexible representations of data.
10 is not colored.
"""
import network3
from network3 import Network
from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
training_data, validation_data, test_data = network3.load_data_shared()
mini_batch_size = 10
net = Network([
FullyConnectedLayer(n_in=784, n_out=100),
SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size)
net.SGD(training_data, 60, mini_batch_size, 0.1, validation_data, test_data)
\end{lstlisting}
\end{document}
答案1
这是解决评论问题的一种方法。它不是完全自动化的,因为你需要突出显示评论片段,但只要这不是常规做法,那么你应该没问题。(这是基于这个答案到继续列表包中的行号?)。这种方法有几个明显的缺陷,但作为一种快速破解方法,它可能是合适的。
关键思想
- 将代码分为注释块和非注释块(这不是完全自动化的)。为此,我创建了一个
\begin{brokenlst}
环境来使用。 \begin{pycode}
然后逐个输入代码块。- 应该对代码块进行命名,以确保它知道是否应该选择并继续编号。
- 通过切换来标记这是否是注释还是常规块或代码
style
。
新变化
请注意以下事项:
\makeatletter
\lstnewenvironment{pycode}[1][]{%
\lstset{%
#1,
%...
%numberstyle=\color{black}, % Removed
%...
aboveskip=-0.5\baselineskip, % Changed
%belowskip=-0.\baselineskip, % Removed
%...
}%
\csname\@lst @SetFirstNumber\endcsname
}{%
\csname \@lst @SaveFirstNumber\endcsname
}
\makeatother
\lstdefinestyle{comment}{numberstyle=\color{magenta}}
\newenvironment{brokenlst}{\addvspace\baselineskip}{\addvspace\baselineskip}
输出
完整代码
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor,color}
\renewcommand\lstlistingname{Python Code}
\makeatletter
\lstnewenvironment{pycode}[1][]{%
\lstset{%
#1,
language=Python,
numbers=left,
numbersep= 7mm,
%numberstyle=\color{black}, % Removed
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{black}\ttfamily,
commentstyle=\color{magenta},
stringstyle=\color{red},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=-0.5\baselineskip, % Changed
%belowskip=-0.\baselineskip, % Removed
basicstyle=\ttfamily\scriptsize,
classoffset=1,
morekeywords={var, func, extends, import, as,from},
keywordstyle=\color{orange}\bfseries,
classoffset=2,
morekeywords={network3},
keywordstyle=\color{blue}\bfseries,
firstnumber=auto,
}%
\csname\@lst @SetFirstNumber\endcsname
}{%
\csname \@lst @SaveFirstNumber\endcsname
}
\makeatother
\lstdefinestyle{comment}{numberstyle=\color{magenta}}
\newenvironment{brokenlst}{\addvspace\baselineskip}{\addvspace\baselineskip}
\begin{document}
We begin with some broken code
\begin{brokenlst}
\begin{pycode}[name=ForLoopBlock, style=comment]
# echo xyz
\end{pycode}
\begin{pycode}[name=ForLoopBlock]
for i in range(10):
print('Hello world')
\end{pycode}
\begin{pycode}[name=ForLoopBlock, style=comment]
# x = 2
\end{pycode}
\begin{pycode}[name=ForLoopBlock, caption={Some broken code.}]
for i in range(10):
print('Hello world') # Why?
\end{pycode}
\end{brokenlst}
Some other unrelated code
\begin{brokenlst}
\begin{pycode}[name=Something else]
for i in range(10):
print('Hello world')
\end{pycode}
\end{brokenlst}
The user's code snippet.
\begin{brokenlst}
\begin{pycode}[name=ManyPlots, style=comment]
"""
===============
Many plot types can be combined in one figure to create
powerful and flexible representations of data.
10 is not colored.
"""
\end{pycode}
\begin{pycode}[name=ManyPlots, caption={Doesn't look too bad \dots}]
import network3
from network3 import Network
from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
training_data, validation_data, test_data = network3.load_data_shared()
mini_batch_size = 10
net = Network([
FullyConnectedLayer(n_in=784, n_out=100),
SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size)
net.SGD(training_data, 60, mini_batch_size, 0.1, validation_data, test_data)
\end{pycode}
\end{brokenlst}
Other languages shouldn't be too effected.
\begin{lstlisting}[language=c, caption={Perhaps revise your default listing name?}]
int main()
{
printf("hello world")
}
\end{lstlisting}
\end{document}