I would like to convert a collection of digitized documents to Mathematica notebooks. They are formatted as XML, divided into pages. The XML contains formatting I would like to preserve, but I do not want to display page divisions, in other words I would like the text to flow like a typical notebook. However, I would like to use a docked cell to display the page range of any text that is selected. A text selection could come from a single page or multiple pages.

My question is, what is the best way to represent this data? I imagine StyleBoxes and TagBoxes (to hold page numbers) but I don't have a clear understanding of how these interact. When I try to nest one inside the other (TaxBox[StyleBox[...]] or StyleBox[TagBox[...]]) I end up with cells that just print as expressions. I also imagine I could use InterpretationBoxes to store the formatted text and then a hidden duplicate copy with the page information added, but that is so inefficient I have to believe there is a better way.

Here's an example. What would be a good way to represent this text? Say it spans pages 1-2, divided at ///.

A sentence that spans two /// pages and has some mixed formatting.


The following code almost works, but the way it behaves when I try to select text with my mouse is off.

    StyleBox["A sentence", "Text"],
    StyleBox["that spans two", "Text", FontWeight -> "Bold"]
    }], "Page1"],
    StyleBox["pages", "Text", FontWeight -> "Bold", 
     FontSlant -> "Italic"],
    StyleBox["and has some mixed formatting.", "Text"]
    }], "Page2"]
 }]] // CellPrint

If I place the cursor on the left side of this sentence and drag toward the right, once I pass "two" the entire right-side box becomes selected. So, for example, I cannot select "two pages" using my cursor. I understand this is related to the hierarchical nature of the underlying expressions; is there something that can be done about it? Or is there an alternative representation that will avoid this problem?

  • 1
    $\begingroup$ Where is the hidden information in your example? Sorry but am quite confused about what you are wanting to achieve. $\endgroup$ Dec 13, 2013 at 2:17
  • $\begingroup$ @MikeHoneychurch The tags representing the page numbers: "Page1", "Page2". $\endgroup$
    – mfvonh
    Dec 13, 2013 at 2:19
  • $\begingroup$ Essentially my goal is to present the document as a stream of text without showing page divisions, but if I highlight any text I can see in a docked cell what page in the original (XML) it was taken from. And what I can't figure out is how to represent the data while maintaining "normal" mouse selection behavior. $\endgroup$
    – mfvonh
    Dec 13, 2013 at 2:21
  • $\begingroup$ You could probably do this with cell tags. $\endgroup$ Dec 13, 2013 at 21:40

1 Answer 1


Dummy styles appear to be an excellent technique for storing such information without affecting front-end selection behavior.

Cell[BoxData@StyleBox[#, "Text"] &@RowBox@{
    "A sentence",
    StyleBox["that spans two", FontWeight -> "Bold"]
    }, "Page1"],
    StyleBox["pages", FontWeight -> "Bold", FontSlant -> "Italic"],
    "and has some mixed formatting."
    }, "Page2"]
 }] // CellPrint

More idiomatically,

Notation[page[box_,n_] \[DoubleLongLeftRightArrow] StyleBox[box_,"Page"~~ToString[n_]]]

Cell[BoxData @ StyleBox[#,"Text"]& @ RowBox @ {
 page[#,"1"]& @ RowBox @ {
   "A sentence",
   StyleBox["that spans two",FontWeight->"Bold"]
 page[#,"2"]& @ RowBox @ {
   "and has some mixed formatting."
  • $\begingroup$ I would get rid of the Notation (which requires a Needs["Notation`"] and using the palette) and just define page[box_,n_]:=StyleBox[box,"Page"~~ToString[n]] $\endgroup$ May 1, 2018 at 17:36

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.