# Rule above caption of lstlisting too long inside itemize

I want to use the caption package to insert a horizontal bar above the caption/title of a lstlisting environment (+ some other stylings which are not relevant here). The minimal example below shows a (slightly simplified) call to \DeclareCaptionFormat that achieves that.

However, as one can see in the second lstlisting environment below, the rule above the caption (and the caption itself) extend to the full page width when called inside an itemize environment. I would have expected the rule above the caption to look like the two frame lines above and below the listing. What am I doing wrong?

\documentclass{article}

\usepackage{caption}
\DeclareCaptionFormat{lstlisting}{\rule{\linewidth}{0.4pt}\\#1#2#3}
\captionsetup{format = lstlisting}

\usepackage{listings}
\lstset{title=Title, frame=lines}

\begin{document}
\begin{lstlisting}
line 1
line 2
\end{lstlisting}
\begin{itemize}
\item Listing:
\begin{lstlisting}
line 1
line 2
\end{lstlisting}
\end{itemize}
\end{document}


• My first guess would be that itemize sees listing as some sort of sublist and indents it accordingly. I tried putting the listing in enumerate and description and the same thing happened. – Plergux Nov 3 '20 at 8:48