Bug introduced in 12.0 or earlier and persisting through 12.1

Create a notebook having interspersed cells of figures and text:

 CellPrint@TextCell[Plot[Sin[x], {x, 0, 10}], "Text"];
 CellPrint@TextCell[ToString[i], "Text"],
 {i, 20}

Now save it as PDF. If you use the same paper size as me, there will be a page break after the cell containing 3. Edit the cell and add PageBreakBelow -> False. Alternatively, edit the next cell and add PageBreakAbove -> False. This can be done with the Option Inspector.

But the page will still be broken at that point.

Page breaks can also be visualized without printing, by setting ShowPageBreaks -> True on the notebook and setting the Printout environment.

Why does Mathematica not respect page break instructions? How can I force it to not break the page between these two cells?

What I wanted was to create a figure caption cell style that stays together with the figure above, but I can't even get the page break options to work reliably when set explicitly on a cell, not to mention when included in a style.


Update 2

I've send a bug report to the official support on this issue: [CASE:4409941]. Just in case, I reproduce it here:

The goal is to prevent addition of page breaks between the Caption cells with numbers and the next cells.

According to the Documentation, it should be possible to achieve this simply by adding PageBreakBelow->False to the Caption cells, but it doesn't work. Even in the case of grouped cells and with option GroupPageBreakWithin->False it doesn't work. Addition of PageBreakAbove->False to the next cells also doesn't change anything.

Unexpectedly, addition of PageBreakWithin->False gives the desired result, in full contradiction with the documented meaning of this option and with undesired effect associated with its documented meaning.

The code below demonstrates the problem: pay attention to the bottom of each imported page: the Caption cell number 6 at the bottom is separated from the next cell containing "line"s by the page break in the case of nb[2] despite the fact that every of the used options should prevent this according to the Documentation: PageBreakBelow->False and GroupPageBreakWithin->False and PageBreakAbove->False. But none of them works, as well as all them together! The case of nb[3] demonstrates the expected behavior for nb[2]: the caption cell number 6 is on the page 2 with the next cell - but it is achieved by using the option PageBreakWithin->False which shouldn't behave like this, according to the Documentation!

(* The default state *)
nb[1] = CreateDocument[
   Flatten@Table[{TextCell["Caption" <> ToString[i], "Section", "Text"], 
      TextCell[Column[Table[line, {9}], Center, 0.07], "Text"]}, {i, 20}], 
      ShowPageBreaks -> True, WindowSelected -> False];
(* An attempt to use PageBreakBelow->False and 
GroupPageBreakWithin->False and PageBreakAbove->False: failure. 
Try to use them separately and/or without grouping (remove "Section" style)
- it also fails in the all cases! *)
nb[2] = CreateDocument[
   Flatten@Table[{TextCell["Caption" <> ToString[i], "Section", 
       "Text", PageBreakBelow -> False, GroupPageBreakWithin -> False], 
      TextCell[Column[Table[line, {9}], Center, 0.07], "Text", 
       PageBreakAbove -> False]}, {i, 20}], ShowPageBreaks -> True, 
   WindowSelected -> False];
(* An attempt to use PageBreakWithin->False: works, but why? It 
shouldn't work in the sense of GroupPageBreakWithin->False! *)
nb[3] = CreateDocument[
   Flatten@Table[{TextCell["Caption" <> ToString[i], "Section", "Text"], 
      TextCell[Column[Table[line, {9}], Center, 0.07], "Text"]}, {i, 20}], 
      PageBreakWithin -> False, ShowPageBreaks -> True, WindowSelected -> False];
Row[Row /* Panel /@ 
  Table[Show[#, ImageSize -> {Automatic, 500},  PlotRange -> Automatic] & /@ 
    ImportString[ExportString[nb[i], "PDF"]][[;; 2]], {i, 3}]]
Do[NotebookClose[nb[i]], {i, 3}]



I have checked and found that GroupPageBreakWithin -> False options is ignored too, both for CellGrouping -> Automatic and CellGrouping -> Manual. Here are corresponding pieces of code:

 Flatten@Table[{TextCell[ToString[i], "Section", 
     GroupPageBreakWithin -> False], 
    ExpressionCell[Plot[Sin[x], {x, 0, 10}], "Text", 
     GroupPageBreakWithin -> False]}, {i, 20}], 
 GroupPageBreakWithin -> False]

 Table[CellGroup[{TextCell[ToString[i], "Section", 
     GroupPageBreakWithin -> False], 
    ExpressionCell[Plot[Sin[x], {x, 0, 10}], "Text", 
     GroupPageBreakWithin -> False]}], {i, 20}], 
 CellGrouping -> Manual, GroupPageBreakWithin -> False]

In both cases the first break comes after "4", in full contradiction with the Documentation for GroupPageBreakWithin. Definitely a bug.

Original answer

I confirm the problem with versions 12.0 and 12.1 on Windows 10 x64: both File ► Save As... ► PDF Document (*.pdf) and actual printing to a PDF printer don't respect the PageBreakBelow -> False and PageBreakAbove -> False settings. The same is true for the Export function:

Export["test.pdf", EvaluationNotebook[]] // SystemOpen


Worth reporting as a bug.

I also noticed that the File ► Print Preview... mode is very unstable and buggy: sometimes it visualizes the page breaks with ShowPageBreaks -> True, sometimes not, and even worse: this behavior and positions of the page breaks change with changing the Magnification. Here are screenshots of the same page (generated as described in the OP) in the "Print Preview" mode with ShowPageBreaks -> True set, but with different magnifications (the cursor is placed right after the cell containing "3" in the all cases):






Notice how "3" and the insertion point indicator are rendered after the page break at magnifications 50% and 75% (also notice that the break itself is rendered differently as compared to other magnifications) and how the page break isn't shown at magnification 150%. The page break also disappears after resizing the window by mouse...

| improve this answer | |
  • 1
    $\begingroup$ I was able to get it working when I set PageBreakWithin -> False on the figure cell and PageBreakBelow -> False on the cell containing 3. This works. But it's inconvenient as I was hoping to be able to have a cell which always stays attach to the cell above (or below), without having to also modify the cell above (or below). I wanted to do this to create figure captions. $\endgroup$ – Szabolcs Mar 24 at 12:57
  • $\begingroup$ @Szabolcs Thank you, the combination of PageBreakWithin -> False for the figure cell and PageBreakBelow -> False on the cell containing 3 works. Good to know that there is a way to control page breaking at all! May be it is also worth to try other combinations: GroupPageBreakWithin -> False for the cell with 3 (and auto-group it with next cell) and PageBreakWithin -> False for all cells with graphics, don't know whether it fits your needs. $\endgroup$ – Alexey Popkov Mar 24 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.