# Problem with listings when using line numbers, name, and beamer overlays

It's is well known that to ensure that a certain <counter> is automatically reset on subsequent slides of a same frame one has to use either \resetcounteronoverlays{<counter>} (for LaTeX counters) or \resetcountonoverlays{<counter>} (for TeX counters). However, this seems not to work for the lstnumber counter used for numbering lines in a lstlisting environment when the name key is used, as the following simple MWE shows:

\documentclass{beamer}
\usepackage{listings}

\lstset{numbers=left}
\resetcounteronoverlays{lstnumber}

\begin{document}

\begin{frame}[fragile]
\begin{onlyenv}<1>
\begin{lstlisting}[name=first]
test line in first listing
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{lstlisting}[name=second]
test line in second listing
\end{lstlisting}
\end{onlyenv}
\end{frame}

\end{document}


The output:

If the name option is not used, the effect of \resetcounteronoverlays{lstnumber} is the expected and one gets the correct result.

How can the lstnumber be prevented from stepping when using the name key and beamer overlays?

• This gets better if you give the two listings the same name...the second then has line number 4! When name=first the starting number is stored in the macro, not counter, \lstno@first. There is no standard beamer command to rest that. Mar 11 '14 at 16:00
• @AndrewSwann Thanks. I think the macro is \lst@firstnumber? Mar 12 '14 at 12:34
• @GonzaloMedina That is cerntainly involved too, and gets updated by \lst@SetFirstNumber which uses \lstno@first Mar 12 '14 at 13:01
• @AndrewSwann would you like to provide an answer based on your comments? Mar 12 '14 at 13:11

For named listings, with say name=myprog, the values of line numbers is kept track of in corresponding macros \lstno@myprog. Unfortunately they are indeed macros and not counters, so the usual beamer resetting commands have no effect. On an individual listing you can always provide the firstnumber option to specify the starting line number. Otherwise, you need to do some manual resetting of the macros \lstno@... etc.

In the following we reset the numbering for the second family of listings in these two different ways. This gives

First overlay: name=first, OK

Second overlay: name=second, line number set explicitly, OK

Second slide, first overlay: name=first, no resetting, goes wrong

Second slide, second overlay: name=second, reset before frame, OK

The resetting can also be done between overlays within a frame.

\documentclass{beamer}
\usepackage{listings}

\lstset{numbers=left}

\begin{document}

\begin{frame}[fragile]
\begin{onlyenv}<1>
\begin{lstlisting}[name=first]
test line in first listing
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{lstlisting}[name=second,firstnumber=1]
test line in second listing
\end{lstlisting}
\end{onlyenv}
\end{frame}

\makeatletter
\def\lstno@second{1}
\makeatother

\begin{frame}[fragile]
\begin{onlyenv}<1>
\begin{lstlisting}[name=first]
test line in first listing
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{lstlisting}[name=second]
test line in second listing
\end{lstlisting}
\end{onlyenv}
\end{frame}
\end{document}


Note that setting \lst@firstnumber is not the correct thing to do, that sets the starting number for all listings.