[Tex/LaTex] Multipage listings with minted and captions

captionslistingsminted

I have source long listings (2+ pages) that I highlight using minited via \inputminted. I wish to have have a caption and a reference label for the listing in \listoflistings and in my text when I refer to my code.

I have tried the attached code by I get a warning.

Package caption Warning: The option `hypcap=true' will be ignored for this
(caption) particular \caption on input line 11.
See the caption package documentation for explanation.

Could someone explain the correct was to do this? I do this in 13 places so I would like to understand is this the best way to achieve this?

Thanks

Stuart

\documentclass[a4paper,11pt]{report}
\usepackage[hidelinks]{hyperref}  % I use XeLaTeX to pdf with hyperlinks
\usepackage{minted}
\usepackage{subcaption}  %Include this in MVE as I have figures with subcaption elsewhere

\begin{document}
\listoflistings
\newpage
In my Listing~\ref{label-to-long-listing} I have more than 2 page source code listing.
\inputminted[linenos, breaklines=true,fontsize=\scriptsize, numberblanklines=false]{bash}{long.txt}
\captionof{listing}{Long Listing ToC\label{label-to-long-listing}}

\end{document}

Best Answer

minted provides listing environment that puts the code block in a floating environment. Inside listing you can provide a \caption and a \label.

\documentclass[a4paper,11pt]{report}
\usepackage[hidelinks]{hyperref}  % I use XeLaTeX to pdf with hyperlinks
\usepackage{minted}
\usepackage{subcaption}  %Include this in MVE as I have figures with subcaption elsewhere

\usepackage{filecontents}
\begin{filecontents*}{long.txt}
  #!/bin/sh
  /opt/vc/bin/tvservice -o
\end{filecontents*}

\begin{document}
\listoflistings
\cleardoublepage
In my Listing~\ref{label-to-long-listing} I have more than 2 page source code listing.
\begin{listing}[H]
\inputminted[linenos, breaklines=true,fontsize=\scriptsize, numberblanklines=false]{bash}{long.txt}
\caption{Long Listing ToC\label{label-to-long-listing}}
\end{listing}

\end{document}

enter image description here

Alternative is (since you want page breaks) to use

\usepackage[all]{hypcap}

Also use \captionof inside a group as it demands so.

\documentclass[a4paper,11pt]{report}
\usepackage[hidelinks]{hyperref}  % I use XeLaTeX to pdf with hyperlinks
\usepackage[all]{hypcap}    %% use after hyperref
\usepackage{minted}
\usepackage{subcaption}  %Include this in MVE as I have figures with subcaption elsewhere

\usepackage{filecontents}
\begin{filecontents*}{long.txt}
  #!/bin/sh
  /opt/vc/bin/tvservice -o
\end{filecontents*}

\begin{document}
\listoflistings
\cleardoublepage
In my Listing~\ref{label-to-long-listing} I have more than 2 page source code listing.
\bgroup                    %%<<-- use this
\inputminted[linenos, breaklines=true,fontsize=\scriptsize, numberblanklines=false]{bash}{long.txt}
\captionof{listing}{Long Listing ToC\label{label-to-long-listing}}
\egroup                  %% and this

\end{document}