\lstinputlisting の参照が機能しない

\lstinputlisting の参照が機能しない

何か間違ったことをしているのかもしれないが、何が間違っているのか分からない...

次のようなコードにラベルを設定したいと思います。

\lstinputlisting[language=Python, label={lst:control_os_call}, firstline=231, lastline=257]{./code/server.py}

だから、\refを使ってリンクしてみることにした。

\ref{lst:control_os_call}

しかし、ラベルが見つからないというエラーが常に発生します ( のラベルのみ\lstinputlisting)。それで...これらの種類のラベルを参照するには、特定のパッケージをインポートする必要がありますか、それとも何が間違っているのでしょうか?

どうもありがとう :)

答え1

追加する必要があるのはcaptionそれだけです

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{server.py}
This is some python code
\end{filecontents*}

\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}
\lstinputlisting[language=Python,caption={some python code}, label={lst:control_os_call}, firstline=1, lastline=257]{server.py}

Here is the code~\ref{lst:control_os_call}
\end{document}

ここに画像の説明を入力してください

関連情報