# ConTeXt : how to prevent page breaks

There is a common thing that bothers me about the way ConTeXt makes page breaks : there are things I'd rather not have in first line. For instance, if the following code snippet is found within a longer document

which leads to the result
\startformula
a = b
\stopformula


it may produce an output where the text is the last line of a page and the formula is starting the next page. Another exemple is the way I manage exercises in separate files, which look like

\exercisetitle{My title}
\startmode[questions]
Here are the questions
\stopmode
\stopmode


I often find the title at the bottom of a page and the beginning of the exercise on the next page, while the page break would be better before the title in order to keep the title and the exercise stuck together. However, I can't simply use a \vbox, since the exercise may be long and a page break inside the exercise may occur without problems.

I can manage it by hand, but it is quite stupid since the aim of a such a tool is to be free (at least, most of the time) of these minor adjustements. What are the configuration options that I could put in my environment file to regulate this behavior ? I am aware of \page[no], but it is not that automated, and sometimes just doesn't work (for instance, I the following MWE works, but I had an exactly similar situation in my real documents where the \page[no] command didn't work.

\setuppapersize[A5][A5]
\defineitemgroup[questions][levels=2]
\setupitemgroup[questions][1][n,packed]
\setupitemgroup[questions][2][a,packed,repeat][stopper=)]
\starttext
% A bit of text to reach the end of the page
\input knuth
\blank
\input knuth
% The useful part
{\sl My dear fellow TeX users}, I would like to introduce you the following itemize :
\page[no]
\startquestions[columns]
\item one !
\item two !
\item three !
\item four !
\item five !
\item six !
\stopquestions
\stoptext

• Better use \blank[samepage] instead of \page[no]. The latter only works in vertical mode and will produce no result if applied in horizontal mode (which might happen by accident). Aug 25, 2016 at 9:46
• Add autointro to your options: \setupitemgroup[questions][1][n,packed,autointro] Aug 25, 2016 at 17:12

Unfortunately, \defineitemgroup does not have a spacebefore option but instead you can put \blank[samepage] into the before key. Where spacebefore is available it makes more sense to use that, for instance with formulas: \setupformula[spacebefore={samepage}].

N.B.: By default itemgroup has \blank sitting in the before key. Without any optional argument \blank will insert a blank of size \currentvspacing. To maintain this amount (which is lost when saying \blank[samepage]) one has to include it, i.e. \blank[\currentvspacing,samepage]. This is somewhat ugly but I don't know a better way to fetch the current spacing value.

\setuppapersize[A5][A5]
\defineitemgroup[questions][levels=2,before={\blank[samepage]}]
\setupitemgroup[questions][1][n,packed]
\setupitemgroup[questions][2][a,packed,repeat][stopper=)]
\starttext
% A bit of text to reach the end of the page
\input knuth
\blank
\input knuth
% The useful part
{\sl My dear fellow TeX users}, I would like to introduce you the following itemize :
\startquestions[columns]
\item one !
\item two !
\item three !
\item four !
\item five !
\item six !
\stopquestions
\stoptext

• It may be better to use intro or autointro keywords Aug 25, 2016 at 17:14

For itemize and itemgroups, you can use intro keyword to prevent pagebreak before the start of the itemgroup. For example:

\setuppapersize[A5][A5]
\defineitemgroup[questions][levels=2]
\setupitemgroup[questions][1][n,packed,intro]
\setupitemgroup[questions][2][a,packed,repeat][stopper=)]
\starttext
% A bit of text to reach the end of the page
\input knuth
\blank
\input knuth
% The useful part
{\sl My dear fellow TeX users}, I would like to introduce you the following itemize :
\startquestions
\item one !
\item two !
\item three !
\item four !
\item five !
\item six !
\stopquestions
\stoptext


It also prevents page breaks between the items if there are only two items.

Another option is to use the autointro key, that prevents a page break before the start of the item group if the previous paragraph is less than two lines. So, if you are inserting an itemgroup after a section head, you could also use autointro

Unfortunately, these options don't work with columns option before \startmixedcolumn comes too early in the definition.

• You should remove \page[no] to show off that it really works... but for me it then doesn't :( Aug 25, 2016 at 19:35
• @HenriMenke: Oops, didn't see the page[no]. Using intro works without the column. so the "bug" is with columns. Aug 25, 2016 at 19:49
• Is it possible to use intro or autointro as arguments in the startquestions command ? Depending on the context, it can be perfectly acceptable to have a page break, so it must be tunable... Aug 25, 2016 at 22:32
• Yes. You can use the as option of \startquestions as well Aug 25, 2016 at 22:55