# PageBreakAbove -> True breaks CellMargins

Top CellMargin of the cell with PageBreakAbove is ignored in SlideShow environment (or for ShowPageBreaks -> True).

CreateDocument[
{ Cell["s1", "Section"
, Background -> LightBlue
, CellMargins -> 100 {{1, 1}, {1, 1}}
]
, Cell["s2", "Section"
, Background -> LightBlue
, CellMargins -> 100 {{1, 1}, {1, 1}}
, PageBreakAbove -> True
]
}
, ScreenStyleEnvironment -> "SlideShow"
]


If "CellBreak" cell is put manually it works:

CreateDocument[
{ Cell["s1",...], Cell["", "PageBreak"], Cell["s2", ...] }
, ScreenStyleEnvironment -> "SlideShow"
]


But I need automatic page breaking.

Is there any fix? Is this a bug?

In addition to Slideshow environment it also breaks .pdf printout.

## Update: How to fix the bug

I've found where the bug lives: it is the option CellGroupingRules -> {"SectionGrouping", 30} of the built-in "Section" style. Overriding this option with anything like CellGroupingRules -> Automatic recovers the lost upper cell margin:

CreateDocument[
{ Cell["s1", "Section"
, Background -> LightBlue
, CellMargins -> 100 {{1, 1}, {1, 1}}
]
, Cell["s2", "Section"
, Background -> LightBlue
, CellMargins -> 100 {{1, 1}, {1, 1}}
, PageBreakAbove -> True
, CellGroupingRules -> Automatic
]
}
, ScreenStyleEnvironment -> "SlideShow"
]


I think that this fix proves that the behavior described in the question is a bug. Please report it to the support.

Is this a bug?

Probably only the developer can give definitive answer, but there are high chances that this behavior is "by design" since (emphasis is mine):

CellMargins is an option for Cell that specifies the absolute margins in printer's points to leave around a cell.

PageBreakAbove is an option for Cell which specifies whether a page break should be made immediately above the cell if the notebook that contains the cell is printed.

From the above it is natural to expect that the page break immediately above the cell shouldn't be above the upper cell margin (which itself is immediately above the cell). So probably it isn't a bug (but still worth to ask the support).

What indeed looks like a bug is that if we add PageBreakBelow -> True for the first cell (and remove PageBreakAbove -> True from the second), we get exactly the same: the upper margin of the second cell is lost, but the bottom margin of the first cell is kept! So I recommend to file a bug report for PageBreakBelow.

As a workaround one can use CellFrameMargins (with appropriate setting for CellFrame which should be turned on for the corresponding side of the cell) instead if CellMargins because CellFrameMargins doesn't exhibit the described behavior in conjunction with PageBreakAbove. A drawback is that it changes the background filling (everything inside of the CellFrame is filled by the Background option).

• Thanks again for your insights. Here's a better representation of my real case: CreateDocument[{Cell["Section", "Section"], Cell["s2", "Test", PageBreakAbove -> True], Cell["s1", "Test"]}, ScreenStyleEnvironment -> "SlideShow", StyleDefinitions -> Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]], Cell[StyleData["Test"] , CellGroupingRules -> {"GroupTogetherGrouping", 0} , Background -> LightBlue , CounterIncrements -> "Test" , CellMargins -> {{50, 50}, {50, 150}} ]}]] So unfortunately setting CellGropuingRules for Test cells would... – Kuba Aug 26 '17 at 20:45
• ...would break my plan but your fix works even when I set it for the Section above the cell break, which allows me to presereve Test's GroupTogetherGrouping. So many thanks and indeed there appears to be a problem with section grouping. – Kuba Aug 26 '17 at 20:47
• Received the response, see my answer. Had to drop the bug tag. – Kuba Aug 29 '17 at 7:46

### tl;dr; fix:

CreateDocument[
{ Cell["s1", "Section", CellMargins -> 100 {{1, 1}, {1, 1}}]
, Cell["s2", "Section"
, CellMargins -> 100 {{1, 1}, {1, 1}}
, PageBreakAbove -> True
, CellElementSpacings -> {"ClosedGroupTopMargin" -> 100}
]
}
, ScreenStyleEnvironment -> "SlideShow"
]


### WRI Support response

"It's using the "ClosedGroupTopMargin" instead. This is preferentially used whenever the previous cell ends a closed cell group or the previous cell has the CellGroupingRules set to use "SectionGrouping". The behavior is not documented, but it's most definitely not a bug."

"ClosedGroupTopMargin" is a non-documented sub-option of the CellElementSpacings notebook option. As mentioned above, its behavior is not documented, but the option itself is preferred whenever the previous cell has the CellGroupingRules set to "SectionGrouping". Because of this, the workaround to set CellGroupingRules to Automatic works.

• CASE:3934040

So using CellElementSpacings -> {"ClosedGroupTopMargin" -> 50} does what I need.
Worth to mention that "ClosedGroupTopMargin" greater than parent top CellMargins will be clipped to the latter so 100 will still be 50 if CellMargins is e.g. {{0, 0}, {0, 50}}