What difference is there between caption and title in lstlisting in latex?
I can see the title on the output pdf. I do not see the label anywhere though. What is the lstlisting label used for?
Thanks
LaTeX forum ⇒ General ⇒ What difference is there between caption and title in lstlisting in latex?
-
- Posts: 706
- Joined: Tue Mar 25, 2008 5:02 pm
What difference is there between caption and title in lstlisting in latex?
According to the documentation (which you should read!), the "title" field is the entire title of the code sample without a number. The caption is the same thing, except it says "Listing 1: [caption contents]" or similar for second, third, etc. listings. If you specify both, whichever is specified last "wins."
The "label" is how you refer to the caption number in the text (as "\label" won't work inside a verbatim environment). Observe:
If you switch the line that specifies a title with the one that specifies the caption, then re-run LaTeX a couple of times, the "\ref{hello}" part won't work, as there will be no number to refer to.
The "label" is how you refer to the caption number in the text (as "\label" won't work inside a verbatim environment). Observe:
\documentclass{article} \usepackage{listings} \begin{document} %\begin{lstlisting}[title={this is the title},label={hello}] \begin{lstlisting}[caption={this is a caption},label={hello}] This is code # Commented # etc. if ( 5 > 3 ) print "hello" \end{lstlisting} The listing above is Code sample~\ref{hello}. \end{document}
If you switch the line that specifies a title with the one that specifies the caption, then re-run LaTeX a couple of times, the "\ref{hello}" part won't work, as there will be no number to refer to.
Who is online
Users browsing this forum: No registered users and 19 guests