3
$\begingroup$

In my stylesheet I have a particular style of cell, Exercise, that has a corresponding counter object. In a notebook with that stylesheet, there are several Exercise cells in each of which I've inserted the number for that counter:

Exericse 1....

Exercise 2....

Now I have a different cell, that begins:

In Exercise we saw that ...

In that different cell, after "Exercise ", I want to insert, as a hyperlink, the number of a particular one of those cells — in such a way that the inserted number will change automatically in case the number of the target Exercise style cell should change due to creation or deletion of other Exercise cells.

How to do it?

Having that kind of cross-referencing is such a basic property for documents of any kind, surely there's some really simple way to do it?

(I'm sure I've done it before, but it's one of those things that's just obscure enough or unobvious enough to forget easily!)

$\endgroup$
  • $\begingroup$ I've done it with Cell > CellTags > Add/Remove Cell Tag and Insert > Hyperlink. Is that what you're looking for? $\endgroup$ – Michael E2 Sep 7 '15 at 0:06
  • $\begingroup$ Related: (6073). $\endgroup$ – Silvia Sep 7 '15 at 2:09
  • $\begingroup$ @Silvia: The related 6073 does not do what I'm asking about. I want to insert in some other cell, as a hyperlink, the number of a particular numbered cell. $\endgroup$ – murray Sep 7 '15 at 2:36
  • $\begingroup$ @Michael E2: I know about using cell tags and referencing them by hyperlink. But even if I add some tag to the auto-numbered cell in question, using Insert > Hyperlink to that tag will not produce the number of that auto-numbered cell. $\endgroup$ – murray Sep 7 '15 at 2:38
  • 1
    $\begingroup$ @murray Oops, I should have said Insert > Automatic Numbering. Then select the style/counter name and tag of the numbered cell. $\endgroup$ – Michael E2 Sep 7 '15 at 10:01
4
$\begingroup$

To insert a link, I usually do as Michael's comment said. To make it dynamically reflecting the current status of the referred counter object, you need to use the second argument of CounterBox (let's call it "referring-mode" hereafter). An example should explain it more clearly than words:

    {
        (* Initialize myCounter:                                   *)

        Cell["", "Text",
            CounterAssignments -> {{"myCounter", 0}}
            ],

        (* A group of Cells with instances of myCounter:           *)

        Cell[BoxData[
                        CounterBox["myCounter"]
                        ], "Text",
                    CounterIncrements -> {"myCounter"}
                    ] //
                ConstantArray[#, 5] & //
            MapAt[Append[#, CellTags -> "tag1"] &, #, 3] &,

        (* A Cell with an instance of myCounter in referring-mode: *)

        Cell[BoxData[
                      RowBox[{"(",
                              CounterBox["myCounter", "tag1"],
                              ")"}]
                    ], "Text"]
    } //
Flatten // CellPrint

counter reference example

$\endgroup$
  • $\begingroup$ That almost does what I want: it requires creating a CellPrint[Cell...]] expression each time; I thought there's some more direct way. It also inserts unwanted parentheses around the number, as you show, and it inserts the number in a font other than the current cell's style. $\endgroup$ – murray Sep 7 '15 at 2:48
  • $\begingroup$ You've also shown more than I need: Since I already have a cell style in the stylesheet that has the auto-numbering property, I don't need to use a programmatic way to create the auto-numbered cells; in each one I can just use Insert > Automatic Numbering. $\endgroup$ – murray Sep 7 '15 at 2:51
  • $\begingroup$ @murray I used CellPrint just for convenience. I believe a palette with a button can be created to "parse" a hand-written cell. Say, we write this is a reference: <*myCounter,tag1*> . in a Text cell. It should not be hard to read the cell-expression, catch the special <*...*> part, and translate it to a CounterBox["myCounter", "tag1"]. As for the example, I intend to make it complete and self-explainable. :) $\endgroup$ – Silvia Sep 7 '15 at 2:53
  • $\begingroup$ @murray If you look at the last Cell[...] in my code, you'll see the "unwanted parentheses around the number" is added by me intentionally. I add it to demonstrate (to those readers less familiar with Boxes) how to integrate the CounterBox with other format. You can remove it safely. As for the style, it's my own stylesheet, sorry for the confusing. $\endgroup$ – Silvia Sep 7 '15 at 3:07
  • $\begingroup$ Oops--missed the deliberate parens there. $\endgroup$ – murray Sep 7 '15 at 3:44
1
$\begingroup$

I realize that you are asking another thing, but I would like to offer a look from a different direction. Why not to bring the text to the reader, instead of sending the reader to the text. It will be easier to read and understand. This requires a bit more work, however, so that one may be reluctant doing this. Anyway here is an example of a code making a button to open a separate notebook with the exercise text:

    Button["Show Exercise 1",
 CreateDocument[{
   (* This makes a Section title *)
   TextCell["Exercise 1", "Section", FontSize -> 40],

   (* This makes a text *)
   TextCell["Some text", "Text", FontSize -> 20],

   (* This makes a numbered formula *)
   TextCell[y == Sin[x^2] // TraditionalForm, 
    "DisplayFormulaNumbered", FontSize -> 20, TextAlignment -> Center],

   (* This makes a plot *)
   ExpressionCell[Plot[Sin[x], {x, 0, 2 \[Pi]}], "Figure"],

   (* This makes a caption for illustration *)
   TextCell["Some caption", "FigureCaption", FontSize -> 14],

   (* This makes another numbered formula *)
   TextCell[z == Exp[-x^2] // TraditionalForm, 
    "DisplayFormulaNumbered", FontSize -> 20, TextAlignment -> Center]
   },
  StyleDefinitions -> 
   FrontEnd`FileName[{"Article"}, "JournalArticle.nb", 
    CharacterEncoding -> "UTF-8"], WindowTitle -> "Exercise 1", 
  WindowSize -> {750, 500}, Background -> LightYellow], 
 ImageSize -> {200, 18}, BaseStyle -> {"GenericButton", Bold}]

Pressing the button will return this notebook:

enter image description here

Hope this might help.

$\endgroup$
  • $\begingroup$ On the one hand, that kind of presentation is effective. On the other hand, it means burying in the source notebook code that is much, much more advanced than the stage at which its users may be working. $\endgroup$ – murray Sep 7 '15 at 14:29
  • $\begingroup$ If one is going to do that sort of thing, with pop-ups, then an especially nice way to do it is with use of the Derivations and other "writing" tools in David Park's Presentations application (home.comcast.net/~djmpark/DrawGraphicsPage.html). $\endgroup$ – murray Sep 7 '15 at 14:33
  • 1
    $\begingroup$ @murray Yes, you are right, I developed this sort of things being strongly influenced by ideas of David Park and constantly discussing them with him. It is his main idea to bring information to the reader, not vice versa. Concerning burying the advanced code problem, the users normally do not see the background code. One can keep it in a separate notebook, only copy-pasting the button to the notebook for the readers. The button may have a form of the reference number, thus, looking exactly like the reference you intend to give. $\endgroup$ – Alexei Boulbitch Sep 7 '15 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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