TransWikia.com

Rule above caption of lstlisting too long inside itemize

TeX - LaTeX Asked by Peter Kling on April 27, 2021

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}

enter image description here

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP