0

I'm trying to create an environment for listing code that will use a ruled caption (in the style of the algorithm package). I don't want it to be a float environment, since I have to allow pagebreaks for long chunks of code.

\usepackage{minted, caption}
% other code
\newenvironment{code}{\captionsetup{type=listing}}{}

I tried \captionsetup[ruled]{type=listing}, but I'm getting the message

Package caption error: \caption outside float

What am I missing here?

2
  • Try \captionof{listing}?
    – Bernard
    Apr 19, 2018 at 22:38
  • If I read the manual correctly, \captionsetup[ruled]{} doesn't add a rule to caption, but rather formats the caption for a ruled float. Apr 19, 2018 at 23:28

1 Answer 1

1

The caption package tests for all sorts of things, including the counter name, the environment name, \l@listing, \ext@listing and Knuth knows what else. Frankly, it is a whole lot easier just to use the float package to create a listing float type and redefine the listing environment to NOT generate a float.

Minted doesn't work on my system, so I wasn't able to test it.

\documentclass{article}
\usepackage{float}
\usepackage{caption}

\newfloat{listing}{htp}{lol}
\floatname{listing}{Listing}

\makeatletter
\renewenvironment{listing}{\def\@captype{listing}%
  \trivlist
  \vskip\intextsep\item}%
{\par\vskip\intextsep\endtrivlist}
\makeatother

\begin{document}
\begin{listing}
\hrule
\caption{test}
\hrule
\end{listing}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .