# Using the same line numbers in lstlisting on repeated beamer slide

In the following example, the line numbers on the second occurrence of the slide start from "3". But the references in the itemize item of the second occurrence of the slide show as "1" and "2" (i.e. the line numbers from the first occurrence of the slide).

I would like either:

1. every occurrence of the slide has the same line numbers
2. or, the references refer to the line numbers on the most recent occurrence of the slide

However, on a given occurrence of the slide, the line numbers in the right listing should continue from the line numbers in the left listing.

How can I achieve that?

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{
basicstyle=\normalsize\ttfamily,
numbers=left,
numberstyle=\tiny\color{gray},
stepnumber=1,
numbersep=5pt,
escapechar=£,
breaklines=true}
\begin{document}

\begin{frame}<1>[fragile,t,label=foo]
\frametitle{Example}
\begin{minipage}{.45\textwidth}%
\begin{lstlisting}[name=fooex]
foo£\label{lst:foo}£
\end{lstlisting}%
\end{minipage}%
\begin{minipage}{.45\textwidth}%
\begin{lstlisting}[firstnumber=auto,name=fooex]
bar£\label{lst:bar}£
\end{lstlisting}%
\end{minipage}%

\only<1>{
\begin{itemize}
\item First stuff
\end{itemize}}

\only<2>{
\begin{itemize}
\item stuff about lines \ref{lst:foo} and \ref{lst:bar}
\end{itemize}}
\end{frame}

\begin{frame}
\frametitle{Another frame}
\end{frame}

\againframe<2>{foo}
\end{document}

• Does adding firstnumber=1 (or whatever be the desired first number) to first listing help? – Ignasi Apr 1 '16 at 17:03
• That works pretty well thanks. There is one slight problem - that if I click the references in the pdf, it takes me to the first occurrence of the slide rather than the second. But other than that it is good. – flamingpenguin Apr 4 '16 at 9:40

1. To have constant numbers follow the suggestion from Ignasi ans specify the firstnumber

2. To specify to which occurrence of the label it should jump: \label<2>{lst:bar}. Of course this is not automatically the most recent occurrence, but for for simple constructs it might be enough. If you need to jump to both sides, you can use multiple labels \label<1>{lst:bara}\label<2>{lst:barb}

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{
basicstyle=\normalsize\ttfamily,
numbers=left,
numberstyle=\tiny\color{gray},
stepnumber=1,
numbersep=5pt,
escapechar=£,
breaklines=true}
\begin{document}

\begin{frame}<1>[fragile,t,label=foo]
\frametitle{Example}
\begin{minipage}{.45\textwidth}%
\begin{lstlisting}[firstnumber=1,name=fooex]
foo£\label<2>{lst:foo}£
\end{lstlisting}%
\end{minipage}%
\begin{minipage}{.45\textwidth}%
\begin{lstlisting}[firstnumber=2,name=fooex]
bar£\label<2>{lst:bar}£
\end{lstlisting}%
\end{minipage}%

\only<1>{
\begin{itemize}
\item First stuff
\end{itemize}}

\only<2>{
\begin{itemize}
\item stuff about lines \ref{lst:foo} and \ref{lst:bar}
\end{itemize}}
\end{frame}

\begin{frame}
\frametitle{Another frame}
\end{frame}

\againframe<2>{foo}
\end{document}