9

I want to create a box (in which I put extra information for the reader) that behaves like a float. In other words, it should have a caption (outside of the box that says Box 1: Here is my box caption) and label that I can refer to with \cref like any other float.

Most of the solutions using \fbox, \framebox, \usepackage{framed} are all creating the box, but not about the caption and the label.

How to accomplish a floating box?

Heisenberg
  • 1,687
  • If you want a non-float, use \captionof macro from the caption package. However, there is no reason you can't put a box around the content of a float figure. – Steven B. Segletes Apr 27 '16 at 01:36
  • 1
    Does \captionof has a box option? (It does not from my experimentation). I want the caption to say Box 1: blah, and ideally, the \cref from the cref package to intelligently refer to the float as a Box. – Heisenberg Apr 27 '16 at 01:39

1 Answers1

6

First as a non-float. Then, as a float. EDITED to put caption outside of box. EDITED to introduce new float, InfoBox.

\documentclass{article}
\usepackage{caption,graphicx,newfloat}
\DeclareCaptionType{InfoBox}
\begin{document}
\fboxsep=8pt\relax
\fboxrule=2pt\relax
\begin{center}
\fbox{\centering
  \includegraphics[width=2in]{example-image-A}}\par
  \captionof{InfoBox}{Here is my caption\label{fg:A}}
\end{center}
\begin{InfoBox}[ht]
\centering
\fbox{\centering\medskip
  \includegraphics[width=2in]{example-image-B}}\par
  \caption{Here is my caption\label{fg:B}}  
\end{InfoBox}
In boxes \ref{fg:A} and \ref{fg:B}...
\end{document}

enter image description here

  • I actually want the caption to say Box 1: here's my caption, and the caption to be outside of the box. In other words, I want the box itself to behave like a float, not to have a float inside the box. – Heisenberg Apr 27 '16 at 01:44
  • @Heisenberg I edited to place caption outside of box. But am I to understand that you want the float called "Box"? If so, would that be in addition to figures and tables elsewhere in the document, or in lieu of figures? – Steven B. Segletes Apr 27 '16 at 01:49
  • Thanks for your help! It would be in addition to figures and tables, i.e. an additional type of float. It's very common in policy documents, e.g. this 2015 World Investment Report, box II.1, page 52 of 253 – Heisenberg Apr 27 '16 at 01:52
  • @Heisenberg Please see revision. – Steven B. Segletes Apr 27 '16 at 02:06
  • It works! I learned about the creating a new float type. It seems that one can't create a float type called Box though, correct? That's rather not ideal... – Heisenberg Apr 27 '16 at 02:14
  • @Heisenberg Apparently, the macro \Box is already defined (I believe it is a math symbol). If one was willing to overwrite it, one could probably \let\Box\relax and then define a new caption named Box...but I haven't tried it. – Steven B. Segletes Apr 27 '16 at 02:26
  • Any way to automatically add \fbox to the environment? – jessexknight Oct 26 '18 at 22:19
  • @JesseKnight If you mean to the InfoBox environment, I don't think so, but you certainly could create a macro like \includegraphics that puts a box around an imported image. – Steven B. Segletes Oct 26 '18 at 23:23