MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Notebook contents

  • To: mathgroup at smc.vnet.net
  • Subject: [mg30583] Re: Notebook contents
  • From: Tom Burton <tburton at cts.com>
  • Date: Thu, 30 Aug 2001 03:51:29 -0400 (EDT)
  • References: <9mhv7u$4oc$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Wed, 29 Aug 2001 05:40:14 +0000 (UTC), in comp.soft-sys.math.mathematica you wrote:

>Now I want to insert a page of contents (an index) at the
>beginning of my document.

You may be able to adapt the following code. It loops through a list of filenames "files" (these should be a fully qualified names), then constructs a TOC.

source cell style:           generated TOC cell style:
Title                        TSection
Section                      TSubsection
Subsection                   TSubsubsection

with hyperlinks. This code is specific and fragile. It assumes that sections and subsections are auto-numbered in Arabic numerals. It assumes that the text of the Title cell has the form

"Chapter nn\n
text"

References are treated separately at the end, etc.

So you would have to fiddle with this. Let me know if you want to bother and need more help.


link[text_String, opts___?OptionQ] := 
  Module[{tags = CellTags /. Flatten[{opts}]},
    If[tags === CellTags,
      text,
      ButtonBox[RowBox[text], 
        ButtonData :> Evaluate[{data1a <> ".nb", First[Flatten[{tags}]]}], 
        ButtonStyle -> "Hyperlink"]
      ]
    ]

data2 = {};
Map[Function[fName,
      nb = NotebookOpen[fName, Visible -> False];
      contents = NotebookGet[nb]; NotebookClose[nb];
      data1 = 
        Cases[First[contents], 
          Cell[__, "Title", ___] | Cell[__, "Section", ___] | 
            Cell[__, "Subsection", ___], -1];
      chapterNumber = 
        ReadList[
            StringToStream[
              data1\[LeftDoubleBracket]1, 1\[RightDoubleBracket]], 
            Word]\[LeftDoubleBracket]2\[RightDoubleBracket];
      data2 = Join[data2, data1 /. {
              
              Cell[t_, 
                  "Title", ___] :> (j = 
                    StringPosition[t, "\n"]\[LeftDoubleBracket]1, 
                      1\[RightDoubleBracket]; data1a = StringTake[t, j - 1]; 
                  data1b = StringDrop[t, j]; 
                  Cell[TextData[{ButtonBox[data1a, 
                          ButtonData :> Evaluate[{data1a <> ".nb", None}], 
                          ButtonStyle -> "Hyperlink"], " ", data1b}], 
                    "TSection"]),
              
              Cell[TextData[t__], "Section", opts___] :> 
                Cell[TextData[{link[chapterNumber, opts], ".", 
                      CounterBox["Section"], " ", t}], "TSubsection"],
              
              Cell[t_, "Section", opts___] :> 
                Cell[TextData[{link[chapterNumber, opts], ".", 
                      CounterBox["Section"], " ", t}], "TSubsection"],
              
              Cell[TextData[t__], "Subsection", opts___] :> 
                Cell[TextData[{link[chapterNumber, opts], ".", 
                      CounterBox["Section"], ".", CounterBox["Subsection"], 
                      " ", t}], "TSubsubsection"], 
              Cell[t_, "Subsection", opts___] :> 
                Cell[TextData[{link[chapterNumber, opts], ".", 
                      CounterBox["Section"], ".", CounterBox["Subsection"], 
                      " ", t}], "TSubsubsection"]}];
      ], files];
AppendTo[data2, 
    Cell[TextData[{ButtonBox["References", 
            ButtonData :> {"References.nb", None}, 
            ButtonStyle -> "Hyperlink"], " ", 
          "Bibliography with hyperlinks"}], "TSection", GeneratedCell -> True,
       CellAutoOverwrite -> True]];
Clear[contents];
CellPrint[data2]
Tom Burton


  • Prev by Date: Re: Eulerian numbers
  • Next by Date: Re: How do I script the evaluation of a Notebook
  • Previous by thread: Notebook contents
  • Next by thread: Geometry Utility, drawing technique