Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 21 down vote accepted

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}
share|improve this answer
    
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
    
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 at 23:00

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}
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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