5

I want to avoid displayed formulas at the top of pages in ConTeXt (in fact, my big document has several displayed formulas on top of pages), and I have read this answer on the subject.

Thus, I played with \predisplaypenalty, which by default is set to 0 in ConTexT. I used the following code:

\predisplaypenalty=10000

\starttext
\dorecurse{3}{\input knuth\par}
\input knuth text without a new paragraph
\startformula
\exp(x)=\sum_{k=0}^{+\infty}\frac{x^k}{k!}.
\stopformula
Still we have \tex{predisplaypenalty} set to \the\predisplaypenalty
\stoptext

The top of page 2 looks like this:

output of the code, showing the formula on top

In the source, predisplaypenalty is mentioned in some files, for example in strc-mat.mkiv where it is set to \zerocount. In syst-pln.mkiv, which is a module setting "a couple of variables to the Plain TeX values" it is set to 10000, but that is probably overwritten (if read) in strc-mat.mkiv.

Question What is the correct way to avoid displayed formulas on top of pages in ConTeXt?

Just to clarify, you can assume that I use the latest standalone version of ConTeXt.

Update I changed the code so that there is no paragraph break before the formula. This was not the problem.

mickep
  • 8,685
  • Isn't this because you added a \par before the start of \startformula? LaTeX does the same thing. – daleif Aug 30 '17 at 09:38
  • @daleif probably, I will check when I get back to the computer. Thanks! – mickep Aug 30 '17 at 09:53
  • @daleif In fact that was not the problem this time. Luckily (for me), Henri was able to figure out a way to solve the problem. – mickep Aug 30 '17 at 10:44

1 Answers1

6

The first thing to notice is that you shouldn't set counters like \predisplaypenalty by hand. There is a high-level interface for almost everything in ConTeXt. The correct way to handle this is to set \setupformulas[spacebefore={big,samepage}]. Now this is where the fun begins...

In strc-mat.mkiv we find

\c!spacebefore=\v!big,
\c!spaceafter=\formulaparameter\c!spacebefore,

This makes the spaceafter always equal to the spacebefore by default. So we have to come up with a more sensible variant, which is

\setupformulas[spacebefore={big,samepage},spaceafter=big]

If you do that the formula is still preceded by a page break. Hm, what is going on? Some investigation led me to find that spacebefore is inserted in some overly complicated manner which messes with \prevdepth. Now \prevdepth is used by \blank to make certain decisions about page breaking which in this case breaks our carefully crafted setup. Is there a way out?

Yes, there is. As always there is some undocumented magic in ConTeXt and in this case we can simply change the way space is inserted before the formula by flicking one switch (the default is \plusthree).

\c_strc_formulas_space_model=\plustwo

I'll investigate further. Maybe I find the offending bit and can show it to Hans, so he can fix it.

\setupformulas[spacebefore={big,samepage},spaceafter=big]

\unprotect
\c_strc_formulas_space_model=\plustwo
\protect

\starttext

\dorecurse{4}{\input knuth\par}
\startformula
  \exp(x)=\sum_{k=0}^{+\infty}\frac{x^k}{k!}.
\stopformula

\stoptext

enter image description here

Henri Menke
  • 109,596
  • 1
    Many thanks! I actually tried the spacebefore and spaceafter with samepage (I forgot to mention them in the question), but without luck. I would never have figured out the trick with formula space model. Thanks! (Also, I hoped there was going to be a ConTeXt way of doing this, this is why I formulated the question as I did.) – mickep Aug 30 '17 at 10:43
  • 2
    @mickep I think it's a bug. Reported here: https://mailman.ntg.nl/pipermail/ntg-context/2017/089719.html – Henri Menke Aug 30 '17 at 10:45