3

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}
  • "See the caption package documentation for explanation." is not an option? All package errors and warning are explained in detail, see appendix A.3 "Warnings". – Axel Sommerfeldt Oct 6 '15 at 9:24
1

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}
  • Sorry but this only show one page of source code listing. My code does not goto to the second or more pages. Could you check with more than one page of code? – Stuie Oct 5 '15 at 11:08
  • @Stuie I don't have a bash code that big. Yes this won't break. I will add another option. – user11232 Oct 5 '15 at 11:09
  • @Stuie Check the updated answer. – user11232 Oct 5 '15 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.