# Adding a frame and background color to floats in ConTeXt

I am trying to decorate the floats in my thesis as follows:

• each float shall have a top/bottom frame in a custom color,
• the caption and content of each float are supposed to have the same background color.

Basically, what I want it to look like is this:

So far I've only managed to do this by embedding the float in a framed text like this:

\startframedtext[frame=off,topframe=on,bottomframe=on,framecolor=darkgray,background=color,backgroundcolor=lightgray]
\placefigure[here][figure:figure label]
{figure caption}
{figure content}
\stopframedtext


This works as long as the float is placed somewhere in the middle of a page. But as soon as they are the first element on a page, caption and content are moved out of the framed text and it looks a bit like this:

I guess this has to do with the special way floats are placed by ConTeXt.

Now, I wonder if there is a way to fix this. One way could be to not use floats at all and setup a custom enumerated environment with labels instead. Any ideas what else I could try? \setupfloats and \setupcaptions do have before=, after= and inbetween= options but I couldn't get them to create the desired decorations for me.

• This was not possible three years ago. See this thread on the ConteXt mailing list. However, a lot of internals have changed since then. You could try asking on the ConTeXt mailing list again. – Aditya Aug 11 '11 at 4:44

The following works in MkII (using the pdftex engine), but not in MkIV because \placefloatcaption is not yet implemented in MkIV.

\setupexternalfigures[location={local,global,default}]

\setupcolors[state=start]
\setupfloats[frame=off,background=color, backgroundcolor=gray, backgroundoffset=3mm]

\starttext

\section{A figure with a background}

\placefigure
{none}
{\placelegend
{\externalfigure[cow]}
{\placefloatcaption[figure]{A dutch cow}}}

\stoptext


For MkIV, you can define your own float handler to draw a background. For example:

\setupexternalfigures[location={local,global,default}]

\installfloatboxbuilder {bottombackground}    \dofloatboxbackgroundbuilder

\unprotected\def\dofloatboxbackgroundbuilder
{\expanded{\doifinset{\v!overlay}{\floatcaptionparameter\c!location}}\bothangfloat
{\tempfloatwidth\wd\float_content_box
\framed
[
\c!align=\v!normal,
\c!frame=\floatcaptionparameter\c!frame,
\c!framecolor=\floatcaptionparameter\c!framecolor,
\c!background=\floatcaptionparameter\c!background,
\c!backgroundcolor=\floatcaptionparameter\c!backgroundcolor,
\c!backgroundoffset=\floatcaptionparameter\c!backgroundoffset,
]%
{\ifparfloat
\hbox{\hbox               {\box\float_content_box}}%
\dofloatboxbetweenstack
\hbox{\locate_side_float  {\box\float_caption_box}}%
\else
\setfloathsize
\hbox{\float_align_content{\box\float_content_box}}%
\dofloatboxbetweenstack
\hbox{\locate_text_float  {\box\float_caption_box}}%
\fi}}}

\setupcaption[location={bottombackground},background=color, backgroundcolor=gray, frame=off]
\starttext

\section{A figure with a background}

\startplacefigure[title={A dutch cow}]
\externalfigure[cow]
\stopplacefigure

\stoptext


You put the figure in the frame, but instead you should put the frame in the figure. When your figure is placed "here" it ends up within the frame, but top or bottom placement move it out of the frame. This would place the caption outside of the frame:

  \placefigure[top][figure:figure label]
{figure caption}
{
\startframedtext[frame=off,topframe=on,bottomframe=on,framecolor=darkgray,background=color,backgroundcolor=lightgray]
figure content
\stopframedtext
}

• This makes a lot of sense if only the float/figure content is to be decorated. In this case the caption is supposed to be included in the frame which is not implemented in your proposal. – Jannis Pohlmann Aug 8 '11 at 11:54
• Right. You can define and setup your own float, where you preset the framing. However, i found no option to put the caption into the frame. – beza1e1 Aug 8 '11 at 12:06