# Split listing into multiple sections

I have a big listing that I split up into several lstlisting environments like so:

Some paragraphs describing part 1

\begin{lstlisting}[language=javascript,name={Caption},label={list:Label}]
Code Part 1
\end{lstlisting}

Some paragraphs describing part 2

\begin{lstlisting}[language=javascript,name={Caption},label={list:Label}]
Code Part 2
\end{lstlisting}

...


The code needs, in total, about 2 pages, and I put every interesting component in a separate listing so that I can add a description in-between. This has several consequences:

1. Since I refer to specific lines in my descriptions, the line numbers should continue where they stopped
2. Since all listing-environments show the same piece of code, I do not want the listing-counter to increase. The caption should always state the same ("Listing 42: Caption")

I managed to solve point (1) by using the same caption/label for all listings. But how can I ensure that the captions do not increase the listing-counter? What is the usual way to interrupt a long listing with descriptions?

Working example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage[final]{listings}

\lstset{
captionpos=b,
extendedchars=true,
numberbychapter=false,
caption=\lstname,
numbers=none,
basicstyle={\fontfamily{pcr}\selectfont\footnotesize},
breaklines,
breakatwhitespace,
breakautoindent,
}

\begin{document}

Some paragraphs describing part 1

\begin{lstlisting}[name={Caption},label={list:Label}]
Code Part 1
\end{lstlisting}

Some paragraphs describing part 2

\begin{lstlisting}[name={Caption}]
Code Part 2
\end{lstlisting}

\end{document}

• Your code doesn't use the caption key, so where is the problem? (But don't use the same label more than one). – Ulrike Fischer Jul 17 '17 at 12:53
• The caption is automatically added and uses the value of name. It shows Code 1: Caption and Code 2: Caption. The only thing I want to change is, that both captions use the same counter and show Code 1: Caption. – maja Jul 17 '17 at 13:03
• You didn't get the hint: Don't show only snippets. Make a real, complete example. – Ulrike Fischer Jul 17 '17 at 13:05
• I know that you can manually set the counter of enumerations with \setcounter{enumi}{4}, but I would need it for listing captions. And instead of setting an absolute value, I want to reduce it by 1. – maja Jul 17 '17 at 13:06
• @UlrikeFischer The problem with the working example is, that I need to use a custom document class from my university. And I'm not good enough in LaTeX to know which parts are responsible for which effects. Unfortunately, this makes it really difficult to post questions and - of course - provide answers. I'll give it a try anyways – maja Jul 17 '17 at 13:11

Add an empty caption and then the real caption with the title key:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage[final]{listings}

\lstset{
captionpos=b,
extendedchars=true,
numberbychapter=false,
caption=\lstname,
numbers=none,
basicstyle={\fontfamily{pcr}\selectfont\footnotesize},
breaklines,
breakatwhitespace,
breakautoindent,
}

\begin{document}

Some paragraphs describing part 1

\begin{lstlisting}[name={Caption},label={list:Label}]
Code Part 1
\end{lstlisting}

Some paragraphs describing part 2

\begin{lstlisting}[name={Caption},caption={},title={\lstlistingname~\thelstlisting: Blub (continued)}]
Code Part 2
\end{lstlisting}

\end{document}