3

I'm experimenting to place a rule above and below a floating figure to achieve the following design. But to no avail.

I've tried to place a \blackrule before and after the \placefigure. But that's obviously not gonna work since the float floats around the document without taking the rule with it.

Mock up

EDIT:

I tried the following code, which doesn't quite do the trick. I need the rule above the caption.

\startplacefigure \blackrule[width=\textwidth,height=.8pt,color=red] \externalfigure[kitten.jpg][width=\textwidth] \blackrule[width=\textwidth,height=.8pt,color=red] \stopplacefigure

enter image description here

3
  • Welcome to TeX.SE! Could you please provide some example code that shows the problem and/or your attempts so far?
    – Marijn
    Commented Oct 29, 2018 at 20:49
  • Try \startplacefigure\blackrule\externalfigure [kitten]\blackrule\stopplacefigure. Then you'd just need to adjust captioning.
    – TeXnician
    Commented Oct 29, 2018 at 20:50
  • @TeXnician But then how do I get the caption beneath the rule?
    – Christoph
    Commented Oct 29, 2018 at 21:03

2 Answers 2

2

AFAIK, there is no in-built option for this, but it is relatively straight forward to implement using the core commands:

\def\floatbackrule{\blackrule[width=\textwidth,height=.8pt,color=red]}

\unprotect
\installfloatboxbuilder {topframed}    \strc_floats_build_box_top_framed
\installfloatboxbuilder {bottomframed} \strc_floats_build_box_bottom_framed

\def\strc_floats_build_box_top_framed
    {\floatbackrule
     \strc_floats_build_box_top
     \floatbackrule}

\def\strc_floats_build_box_bottom_framed
    {\floatbackrule
     \strc_floats_build_box_bottom
     \floatbackrule}

\protect

Here is an example:

\setupcaption
  [
    location=topframed,
  ]

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

\starttext 

\section{A figure with rules} 

\input knuth

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

\dorecurse{4}{\input knuth \endgraf}

\stoptext

which gives:

enter image description here

Addendum: This simple method fails with longer captions and does not work with the align option. The reason is that an internal macro \strc_floats_prepare_page_caption does some corrections with the location parameter is top or bottom. These corrections don't take place when location is set to topframed. To fix this, we need to patch `\strc_floats_prepare_page_caption' as follows:

\def\strc_floats_prepare_page_caption
  {\edef\p_strc_floats_caption_width   {\floatcaptionparameter\c!width}%
   \edef\p_strc_floats_caption_minwidth{\floatcaptionparameter\c!minwidth}%
   \edef\p_strc_floats_caption_align   {\floatcaptionparameter\c!align}%
   \dostarttagged\t!floatcaption\empty
   \doifcommonelse\floatcaptionlocation{\v!top,\v!bottom,topframed,bottomframed}
      {\strc_floats_prepare_page_caption_top_bottom}
      {\ifx\p_strc_floats_caption_width\v!fit
         \strc_floats_prepare_side_auto_caption
       \else\ifx\p_strc_floats_caption_width\v!max
         \strc_floats_prepare_side_auto_caption
       \else
         \strc_floats_prepare_side_width_caption
      \fi\fi}%
   \dostoptagged}

Here is a complete example:

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

\def\floatbackrule{\blackrule[width=\textwidth,height=.8pt,color=red]}

\unprotect
\installfloatboxbuilder {topframed}    \strc_floats_build_box_top_framed
\installfloatboxbuilder {bottomframed} \strc_floats_build_box_bottom_framed

\def\strc_floats_build_box_top_framed
    {\floatbackrule
     \strc_floats_build_box_top
     \floatbackrule}

\def\strc_floats_build_box_bottom_framed
    {\floatbackrule
     \strc_floats_build_box_bottom
     \floatbackrule}

\def\strc_floats_prepare_page_caption
  {\edef\p_strc_floats_caption_width   {\floatcaptionparameter\c!width}%
   \edef\p_strc_floats_caption_minwidth{\floatcaptionparameter\c!minwidth}%
   \edef\p_strc_floats_caption_align   {\floatcaptionparameter\c!align}%
   \dostarttagged\t!floatcaption\empty
   \doifcommonelse\floatcaptionlocation{\v!top,\v!bottom,topframed,bottomframed}
      {\strc_floats_prepare_page_caption_top_bottom}
      {\ifx\p_strc_floats_caption_width\v!fit
         \strc_floats_prepare_side_auto_caption
       \else\ifx\p_strc_floats_caption_width\v!max
         \strc_floats_prepare_side_auto_caption
       \else
         \strc_floats_prepare_side_width_caption
      \fi\fi}%
   \dostoptagged}

\protect


\setupcaption
  [
    location=topframed,
    align=flushleft,
    width=\textwidth,
  ]
\starttext 

\section{A figure with rules} 

\input knuth

\startplacefigure[title={\input zapf \relax}]
  \externalfigure[cow]
\stopplacefigure

\dorecurse{4}{\input knuth \endgraf}

\stoptext

which gives

enter image description here

3
  • Thank you, your answer comes close. It has a problem with longer captions, which don't wrap right. Could you please have a look?
    – Christoph
    Commented Oct 30, 2018 at 9:08
  • Also the alignment parameter isn't regarded with topframed set: \setupcaption[location=topframed,align=flushleft]. With your core commands at hand I was digging into the ConTeXt source, but I haven't found a solution so far. Your help is much appreciated.
    – Christoph
    Commented Oct 30, 2018 at 12:59
  • Another aspect: the \floatbackrule doesn't seem to be the problem. Even if you leave it out like this \def\strc_floats_build_box_top_framed{\strc_floats_build_box_top} the way the caption wraps change. Please try different caption lengths and widths for the figure. Thank you.
    – Christoph
    Commented Oct 30, 2018 at 13:08
2

The next ConTeXt version will have the new command \setupfloatframed which can be used to set a background for the whole float block. The command takes the same arguments as \setupframed and the syntax for it is shown in the following image.

enter image description here

In addition to setting a background for the whole float you can also set individual backgrounds for the caption or the content of a float. To set the values for both elements you have to use the \setupcaption and \setupfloat commands which accept also the arguments of \setupframed.

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

\setupcaption
  [figure]
  [location=top]

\setupfloatframed
  [figure]
  [width=max,
   toffset=1.5ex,
   boffset=1.5ex,
   framecolor=red,
   topframe=on,
   bottomframe=on]

\starttext

\samplefile{weisman}

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

\samplefile{weisman}

\setupfloat       [intermezzo] [background=color,backgroundcolor=red]
\setupcaption     [intermezzo] [background=color,backgroundcolor=blue]
\setupfloatframed [intermezzo] [background=color,backgroundcolor=green]

\startplaceintermezzo[title={Another cow}]
  \externalfigure[cow][width=6cm]
\stopplaceintermezzo

\samplefile{weisman}

\stoptext

enter image description here

You must log in to answer this question.

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