# ConTeXt: How to place a short caption under a wrapped figure?

This is a follow-up question to a previous ConTeXt solution. That solution dealt with wrapped figures in a ConTeXt document generated from Pandoc Markdown.

Now, I would like to extend that solution to allow for a short caption below a wrapped figure. Moreover, I would like to have the short caption typeset differently, say small-sized bold sans-serif.

Below is a minimal working example that shows how the layout of the original solution gets messed up when adding the short caption text. What do I need to change to the description definition to allow for short captions under wrapped figures?

\setuptolerance[horizontal, tolerant, stretch]

\setuppapersize [A4][A4]
\setuplayout    [width=middle,  backspace=1.5in, cutspace=1.5in,
height=middle, topspace=0.75in, bottomspace=0.75in]

\setuppagenumbering[location={footer,center}]

% Inter-paragraph spacing
\setupwhitespace[medium]

% Break at hyphens
\setbreakpoints[compound]

\setupexternalfigures[location=default]

% Remaining code is for descriptions with a left floating figure
% https://tex.stackexchange.com/a/133372/26348

\startsetups placefigure
\setupexternalfigure
[wfactor=fit]
\stopsetups

% Floats do not have "before=" nor a setup key
\appendtoks \setups{placefigure} \to \everyinsidefloat

\definemeasure[pageheight][\dimexpr(\pagegoal-\pagetotal-2\lineheight)]

\define[1]\startdescription{
\setbox\scratchbox\vbox{\setupexternalfigure[width=4cm]#1}%
\ifdim\ht\scratchbox>\dimexpr\measure{pageheight}\relax
\page
\fi
\startplacefigure[location={left,high,none}]
\box\scratchbox
\stopplacefigure
}

\define\stopdescription{\endgraf}

\starttext
\input douglas.tex

\startdescription{{\externalfigure[cow]}Short caption}
\input linden.tex
\stopdescription

\input bryson.tex
\stoptext


• After a while, it is easier to use XML or TEX as the input format rather than markdown. Markdown just does not have enough configuration options. – Aditya Nov 13 '13 at 1:33
• Hi Aditya. Nope, sorry to say that \hbox{\box\scratchbox} does not work. As for MarkDown, it really is the best way to get easily maintainable output in both HTML and PDF. See for yourself. – Serge Stroobandt Nov 13 '13 at 2:12
• If you are only targeting HTML output (no epub or docbook), you could also give a short to ConTeXt's XML export (which works with most modern browsers). – Aditya Nov 13 '13 at 4:10
• To give a concrete example this tex file generates this html and other css files that are included in the gist. – Aditya Nov 13 '13 at 4:40

The standard way to place a caption on a wrapped figure is as follows:

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

\startplacefigure
[
title={A short caption},
location={left, nonumber},
]
\externalfigure[cow][width=2cm]
\stopplacefigure

\input knuth

\stoptext


which gives:

If you want to use this in an automated way with Pandoc, one option is to redefine the \startdescription macro as follows:

\define[1]\startdescription
{\dostartdescription#1\dostopdescription}

\def\dostartdescription#1#2\dostopdescription
{\setbox\scratchbox\vbox{\setupexternalfigure[width=4cm]#1}%
\ifdim\ht\scratchbox>\dimexpr\measure{pageheight}\relax
\page
\fi
\startplacefigure[location={left,high,nonumber}, style=\ss\bfxx\setupinterlinespace, title={#2}]
\box\scratchbox
\stopplacefigure
}

\define\stopdescription{\endgraf}


which works correctly with the example posted in the question.

However, I am not convinced that this is the right approach to this problem. Personally, I find it easier to preprocess the markdown input. See this blog post for my views.

• I edited your great answer to include the caption styling and updated the image accordingly. – Serge Stroobandt Nov 13 '13 at 20:34
• I have a quite long comment concerning spacing below the image. The comment would not fit here. Please, see my tentative answer below. – Serge Stroobandt Nov 13 '13 at 21:33

## The accepted answer can be still improved upon

The height of the wrapped text is off by one line, or stated differently, the space below the image is too big. This happens independently of whether a caption is present or not. Here is an even more dramatic example made using the same code:

I think, I also found a reason why, over at ConTeXtGarden:

## Images at the beginning of a paragraph

Using \externalfigure[...] at the beginning of a paragraph results in a line break after the image. This is because \externalfigure is a \vbox. > When a \vbox is encountered at (what appears to be) the beginning of a paragraph, vertical mode is maintained. To prevent this, add \dontleavehmode before \externalfigure, like this:

\dontleavehmode
\externalfigure[...] ... first line ...


The problem now is that —despite many desperate attempts— I am not succeeding at integrating this \dontleavehmode in the code of the accepted answer.

## Edit: Improved solution with reduced spacing under the caption

The code referred to in Aditya's comment below, correctly reduces the white space under wrapped figure captions. Here is the result for the original example:

• Add the following code to your preamble. ConTeXt adds structht to the height of the side float while determining the number of lines to hang. This works well under the assumption that the captions are typeset in the same size as the main content. If captions are typeset in a smaller font, then the structht is not needed. – Aditya Nov 14 '13 at 0:37
• Your code performs wonderfully! With all we have learned here, you should really start to consider wrapping things up and contributing this code to the ConTeXt developers as a new description environment. The only issue which remains is the unfortunate page breaking between section titles and this description environment. I will create a last question out of this and point the other developers in the newsgroup to it. Stay put! – Serge Stroobandt Nov 14 '13 at 10:29