# Keeping section heading with body paragraph & image (context)

How can you get Context MkIV to automatically put in page breaks to keep \section, etc. headings with the paragraph when accompanied with a floating image, instead of putting the heading at the end of a partial page and the first paragraph wit image in that section at the top of the next page? Adding \testpage to the sectioning macros doesn't seem a general solution because there is often space for a line of two of text, just not enough for the image.

MWE:

\setuppapersize[letter]
\setupexternalfigures[location=default]

\starttext

\chapter{Chapter}

\section{Section 1}

\input lorem.tex
\input lorem.tex

\section{Section 2}

\placefigure [right]{cow}
{\externalfigure[cow][height=2in]}

\input lorem.tex

\stoptext


This puts the section heading on page one, with the text and image on page two. I'd like to put the section heading with the text and image, without manually adjusting every time the document changes.

Note: there is an solution to a related problem at ConTeXt: Wrapped text misbehaves at page breaks that might be adapted, as it also dealing with finding the size of an image, but I'm a beginner to Context, and not ready to jump into the deep end.

Edit: \testpage is not a general solution without a way to automatically measure the following image in "number of line", the somewhat arbitrary input to \testpage

I am not aware of how to solve the general problem of completely preventing a page break between section title and first paragraph. However, since you provide a height of 2 inches to your image, it's pretty straightforward to convert that height into lines and just use testpage

The getnoflines command seems to calculate the height of something in lines.

Converting inches to lines:

\getnoflines{2in}
\the\noflines


So you can just define a modification of testpage that uses inches as arguments:

\setuppapersize[letter]
\setupexternalfigures[location=default]

\def\Checkpage#1{%
\getnoflines{#1}
\testpage[\the\noflines]%
}

\starttext

\chapter{Chapter}

\section{Section 1}

\input lorem.tex
\input lorem.tex

\Checkpage{3in}
\section{Section 2}

\placefigure [right]{cow}
{\externalfigure[cow][height=2in]}

\stoptext


This isn't exactly automatic but your figure placement isn't automatic either... so you can just add a Checkpage command as needed. And if your figures are always the same height, then you can define an alternative command that uses the fixed figure height.

I used 3 inches rather than 2 as a rough proxy for figure caption and the like.

• One more step toward my preferred solution. Forgive my using general programming terms; I don't know the TeX equivalents yet . What I'd like is to render the image to a buffer, "box" I believe they're called, put the left/right placement in a variable, and have the sectioning macros check that buffer, and if the buffer is not empty, add a page break if needed, and place the buffer, clearing it so it doesn't trigger the action again unless a new image has been placed in the buffer. That solution can "fix" the headers as orphans problem as well. – Scott Lovell Nov 10 '19 at 12:41
• @ScottLovell In general, if you want to do heavy programmatic lifting in ConTeXt, it might be best to do so in Lua. In case you haven't seen the manual yet: pragma-ade.nl/general/manuals/cld-mkiv.pdf. Your requirements seem pretty specific and I'm not sure I can help with all that, but nailing down what specifically you are trying to accomplish rather than how you want to go about it might help. For example, if this figure is indeed automated and always appears in the first paragraph, it would lead to a simpler solution then trying to solve some more general case... – ssokolen Nov 11 '19 at 15:50