1

How to change the color of the numbers in the python code block to blue, but at the same time leave the numbers in the comment block green?

Also, for the keyword network3. How to just change that keyword "network3" to blue, but leave "network3" as black in `network3.load_data_shared()"?

Here is the code:

\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 Answer 1

2

Here is a workaround for the comments. It's not completely automated, as you need to highlight comment snippets, but provided this is not a regular thing then you should be fine. (This is based on this answer to Continue line numbers in listings package?). There are several obvious pitfalls with this, but as a quick hack it may be suitable.

Key ideas

  1. Break code into comment and non-comment blocks (this isn't completely automated). For this I created a \begin{brokenlst} environment to use.
  2. The chunks of code are then input using \begin{pycode} one after another.
  3. The code blocks should be named to ensure it knows if it should pick up and continue the numbering or not.
  4. Flag whether this is a comment or regular block or code by toggling the style.

New changes

Keep an eye out for the following:

\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}

Output

enter image description here

Full code

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .