I'm trying to generate some worksheets using NotebookWrite
so that I can control the page breaks, but have got stuck on the alignment as there appears to be a difference between how Cell
and ToBoxes
are treated. Or maybe it's my formatting.
The instructions are text in a Cell with PageBreakAbove
. Output using NotebookWrite
without modification. They align with the left margin.
The questions are a row of graphics and text. Output using NotebookWrite
and ToBoxes
. They do not align with the left margin.
Styles are the same.
Previously, I collected all the outputs to a variable and created a new notebook; everything lined up fine, but I couldn't control the pagebreaks, hence this approach.
How do I format correctly so everything aligns with the margins?
This is the code:
ClearAll["Global`*"]
hfStyle = {FontFamily -> "Helvetica", 12, Black, Italic};
leftHeader = Cell[TextData[{StyleBox["Left Header", hfStyle]}]];
rightHeader = Cell[TextData[{StyleBox["Right Header", hfStyle]}]];
leftFooter = Cell[TextData[{StyleBox["Left Footer", hfStyle]}]];
rightFooter = Cell[TextData[{StyleBox[CounterBox["Page"], hfStyle]}]];
bookFormat = {
PageHeaders -> {{leftHeader, None, rightHeader}, {leftHeader, None,
rightHeader}},
PageFooters -> {{leftFooter, None, rightFooter}, {leftFooter, None,
rightFooter}},
PageHeaderLines -> {True, True},
PageFooterLines -> {True, True},
PrintingStyleEnvironment -> "Printout",
CellGrouping -> Manual,
WindowSize -> {737, 613},
WindowMargins -> {{Automatic, 0}, {Automatic, 0}},
PrintingCopies -> 1,
PrintingStartingPageNumber -> 1,
PrintingPageRange -> {1, Automatic},
PrintingOptions -> {
"PaperSize" -> {695, 842},
"FirstPageHeader" -> True,
"FirstPageFooter" -> True,
"PageHeaderMargins" -> {45, 45},
"PageFooterMargins" -> {45, 45},
"PrintingMargins" -> {{45, 45}, {0, 56}},
"PrintCellBrackets" -> False},
ShowGroupOpener -> False,
ShowCellLabel -> False,
FrontEndVersion ->
"9.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (November 20, 2012)",
StyleDefinitions -> "Default.nb"};
nb = CreateDocument["", bookFormat, ShowPageBreaks -> True];
a = 0;
t = Table[{h, 0}, {h, 1, 12}];
alignCellStyle =
{ImageSize -> Full, ImageMargins -> {{0, 0}, {40, 0}}, TextAlignment -> Left};
alignRowStyle =
{ImageSize -> Full, ImageMargins -> {{0, 0}, {40, 0}}, Alignment -> Left};
textRowStyle = {"Text", "Helvetica", Plain, 16};
instructions =
Cell[
TextData[{
StyleBox[
"Instructions (controlled page break, text aligned to left margin)",
textRowStyle]}],
alignCellStyle, PageBreakAbove -> True];
Table[
question =
Row[{
ClockGauge[t[[i]], GaugeStyle -> {Blue, Red}, ImageSize -> 120],
Style[" ______ o'clock (not aligned to left margin)",
textRowStyle]},
alignRowStyle];
If[Divisible[a++, 4],
NotebookWrite[nb, instructions]; SelectionMove[nb, Next, Cell]];
NotebookWrite[nb, ToBoxes[question]];
SelectionMove[nb, Next, Cell],
{i, 1, 8}];