Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to display the following header on of each page of an exported pdf

                                          specificTitle XXX

XXX should be the page number while specificTitle should be a custom title that I would like to be able to set in the Front End Cells.

Currently I am using the following code to the add the Page header. Unfortunately I can't seem to figure out a way to set the title while in the notebook front end. EDIT: To help clarify I am looking to not necessarily have separate titles on each page, but have a way to set different titles for each group of pages.

SetOptions[EvaluationNotebook[],
  PrintingOptions -> {"FirstPageHeader" -> True},
  PrintingStartingPageNumber -> 1,
  PageHeaders -> {
    {Cell[TextData[{CounterBox["Page"]}], "PageNumber"], None, None},
    {None, None, Cell[TextData[{CounterBox["Page"]}], "PageNumber"]}
    },
  PageFooters -> {{None, None, None}, {None, None, None}}
  ];

Originally I planed to use Dynamic cell inside the PageHeaders like so

Cell[BoxData[
  DynamicBox[ToBoxes[$CellContext`a, StandardForm]]], "Output"]

And then I planned to set a at different points throughout the notebook. It becomes clear if you render this that each page of the pdf isn't rendered separately but instead the entire notebook is rendered(and executed) then the notebook is converted to a pdf. This makes sense but makes my current approach impractical.

share|improve this question
    
Related (but not dupe): Some tutorials on formatting notebook for exporting to pdf. –  Silvia Jan 1 at 4:04
    
Also related and may be useful Determine page count programmatically –  Liam William Jan 1 at 19:46
    
Other than calculating the page height, you can also use PageBreakAbove and PageBreakBelow options to force a pagebreak. –  Silvia Jan 2 at 5:56
add comment

2 Answers

up vote 2 down vote accepted

You can use the running text option. You can find it at Printing Settings -> Headers and Footers....

running text setting

It will generate a Dynamic-object in the PageHeaders option of the target Notebook, which will read the contents of Cells with specified style and position per page:

running text dialog

One possible way would be put special styled Cells on every page (in the following example, it's the "RunningText" style), set them closed so they won't be printed, and link the running text to their style.

A minimalist example:

{
 Cell["the 1st page-wise header", "RunningText", CellOpen -> False],
 Cell[FromCharacterCode[{RandomInteger[{33, 126}, 30], 10} & /@ Range[20] // Flatten], "Text"],
 Cell["the 2nd page-wise header", "RunningText", CellOpen -> False],
 Cell[FromCharacterCode[{RandomInteger[{33, 126}, 30], 10} & /@ Range[20] // Flatten], "Text"]
} //
Notebook[#,
         ShowPageBreaks -> True,
         PrintingOptions -> {"FirstPageHeader" -> True, "PaperSize" -> {500, 450}},
         PageHeaders -> {
                         {None,
                          Cell[TextData[{
                                         Cell[BoxData[StyleBox[
                                                      FormBox[
                                                              DynamicBox[ToBoxes[FEPrivate`RunningText["RunningText", All, "(no content)", " .,", {}, True, 0], StandardForm]], 
                                                              TextForm], FontSize -> 20]]],
                                         " ", "|", " ",
                                         StyleBox[CounterBox["Page", CounterFunction :> (FEPrivate`StringJoin["\[Tilde]", FEPrivate`ToString[#1], "\[Tilde]"] &)], "PageNumber", FontSize -> 20]
                                       }], "Header"],
                          None},
                         {None,
                          Cell[TextData[{
                                         Cell[BoxData[StyleBox[
                                                      FormBox[
                                                              DynamicBox[ToBoxes[FEPrivate`RunningText["RunningText", All, "(no content)", " .,", {}, True, 0], StandardForm]], 
                                                              TextForm], FontSize -> 20]]],
                                         " ", "|", " ",
                                         StyleBox[CounterBox["Page", CounterFunction :> (FEPrivate`StringJoin["\[Tilde]", FEPrivate`ToString[#1], "\[Tilde]"] &)], "PageNumber", FontSize -> 20]
                                       }], "Header"],
                          None}
                         }
        ] & // NotebookPut

printed pdf sample

share|improve this answer
1  
Thank you for the response. I need to do a little more testing, but this appears to be exactly what I was looking for. I'm not 100% sure how consistent(and how it internally works) FEPrivate`RunningText but it seems to work perfectly right now. –  Liam William Jan 1 at 8:47
    
@LiamWilliam You're welcome :) The programming setting is the same thing with the setting wizard, but may provide more customization. –  Silvia Jan 1 at 8:56
add comment

You can do this using CounterFunction:

   SetOptions[EvaluationNotebook[],
      PrintingStartingPageNumber -> 1,
      PrintingOptions -> {"FirstPageHeader" -> True},
      PageHeaders -> {
        {Cell[TextData[{CounterBox["Page"]}], "PageNumber"],  
         Cell[TextData[
           CounterBox["Page", 
            CounterFunction :> (Part[{"Header 1", "Header 2", "Header 3", 
                 "Header 4", "Header 5"}, #] &)]], "Header"], None},
        {None, 
         Cell[TextData[
           CounterBox["Page", 
            CounterFunction :> (Part[{"Header 1", "Header 2", "Header 3", 
                 "Header 4", "Header 5"}, #] &)]], "Header"],
                Cell[TextData[{CounterBox["Page"]}], "PageNumber"]}},
      PageFooters -> {{None, None, None}, {None, None, None}}];

and replace {"Header 1", "Header 2", "Header 3", "Header 4", "Header 5"} with your list of headers.

share|improve this answer
    
Thank you :) That is a good idea. Unfortunately this won't work if you don't know the number of pages the output will be. For example I am looking to set a page break at different points throughout the notebook and would like every page between pagebreak 1 and pagebreak 2 to have specific header. –  Liam William Jan 1 at 6:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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