[Tex/LaTex] Image caption within \newfloat

captionsfloats

I am using the float package to create my own floats. Now I also want to add an image with its own caption inside, but the caption belongs to the outer float. Is it possible to create something like a figure environment inside my float? I that already but it does not work inside the float.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}

\usepackage{blindtext}
\usepackage{float}
\usepackage{framed}

\newfloat{infobox}{tbp}{ext}

\begin{document}

\begin{infobox}
  \begin{framed}
    \blindtext      
    \includegraphics[width=0.85\textwidth]{dummy}
    \caption{Caption for the image}
  \end{framed}
\end{infobox}

\end{document}

sample output

Best Answer

Floating environments defined with \newfloat do not respect the position of the \caption command but always use the selected float style instead which have a fixed, pre-defined caption position. They behave differently regarding this aspect than figure and table. (BTW: To make figure and table behave like environments defined with \newfloat, the float package offers \restylefloat.)

So if you want to have a floating environment which behaves like figure and table and let you place the \caption where you want to, do not use \newfloat but \DeclareFloatingEnvironment (or \DeclareCaptionType) offered by the newfloat package, or \DeclareNewTOC offered by the KOMA-Script document classes.

For example:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[demo]{graphicx}% "demo" added because we don't have your picture file

\usepackage{blindtext}
%\usepackage{float}
\usepackage{caption}% caption added since you use it anyway
\usepackage{framed}

%\newfloat{infobox}{tbp}{ext}
\DeclareCaptionType[fileext=ext]{infobox}
  % \newfloat replaced by \DeclareCaptionType

\begin{document}

\begin{infobox}
  \begin{framed}
    \blindtext      
    \includegraphics[width=0.85\textwidth]{dummy}
    \caption{Caption for the image}
  \end{framed}
\end{infobox}

\end{document}

enter image description here

Please note that I've added the caption package since (according your comment) you use it anyway. If you don't use the caption package, the straight-ahead way would be loading the newfloat package and using \DeclareFloatingEnvironment instead.

BUT: If all infoboxes should be framed a different (and IMHO better) approach would be using the boxed style of the float (or floatrow) package:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[demo]{graphicx}

\usepackage{blindtext}
\usepackage{float}

\floatstyle{boxed}
\newfloat{infobox}{tbp}{ext}
\restylefloat*{infobox}
\floatname{infobox}{Infobox}

\begin{document}

\begin{infobox}
  \blindtext      
  \includegraphics[width=0.85\textwidth]{dummy}
  \caption{Caption for the image}
\end{infobox}

\end{document}

(The \restylefloat* is necessary so the caption will be placed inside the frame. Without it the caption would always been outside the frame.)

Related Question