Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Where is the hidden information in your example? Sorry but am quite confused about what you are wanting to achieve. – Mike Honeychurch Dec 13 '13 at 2:17
@MikeHoneychurch The tags representing the page numbers: "Page1", "Page2". – mfvonh Dec 13 '13 at 2:19
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. – mfvonh Dec 13 '13 at 2:21
You could probably do this with cell tags. – Mike Honeychurch Dec 13 '13 at 21:40

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."
share|improve this answer

Your Answer


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.