How can I get my IDE to recognize listing labels?
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}
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
\caption{Your caption here!}
\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 \captionsetup
is used so that the caption text is approximately at the same position as lstlisting
would place it.
cross-referencinglistingstexstudio