# Embedding hidden information in styled text

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.

EDIT:

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

Cell[BoxData@RowBox[{
TagBox[RowBox[{
StyleBox["A sentence", "Text"],
StyleBox["that spans two", "Text", FontWeight -> "Bold"]
}], "Page1"],
TagBox[RowBox[{
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?

• 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@{
StyleBox[RowBox@{
"A sentence",
StyleBox["that spans two", FontWeight -> "Bold"]
}, "Page1"],
StyleBox[RowBox@{
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 @ {
StyleBox["pages",FontWeight->"Bold",FontSlant->"Italic"],
"and has some mixed formatting."
}
}]//CellPrint

• 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]]` – Gustavo Delfino May 1 '18 at 17:36