# Floating environment with caption outside the box

I have defined a float environment following this answer in the "optional caption" version:

\documentclass{article}
\usepackage{newfloat}
\usepackage{caption}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Frame]{myfloat}

\captionsetup[myfloat]{labelfont=bf}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

\newenvironment{frameenv}[1][]
{\begin{myfloat}[tb]
\begin{mdframed}[roundcorner=10pt,backgroundcolor=blue!10]
\caption{#1}
}
{\end{mdframed}\end{myfloat}
}

\begin{document}

\lipsum[1]

\begin{frameenv}[Details for the caption]\label{frame1}
\lipsum[1]
\end{frameenv}

See frame \ref{frame1}.

\end{document}


Now I want to move the caption outside the box, so I tried moving the \caption{#1] command in the {enddef} argument of \newenvironment{} between \end{mdframed} and \end{myfloat}, changing the definition of the frameenv environment to the following:

\newenvironment{frameenv}[1][]
{\begin{myfloat}[tb]
\begin{mdframed}[roundcorner=10pt,backgroundcolor=blue!10]
}
{\end{mdframed}\caption{#1}\end{myfloat}
}


When I compile the document I get an error:

! Illegal parameter number in definition of \endframeenv.

1

l.16 }

and I can't figure out where is my mistake.

How can I move the caption outside the box?

One way is to smuggle #1 to the second part using \def\mycaption{#1} and then \expandafter\caption{\expandafter\mycaption} in the end. Big thanks to Axel Sommerfeldt for pointing out the \expandafters and, in particular, wha they are good for!

\documentclass{article}
\usepackage{newfloat}
\usepackage{caption}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Frame]{myfloat}

\captionsetup[myfloat]{labelfont=bf}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

\newenvironment{frameenv}[1][]
{\def\mycaption{#1}\begin{myfloat}[tb]
\begin{mdframed}[roundcorner=10pt,backgroundcolor=blue!10]
}
{\end{mdframed}\expandafter\caption{\expandafter\mycaption}\end{myfloat}
}

\begin{document}

\lipsum[1]

\begin{frameenv}[Details for the caption\label{frame1}]
\lipsum[1]
\end{frameenv}

See frame \ref{frame1}.

\end{document}


• \begin{frameenv}[\label{frame1}] will produce "Frame 1:". To get rid of the colon automatically in this case (empty caption text), replace \caption{\mycaption} with \expandafter\caption\expandafter{\mycaption}. – user2574 Jan 12 at 16:14
• @AxelSommerfeldt Thanks! I will be happy to remove this. It is much more appropriate if you write an answer. – Schrödinger's cat Jan 12 at 16:19
• It's not clear to me my I should write my own answer. Your answer is fine and my comment is just a little improvement of your answer (by adding two \expandafter), so the "drop separator if caption text is empty" algorithm of the caption package will remain intact. – user2574 Jan 12 at 16:35