2
$\begingroup$

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}];
$\endgroup$
1
  • $\begingroup$ @m_goldberg Thank you for improving the question. $\endgroup$
    – hadi
    Commented Oct 19, 2014 at 18:00

1 Answer 1

2
$\begingroup$

Using

NotebookWrite[nb, Cell[{ToBoxes[question]}]]

or

NotebookWrite[nb, Cell[{ToBoxes@question}, "Input", CellMargins -> {{0, 10}, {5, 10}}]];

instead of

NotebookWrite[nb, ToBoxes[question]]

gives

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you. Solved the problem perfectly. Still getting to grips with what can go inside what. Tried putting the question in a cell but not wrapping it all in a cell. Don't understand the use of @ in second solution. Documentation says it's Prefix. I'll keep reading. $\endgroup$
    – hadi
    Commented Oct 19, 2014 at 19:20
  • 1
    $\begingroup$ @hadi, my pleasure. Welcome to Mathematica.SE. $\endgroup$
    – kglr
    Commented Oct 19, 2014 at 19:26
  • $\begingroup$ @hadi, f@x is same as f[x].. $\endgroup$
    – kglr
    Commented Oct 19, 2014 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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