# Referencing caption of code listing

I would like to reference some piece of code which has a caption on it like this:

\begin{lstlisting}[caption=componenet \label{Label} , backgroundcolor = \color{lightgray}]
//Here is my code
\end{lstlisting}


I reference it somewhere else like \ref{Label}, but when I click I get the initial page of my document, so this is not working. Any better idea on how to reference this?

• Try \begin{lstlisting}[caption=componenet, label=Label, backgroundcolor = \color{lightgray}] instead. – moewe Dec 9 '18 at 21:33
• @moewe Would you like to write an answer? – samcarter_is_at_topanswers.xyz Dec 12 '18 at 0:30
• @samcarter Done. – moewe Dec 12 '18 at 9:04

The lstlisting environment has a label key that can be used to give it a \label. Just use
\begin{lstlisting}[caption=componenet, label=Label, backgroundcolor = \color{lightgray}]