3

I made a box float using the following:

\usepackage{float}
\newfloat{Box}{t}{bx}[chapter]

and

\begin{Box}[htp]
  \label{box:test}
  \fcolorbox{black}{boxgray}{\parbox[c]{\textwidth}{The text goes here}}
\end{Box}

I would like to have the label, i.e. 'Box 1' appear inside the box in the first row of text. Does anyone have an idea of how to implement this?

A basic version of the file I am using:

\documentclass[11pt]{article}

\usepackage{color}


\definecolor{boxgray}{RGB}{215,215,215}
\usepackage{hyperref}

\usepackage{float}
\newfloat{Box}{t}{bx}[section]


\begin{document}


\section{example}

Something about \ref{box:test}.

\begin{Box}[htp]
  \label{box:test}
  \fcolorbox{black}{boxgray}{\parbox[c]{\textwidth}{The text goes here}}
\end{Box}


\end{document}  
2
  • Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format.
    – jub0bs
    Jun 6, 2013 at 10:02
  • Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Jun 6, 2013 at 10:32

1 Answer 1

3

You need an anchor for the label. This is normally done by caption and not by the float environment. This is the reason why you should put the label after caption. In your case there is no anchor. A trick would be:

\usepackage{etoolbox}
\AtBeginEnvironment{Box}{\refstepcounter{Box}}

However there is no hint inside your environment that the reference number contains to the environment. So I suggest the following:

  1. Define the new float environment with newfloat
  2. Related to point 1 you can setup the caption with captionsetup. (I know caption supports float)
  3. Using tcolorbox to draw the frame around the caption.

Here an MWE:

\documentclass[11pt]{article}

\usepackage{xcolor}
\definecolor{boxgray}{RGB}{215,215,215}

\usepackage{caption}
\usepackage{newfloat}%loaded by caption too
\DeclareFloatingEnvironment[%
fileext=bx,%
within=section,%
placement=t,
name=Box,%
listname={List of Boxes},%
]{Boxed}
\captionsetup[Boxed]{justification=justified,format=plain,singlelinecheck=false,skip=0pt}
\usepackage{tcolorbox}
\newcommand*\boxedcaption[2][box:\arabic{Boxed}]%
{\begin{tcolorbox}\caption{#2}\label{#1}\end{tcolorbox}}

\usepackage{lipsum}

\usepackage{hyperref}


\begin{document}


\section{example}

Something about \ref{box:test}.

\begin{Boxed}[htp]
\lipsum[1]
\boxedcaption[box:test]{Text goes here}
\end{Boxed}

Something about \ref{box:2}.

\begin{Boxed}[htp]
\lipsum[2]
\boxedcaption{Text goes here}
\end{Boxed}

\end{document} 
1
  • Or do you want a frame around the environment Box? Jun 6, 2013 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .