# An environment for fixed figures

I am trying to create an environment that I can use to include "figures" within a framed environment. Because floats aren't allowed inside framed environments, I want to create a new environment fixedfigure that I can use within my framed environment. For style reasons, I want my fixedfigure environment to look as much like a figure environment as possible from the user's viewpoint. I have something that mostly works, but I have a couple of features that I couldn't quite get working. Also, if you notice something that I already have is wrong, I'm always anxious to learn more about best practices.

1. What is the best way to get some vertical space around the fixedfigure? I tried adding a \newline at the beginning of my fixedfigure environment, but got an error because there was no line to end.

2. I want to be able to create my captions in a fixedfigure using the caption command (the same thing I use to create captions in a normal figure). Unfortunately, I couldn't find a way to temporarily redefine caption within the fixedfigure environment. I think I need something like \newcommand{\caption}[1]{\captionof{figure}{#1}}, but this isn't quite right. I also put a feeble attempt using let in the example below (commented out).

\documentclass{article}
\usepackage{caption}

\newenvironment{fixedfigure}
{
% \let\caption\captionof
\noindent
\begin{minipage}{\textwidth}
}
{
\end{minipage}
}

\begin{document}

blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah

\begin{figure}
\centering
the contents of the figure
\caption{the caption of the figure}
\end{figure}

blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah

\begin{fixedfigure}
\centering
the contents of the figure
% \caption{the caption of the figure}
\captionof{figure}{the caption of the figure}
\end{fixedfigure}

blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah

\end{document}


You can add vertical space using the \vspace comand which takes one argument (a length). Your second problem is slightly trickier. First, you need to use \renewcommand, because \caption is already defined. In addition, you need a double hash symbol to denote an argument of \caption, as opposed to an argument of the fixedfigure environment. However, the obvious

\renewcommand\caption[1]{\captionof{figure}{##1}}


causes TeX to freeze when \caption is used. It turns out that you need to restore the definition of \caption from within the macro.

\documentclass{article}
\usepackage{caption}

\newenvironment{fixedfigure}{%
\let\oldcaption\caption
\renewcommand\caption[1]{\let\caption\oldcaption\captionof{figure}{##1}}
\noindent
\begin{minipage}{\textwidth}}
{\end{minipage}}

\begin{document}

\begin{figure}
\centering
the contents of the figure
\caption{the caption of the figure}
\end{figure}

\begin{fixedfigure}
\centering
the contents of the figure

\caption{the caption of the figure}
\end{fixedfigure}

\end{document}


Please forget about the redefinition of \caption and simply add \captionsetup{type=figure} to your environment instead. This has four main advantages:

1. You don't need to fiddle around with a proper redefinition
2. Hyperlinks to the figure (done using hyperref) will jump to the figure instead to the caption
3. All the variants of \caption will work, too, like \caption* or \caption[...]{...}
4. Local \captionsetups within your environment will not be overwritten by \captionsetup[figure]{...}, just like within ordinary figures.

(See caption doc for details.)

\documentclass{article}
\usepackage{caption,hyperref}
\captionsetup[figure]{labelfont=bf}

\newenvironment{fixedfigure}
{%
\par\vskip\intextsep
\noindent
\begin{minipage}{\textwidth}%
\captionsetup{type=figure}%
}
{%
\end{minipage}%
\vskip\intextsep
}

\begin{document}

blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah

\begin{figure}
\centering
the contents of the figure
\captionsetup{labelfont=it}% this one will overwrite "labelfont=bf" for this figure
\caption{the caption of the figure}
\end{figure}

blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah

\begin{fixedfigure}
\centering
the contents of the figure
\captionsetup{labelfont=it}% this one should overwrite "labelfont=bf" for this figure
\caption{the caption of the figure}\label{fig:test}
\caption*{Hey, this works, too}
\caption[Test]{And this one, too}
\end{fixedfigure}

blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah
blah blah blah blah blah

\end{document}