36

I’d like to have an environment that works much like a float, except that it doesn’t float.

That is, in fact, I’d like to be able to define an environment that has a label and a caption.

I explicitly do not want to use a floating environment, though. For one thing, floats do many things fancy which I just don’t need, nor want (such as spacing) and for another, floats have their share of problems (for example consider the problem of splitting a float between pages).

Is there a ready-made way to achieve this? Of course, I could always write my own based on the result of the other answer, i.e. harnessing \refstepcounter. But I’m not sure how to use that, documentation is scarce, and I’d prefer an existing solution.

29

You can use \captionof from either the caption or capt-of package:

\documentclass{article}
\usepackage{caption}

\DeclareCaptionType{mytype}[Typename][List of mytype]
\newenvironment{myenv}{}{}

\begin{document}
\listofmytypes

\section{abc}

\begin{myenv}
\captionof{mytype}{blub} something in my type
\end{myenv}

\end{document}
  • Thanks, that did it. Although redefining \caption inside the environment so I can use that instead of \captionof was surprisingly difficult (I suspect cross-talk between caption and hyperref). But now I can write (almost) usual code and get a caption + label. – Konrad Rudolph Dec 16 '10 at 15:57
  • 1
    Why not just use \captionof{figure}{blub}? That way it will show up in the list of figures. Nor do you need a custom environment. – John Kormylo Oct 1 '14 at 23:00
  • 1
    Just as a small (and late) addition in case anybody stumbles across this answer: If you use a KOMA-class the \captionof command is already available and you don't need caption or capt-of (there are also \captionbelowof and \captionaboveof, see the scrguide) – Skillmon Apr 9 '17 at 13:26
16

Redefining \caption inside the environment should be easy, as long as you let the caption package do the job and do not try it for yourself ;-)

Ulrikes example, modified:

\documentclass{article}
\usepackage{caption}

\DeclareCaptionType{mytype}[Typename][List of mytype]
\newenvironment{myenv}{\captionsetup{type=mytype}}{}

\begin{document}
\listofmytypes

\section{abc}

\begin{myenv}
\caption{blub} something in my type
\end{myenv}

\end{document}
  • Brilliant! That should indeed do the trick. Just for the record, I already got the same effect with a manual redefinition (but your code is much better, and I’ll adapt it) – what I wanted to achieve, though, is the ability to write \caption{…}\label{…} (in any order) and the code produced would put the label inside the caption, like so: \captionof{…\label{…}}. Surprisingly, that fails. – Konrad Rudolph Dec 18 '10 at 11:09

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.