2

Is there a way to number program listings like figures and reference them? I have the following mwe:

\documentclass{article}

\usepackage{hyperref}
\usepackage{listings}

\begin{document}

\autoref{lst:some_code}.

\begin{lstlisting}[float,frame=single,label={lst:some_code}]
if (i<=0) then i := 1;
if (i>=0) then i := 0;
if (i<>0) then i := 0;
\end{lstlisting}

\end{document}

But the reference comes out empty (the hyperlink still works though).

1 Answer 1

2

The caption package cooperates with listings, using \captionsetup[lstlistings]{…}:

\documentclass{article}
\usepackage{caption}
\captionsetup[lstlisting]{labelsep=none, singlelinecheck=off}
 \usepackage{hyperref}
\usepackage{listings}

\begin{document}

\autoref{lst:some_code}.

\begin{lstlisting}[float,frame=single, caption={\empty}, label={lst:some_code}]
if (i<=0) then i := 1;
if (i>=0) then i := 0;
if (i<>0) then i := 0;
\end{lstlisting}

\end{document} 

enter image description here

2
  • Aha, I tried with \captionsetup[listing] or something. This is better. May 16, 2016 at 18:54
  • @Torbjørn T.: As I was not too sure, I checked in the doc, that did the difference ;o)
    – Bernard
    May 16, 2016 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .