# Problems with multiple \starthanging \stophanging in ConTeXT

I'm trying to place multiple figures with additional side text using ConTeXT one after the other.

When there are two figures in a row they overlap. How should they be separated?

The following code creates overlapping figures when I run ConTeXT.

\starthanging{
\placefigure[force][fig:first]{Caption 1} {\externalfigure[first-figure][width=\textwidth]}}
\inother[width=5cm]{Some text for the other bit}
\stophanging

\starthanging{
\placefigure[force][fig:second]{Caption 2} {\externalfigure[second-figure][width=\textwidth]}}
\inother[width=5cm]{Some text for the other bit 2}
\stophanging


What I'm trying to do is this:

-
I am not sure what you are trying to do. Why use \starthanging with a figure that has width=\textwidth. Perhaps you can post an image that shows the desired output. –  Aditya Apr 17 '13 at 19:09
–  Aditya Apr 30 '13 at 5:41

As Aditya mentioned, using hanging is the wrong approach here. In the thread he linked to Hans and Wolfgang already provide two solutions. I'll present a third one, based on Wolfgang's solution, with the following features:

• The additional text is typeset in the margin, not just close to the figure.
• It uses the margindata interface. This allows the margin text to be manipulated using the usual ConTeXt interface.
• I attempted to avoid low-level commands as much as possible.
• The additional figure text is optional.
• Additional margin texts are centred by default. If the its height exceeds the figure height, they are top aligned.

### The code

\useMPlibrary [dum]  %% only for the screenshot

\setuppagenumbering
[alternative=doublesided]

\definemargindata
[FigureMargin]
[inouter]
[voffset=-\dimexpr\nextboxht-\strutheight\relax]

\definemarginframed
[FigureMargin]
[height=\nextboxht,
align={flushleft, lohi}]

\starttexdefinition FigureText
\dowithnextbox{
\startlinealignment[middle]
\copy\nextbox
\doifsomething{\floatuserdataparameter{text}}
{\FigureMargin{\floatuserdataparameter{text}}}
\stoplinealignment
}
\hbox
\stoptexdefinition

\setupfloat
[figure]
[command=\FigureText]


First, an individual margin data set is defined. To correctly align the margin text, it uses a frame with the all the settings that frames usually accept.

Then the command FigureText is defined, which typesets the figure content and the additional margin text. It is not responsible for the caption. The command FigureText takes one argument, which is the figure content. The dowithnextbox trickery allows easy access to the dimensions of the box, which is used to properly align the margin text. This allows the definition of the macro without a parameter.

The margin text is passed as a second argument to \startplacefigure and picked up using \floatuserdataparameter.

### Example

Here is an example how to use it:

\showframe
\starttext
\dorecurse{2}{%%
\startplacefigure
[title=Some figure]
[text=Some additional text for the margin.]
\externalfigure
\stopplacefigure

\startplacefigure
[title=Another figure]
[text=Some more margin text for the other figure.]
\externalfigure [dum] [width=\textwidth]
\stopplacefigure
}
\stoptext


### Caveats

The height of the margin texts is not taken into account. If floats are placed consecutively, margin texts might overlap. Example:

\starttext
\dorecurse{2}{%%
\startplacefigure
[title=Some figure]
[text=\input ward\par]
\externalfigure
\stopplacefigure}
\stoptext


-