# Keep automatic numbering in listings [duplicate]

I have the following MWE

\documentclass{article}

%-> Package for font (and other elements) color:
\usepackage{color}
\definecolor{semigray}{gray}{0.5}

%-> To hyphenate long url addresses:
%\usepackage[hyphens,obeyspaces]{xurl}

%-> To highlight programming codes
\usepackage{listings}
\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}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle, basicstyle=\footnotesize}

\begin{document}

I will present here some piece of conde on python and discuss about them.

\section{First part of the code}
Well... first of all, we import the desired libraries. They are important because... blah blah blah.

\begin{lstlisting}[language=Python]
import numpy as np
#... contains basic functions such as: sin, exp, ln, etc.

import matplotlib.pyplot as plt
#... for graph plotting

from matplotlib.font_manager import FontProperties
#... for font management in plots
\end{lstlisting}

\section{Getting some results}
Let's first apply the \texttt{numpy} functions.

\begin{lstlisting}[language=Python]
#-> DEFINE THE FUNCTIONS
def c_pol(P0, t):
'''
P0: the initial polarization
t: the instant of time normalized to charges lifetime <taup>.
returns: polarization at time t normalized to generation rate <taup*G>.
'''
fP0, ft = float(P0), float(t)
#... make them float

exp = np.exp(-ft)
polarization = (fP0 - 1)*exp + 1
return polarization
\end{lstlisting}

\end{document}


It outputs

I saw some posts (like: lstlisting - line number gaps) with discussion about changing the listing first number.

My question is: is it possible to make listing keep numbering automatically from tha last value at the last call? I mean, is it possible to let listing do it by itself, without manual change?

The picture above illustrates my question. The red rectangle highlights the listing numbers starting again from 1. At left, in red, the values it would present if automatic numbering were continued from last lstlisting call.

## marked as duplicate by Marijn, CarLaTeX, leandriis, Phelype Oleinik, MenschJun 3 at 21:13

• Short answer (from the duplicate): use \lstset{firstnumber=last}. – Marijn Jun 3 at 20:32