# How can I get my IDE to recognize listing labels?

I'm using the listings package to print code snippets. My IDE is TeXstudio 2.8.8. I mention this for the sake of completeness - I assume the behaviour is the same for all IDEs, which is why I wrote a generic title and did not use the tag.

TeXstudio suggests labels when I use the \ref command, but those that start with lst: (read: that are assigned to listings) are ignored. As a consequence I have to rummage through my LaTeX file every time I want to reference a listing to make sure I get the label right.

I suspect this is due to listing labels being assigned in a non-standard way:

\chapter{Example chapter}
\label{chap:example-chapter} % standard way of assigning labels
And he not: I pray thee, that the sons of Noah builded Nineveh.

\begin{lstlisting}[label={lst:example-listing}] % assignment with the 'label' option
print("Hello world!")
\end{lstlisting}

Here's a minimal working example:

\documentclass{report}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[label={lst:example-listing}]
print("Hello world!")
\end{lstlisting}
See listing~\ref{lst:example-listing}.
\end{document}

Is there a different way to assign a label to listings, so that my IDE recognizes and suggests them when I use the \ref command?

I don't think this is supported. It would require an explicit feature of the program.

I can only offer an ugly workaround; the idea is to have another comment character besides %, so everything after ^^A is ignored as if it were after %, but TeXStudio will be fooled into thinking it's a a real label.

\documentclass{report}
\usepackage{listings}

\AtBeginDocument{\catcode`\^^A=14 }

\begin{document}
\begin{lstlisting}[label={lst:example-listing}]
print("Hello world!")
\end{lstlisting}^^A\label{lst:example-listing}

\ref{lst:example-listing}

\end{document}

• Grazie mille egreg! Is ^^A an arbitrary character? – fenceop Jan 26 '15 at 23:22
• @fenceop It stands for ASCII 1, which can't be typed directly. The doc package uses it for comments, for instance. – egreg Jan 26 '15 at 23:25

I figured out a not-so-ugly workaround that I can live with. Here's a minimal working example:

\documentclass{article}
\usepackage{listing} % A wrapper to assign labels in the "standard" way
\usepackage{listings}
\usepackage{caption} % Used for \captionsetup
\begin{document}

\begin{listing}
\begin{lstlisting}
print("Hello world!")
\end{lstlisting}
\captionsetup{skip=-2pt} % Narrow the space between caption and code
\label{lst:example-listing}
\end{listing}

See listing~\ref{lst:example-listing}.

\end{document}

My IDE (TeXstudio 2) now also suggests listing labels when inserting refs. The \captionsetupis used so that the caption text is approximately at the same position as lstlisting would place it.

I guess you could add it in a figure environment and then add the label manually:

\begin{figure}
\label{lst:example-listing}
\begin{lstlisting}
print("Hello world!")
\end{lstlisting}
\end{figure}
• Thank you. The only issue I have with this answer is that figure is a floating environment, and I would rather have complete control on the position of my listings. – fenceop Jan 26 '15 at 22:14
• @fenceop You can use the [H] option together with the package \usepackage{float} to make a figure non-floating: \begin{figure}[H] ... – Loreno Heer Jan 26 '15 at 22:18
• That seems to do the trick, so I'll accept your answer for now. For future reference: I added \floatplacement{figure}{H} to my preamble to make figures non-floating by default. – fenceop Jan 26 '15 at 22:32
• The label won't refer to anything sensible – egreg Jan 26 '15 at 22:48
• @fenceop Did you try using \ref{lst:example-listing}? What number will you get? – egreg Jan 26 '15 at 22:55
\documentclass{report}
\usepackage{listings}

\lstset{escapechar=\@}

\begin{document}
\begin{lstlisting}
@\label{lst:example-listing}@
print("Hello world!")
\end{lstlisting}
\end{document}
• TeXstudio still doesn't suggest the lst:example-listing label. Thank you for trying though. – fenceop Jan 26 '15 at 23:16