20

I'm using the "listings" package to write pieces of code (XML, RDF, Java, etc) and I want them to have the same counter of the Figure environment.

For example,

Listing 1: blablabla
Listing 2: blebleble
Figure 3: bliblibli
Listing 4: blobloblo
Figure 5: blublublu

The purpose of doing it is to change the "Listing" label to "Figure". But I need both environments to share the same counter. How I do it?

Thanks in advance.

20

You can make things so the figure environment shares the counter with lstlisting. However, you should ensure that lstlisting environments are floating too, otherwise the numbers could not agree and a figure could appear before a listing with a lower number.

\documentclass{article}
\usepackage{listings}

\makeatletter
\AtBeginDocument{%
  \let\c@figure\c@lstlisting
  \let\thefigure\thelstlisting
  \let\ftype@lstlisting\ftype@figure % give the floats the same precedence
}
\makeatother

\begin{document}
\begin{lstlisting}[caption=A listing,float]
a=1
\end{lstlisting}

\begin{lstlisting}[caption=Another,float]
b=2
\end{lstlisting}

\begin{figure}
F
\caption{A figure}
\end{figure}

\begin{lstlisting}[caption=Again,float]
c=3
\end{lstlisting}
\end{document}

enter image description here

Thanks to David Carlisle for noting the need for adjusting \ftype@lstlisting.

If you also want that table shares the counter, add similar instructions.

\documentclass{article}
\usepackage{listings}

\makeatletter
\AtBeginDocument{%
  \let\c@figure\c@lstlisting
  \let\thefigure\thelstlisting
  \let\c@table\c@lstlisting
  \let\thetable\thelstlisting
  \let\ftype@lstlisting\ftype@figure % give the floats the same precedence
  \let\ftype@table\ftype@figure % give the floats the same precedence
}
\makeatother

\begin{document}
\begin{lstlisting}[caption=A listing,float]
a=1
\end{lstlisting}

\begin{lstlisting}[caption=Another,float]
b=2
\end{lstlisting}

\begin{figure}
F
\caption{A figure}
\end{figure}

\begin{table}
G
\caption{A table}
\end{table}

\begin{lstlisting}[caption=Again,float]
c=3
\end{lstlisting}
\end{document}
  • 1
    @Rick I made a small change; fix your code too. – egreg Nov 18 '13 at 0:48
  • Thanks again. I fixed my code, but I didn't notice what that new line does. Can you give me a brief explanation? – Rick Nov 18 '13 at 1:24
  • 1
    @Rick Without the added line, there would still be two independent float queues, one for figures and one for listings, so the order could be wrong, with figure 3 before listing 2. The added line adds listings to the queue for figures. – egreg Nov 18 '13 at 7:48
  • 1
    In my document, lstlisting was numbering per section and figure was numbering continuously. This code numbered figure per section. To get around this, assign the figure counter to lstlisting: "... \let\c@lstlisting\c@figure ... \let\thelstlisting\thefigure ... \let\ftype@figure\ftype@lstlisting ..." – badroit May 8 '14 at 20:53
  • 1
    @patryk.beza I added the relevant code. – egreg Jun 6 '17 at 17:26
1

A different strategy is to use the coupled counters feature from xassocnt which establishes a group of counters that are equally stepped if one of the group members is increased. This works for an 'arbitrary' number of counters.

The \let\ftype@lstlisting\ftype@figure trick is 'stolen' from egreg's answer -- such features are not contained in xassoccnt (yet..., perhaps it will be added one day)

\documentclass{article}
\usepackage{listings}

\usepackage{xassoccnt}

\DeclareCoupledCountersGroup{figuretablelistings}

% Only needed since listings defines the lstlisting at the begin of the document, not in the preamble 
\makeatletter
\AtBeginDocument{%
  \DeclareCoupledCounters[name=figurelistings]{figure,table,lstlisting}
  \let\ftype@lstlisting\ftype@figure % give the floats the same precedence
}
\makeatother



\begin{document}
\begin{lstlisting}[caption=A listing,float]
a=1
\end{lstlisting}

\begin{lstlisting}[caption=Another,float]
b=2
\end{lstlisting}

\begin{figure}
F
\caption{A figure}
\end{figure}

\begin{table}
\centering
\begin{tabular}{ll}
  \hline
  \TeX & \LaTeXe \tabularnewline
  \hline
  ConTeXt & Lua\LaTeX  \tabularnewline
  \hline
\end{tabular}
\caption{A Table}
\end{table}


\begin{lstlisting}[caption=Again,float]
c=3
\end{lstlisting}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.