5

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

12

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.)

  • Thanks, but unfortunately I found out that the subfig package somehow fails with my document (cannot reproduce this in a minimal example unfortunately), so I ended up using your subcaption package + float, which now works as intended. – quazgar May 22 '13 at 9:13
  • @your edit: Great, that is exactly what I was looking for. – quazgar May 22 '13 at 9:24
3

You can use \captionof from capt-of (or caption).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{capt-of} % or \usepackage{caption}

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

\newfloat{infobox}{tbp}{ext}

\begin{document}

\begin{infobox}
  \begin{framed}
    \blindtext      
    \includegraphics[width=0.85\textwidth]{example-image}
    \captionof{figure}{Caption for the image}
  \end{framed}
\end{infobox}

\end{document}
  • Thanks, I noticed that it would need some tweaking if used together with subfig, which I just discovered, but it should work, I think. – quazgar May 22 '13 at 7:46
  • @quazgar If you use the subfig package, you don't need to include the capt-of package. – Axel Sommerfeldt May 22 '13 at 8:21
  • Unfortunately I found out that in my "real" document, \captionof did not stay inside the frame (could not reproduce it in a MWE, though). Therefore I used the subcaption package, which works as intended after a \DeclareCaptionType{infobox} statement. – quazgar May 22 '13 at 9:19

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.