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

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?

  • 2
    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}]
//Here is my code
| improve this answer | |

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.