automatic referencing (almost working)
- To: mathgroup at smc.vnet.net
- Subject: [mg13056] automatic referencing (almost working)
- From: "-don." <dexter at csd.uwm.edu>
- Date: Sat, 4 Jul 1998 16:44:59 -0400
- Organization: University of Wisconsin - Milwaukee
- Sender: owner-wri-mathgroup at wolfram.com
hello all,
i've posted a few times before trying to come up with a method of easily
generating
references to sections, equations, etc. within notebooks. P.J. Hinton
has a web
site (referenced in my earlier posts and in his responses) which details
how to
perform referencing manually. this is tedious if there are *many*
equations to
be referenced since it requires an equation (or section) name to be
entered twice.
in my last post, i set the goal of having referencing work as follows:
you're typing away and realize you need a reference, leaving the
courser in the
spot where the reference is to appear, you hit a button on a palette
(called
"CreateReference" or some such), you then select the to-be-referenced
cell and the
reference appears where you wish it.
i've managed to get kinda close to this. at present, the referencing
works as:
you're typing away and realize you need a reference, you select the
to-be-referenced
cell and click the "SetReference" button. this creates a random CellTag
if one does
not already exist and writes the pertinent data to a temp file (for
Unix-like sytems...
placed in /tmp). you then position the courser where the reference is
to be placed
and click the "ReferenceIt" button. a reference in the form of "Section
X" or "(X)"
appears for section-like cells or NumberedEquations.
this generally works if the buttons are in the working notebook, but not
if they're placed in their own palette. i've also found specific
equations for which
it generates errors (one that began with a \[DoubleRightArrow]).
i'd like to invite those out there more skilled than i to take what i've
done and
1) make it function as a palette, 2) make it robust, 3) make it slick.
notebook follows (Mathematica 3.0, Linux). give it a try, see what you
think.
responses here or to dexter at csd.uwm.edu appreciated.
-don.
--------------------------------------------------------------------------
Notebook[{
Cell[BoxData[
RowBox[{"\n",
RowBox[{
RowBox[{"CellPrint", "[",
RowBox[{"Cell", "[",
RowBox[{
RowBox[{"BoxData", "[",
RowBox[{"ButtonBox", "[",
RowBox[{"\"\<SetReference\>\"", ",",
RowBox[{"Active", "->", "True"}], ",",
RowBox[{"ButtonEvaluator", "->", "Automatic"}], ",",
"\n", "\t\t\t\t",
RowBox[{"ButtonFunction", ":>",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"markervalue", ",", "markertype",
",",
RowBox[{"nb", "=",
RowBox[{"SelectedNotebook", "[",
"]"}]}],
",", " ",
RowBox[{"ref", "=", " ",
RowBox[{"NotebookCreate", "[",
RowBox[{"Visible", "->", "False"}],
"]"}]}]}], "}"}], ",", "\n",
"\t\t\t\t\t\t",
RowBox[{
RowBox[{"result", "=",
RowBox[{"Options", "[",
RowBox[{
RowBox[{
"NotebookSelection", "[", "nb", "]"}],
",",
"CellTags"}], "]"}]}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{
RowBox[{
"Dimensions", "[", "result", "]"}],
"[",
RowBox[{"[", "1", "]"}], "]"}], ">",
"0"}], ",", "\n", "\t\t\t\t\t\t\t\t",
RowBox[{"markervalue", "=",
RowBox[{"ToExpression", "[",
RowBox[{
RowBox[{
RowBox[{
RowBox[{
RowBox[{
"ToBoxes", "[", "result", "]"}],
"[",
RowBox[{"[", "1", "]"}], "]"}],
"[",
RowBox[{"[", "2", "]"}], "]"}],
"[",
RowBox[{"[", "1", "]"}], "]"}],
"[",
RowBox[{"[", "3", "]"}], "]"}],
"]"}]}], ",", "\n",
"\t\t\t\t\t\t\t\t",
RowBox[{
RowBox[{"markervalue", "=",
RowBox[{"Random", "[",
RowBox[{"Real", ",",
RowBox[{"{",
RowBox[{"0", ",", "1"}], "}"}],
",",
"20"}], "]"}]}], ";", "\n",
"\t\t\t\t\t\t\t\t",
RowBox[{"SetOptions", "[",
RowBox[{
RowBox[{
"NotebookSelection", "[", "nb",
"]"}],
",",
RowBox[{"CellTags", "->",
RowBox[{
"{", "markervalue", "}"}]}]}],
"]"}]}]}], "]"}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"markertype", "=",
RowBox[{"Part", "[",
RowBox[{
RowBox[{"NotebookRead", "[", "nb",
"]"}],
",", "2"}], "]"}]}], ";",
RowBox[{"NotebookWrite", "[",
RowBox[{"ref", ",",
RowBox[{"Cell", "[",
RowBox[{
"markertype", " ", ",",
"\"\<Output\>\"",
",",
RowBox[{"CellTags", "->",
RowBox[{
"{", "\"\<markertype\>\"",
"}"}]}]}],
"]"}]}], "]"}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"NotebookWrite", "[",
RowBox[{"ref", ",",
RowBox[{"Cell", "[",
RowBox[{
"markervalue", " ", ",",
"\"\<Output\>\"",
",",
RowBox[{"CellTags", "->",
RowBox[{
"{", "\"\<markervalue\>\"",
"}"}]}]}],
"]"}]}], "]"}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"NotebookSave", "[",
RowBox[{
"ref", ",",
"\"\</tmp/ActiveReference.nb\>\""}],
"]"}],
";", "\n", "\t\t\t\t\t\t",
RowBox[{"NotebookClose", "[", "ref", "]"}],
";"}]}], "\n", "\t\t\t", "]"}]}]}], "]"}],
"]"}],
",", "\n", "\"\<Text\>\""}], "]"}], "]"}], ";"}]}]],
"Input"],
Cell[BoxData[
RowBox[{"\n",
RowBox[{
RowBox[{"CellPrint", "[",
RowBox[{"Cell", "[",
RowBox[{
RowBox[{"BoxData", "[",
RowBox[{"ButtonBox", "[",
RowBox[{"\"\<ReferenceIt\>\"", ",",
RowBox[{"Active", "->", "True"}], ",",
RowBox[{"ButtonEvaluator", "->", "Automatic"}], ",",
"\n", "\t\t\t\t",
RowBox[{"ButtonFunction", ":>",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{
"pretext", ",", "posttext", ",", " ",
"markertype", ",", " ", "markervalue",
",",
" ", "mycmd", ",", " ", "mystring", ",",
RowBox[{"nb", "=",
RowBox[{"SelectedNotebook", "[",
"]"}]}],
",", " ",
RowBox[{"ref", "=",
RowBox[{"NotebookOpen", "[",
RowBox[{
"\"\</tmp/ActiveReference.nb\>\"",
",",
RowBox[{"Visible", "->",
"False"}]}],
"]"}]}]}], "}"}], ",", "\n",
"\t\t\t\t\t\t",
RowBox[{
RowBox[{
"FrontEndTokenExecute", "[",
"\"\<CellSplit\>\"",
"]"}], ";", "\n", "\t\t\t\t\t\t",
RowBox[{"SelectionMove", "[",
RowBox[{
"nb", ",", "Previous", ",",
"CellContents"}],
"]"}], ";", "\n", "\t\t\t\t\t\t",
RowBox[{"pretext", " ", "=", " ",
RowBox[{"NotebookRead", "[", "nb",
"]"}]}],
";", "\n", "\t\t\t\t\t\t",
RowBox[{"SelectionMove", "[",
RowBox[{"nb", ",", "All", ",", "Cell"}],
"]"}],
";", "\n", "\t\t\t\t\t\t",
RowBox[{"SetOptions", "[",
RowBox[{
RowBox[{
"NotebookSelection", "[", "nb", "]"}],
",",
RowBox[{"CellTags", "->",
RowBox[{
"{", "\"\<deletetext\>\"", "}"}]}]}],
"]"}], ";", "\n", "\t\t\t\t\t\t",
RowBox[{"SelectionMove", "[",
RowBox[{
"nb", ",", "Next", ",", "CellContents"}],
"]"}], ";", "\n", "\t\t\t\t\t\t",
RowBox[{"posttext", " ", "=", " ",
RowBox[{"NotebookRead", "[", "nb",
"]"}]}],
";", "\n", "\t\t\t\t\t\t",
RowBox[{"SelectionMove", "[",
RowBox[{"nb", ",", "All", ",", "Cell"}],
"]"}],
";", "\n", "\t\t\t\t\t\t",
RowBox[{"SetOptions", "[",
RowBox[{
RowBox[{
"NotebookSelection", "[", "nb", "]"}],
",",
RowBox[{"CellTags", "->",
RowBox[{
"{", "\"\<deletetext\>\"", "}"}]}]}],
"]"}], ";", "\n", "\t\t\t\t\t\t",
RowBox[{"NotebookFind", "[",
RowBox[{
"ref", ",", "\"\<markertype\>\"", ",",
"All",
",", "CellTags"}], "]"}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"markertype", "=",
RowBox[{"First", "[",
RowBox[{"First", "[",
RowBox[{"NotebookRead", "[", "ref",
"]"}],
"]"}], "]"}]}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"NotebookFind", "[",
RowBox[{
"ref", ",", "\"\<markervalue\>\"", ",",
"All",
",", "CellTags"}], "]"}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"markervalue", "=",
RowBox[{"ToString", "[",
RowBox[{"First", "[",
RowBox[{"First", "[",
RowBox[{
"NotebookRead", "[", "ref", "]"}],
"]"}],
"]"}], "]"}]}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"mycmd", "=",
RowBox[{"StringJoin", "[",
RowBox[{
"\"\<CounterBox[\>\"", ",",
"markertype",
",", "\"\<,\\\"\>\"", ",",
"markervalue",
",", "\"\<\\\"]\>\""}], "]"}]}], ";",
"\n",
"\t\t\t\t\t\t",
RowBox[{"Which", "[",
RowBox[{
RowBox[{
"markertype", "==",
"\"\<NumberedEquation\>\""}], ",",
"\n",
"\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mystring", "=",
RowBox[{"TextData", "[",
RowBox[{
"pretext", ",", "\"\<(\>\"", ",",
RowBox[{
"ToExpression", "[", "mycmd",
"]"}],
",", "\"\<) \>\"", ",",
"posttext"}],
"]"}]}], ",", "\n",
"\t\t\t\t\t\t\t\t",
RowBox[{
"markertype", "==", "\"\<Section\>\""}],
",",
"\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mystring", "=",
RowBox[{"TextData", "[",
RowBox[{
"pretext", ",", "\"\<Section \>\"",
",",
RowBox[{
"ToExpression", "[", "mycmd",
"]"}],
",", "\"\< \>\"", ",",
"posttext"}],
"]"}]}], ",", "\n",
"\t\t\t\t\t\t\t\t",
RowBox[{
"markertype", "==",
"\"\<Subsection\>\""}],
",", "\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{
RowBox[{"mycmd1", "=",
RowBox[{"StringJoin", "[",
RowBox[{
"\"\<CounterBox[\>\"", ",",
"\"\<Section\>\"", ",",
"\"\<,\\\"\>\"", ",",
"markervalue",
",", "\"\<\\\"]\>\""}], "]"}]}],
";",
"\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mycmd2", "=",
RowBox[{"StringJoin", "[",
RowBox[{
"\"\<CounterBox[\>\"", ",",
"\"\<Subsection\>\"", ",",
"\"\<,\\\"\>\"", ",",
"markervalue",
",", "\"\<\\\"]\>\""}], "]"}]}],
";",
"\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mystring", "=",
RowBox[{"TextData", "[",
RowBox[{
"pretext", ",", "\"\<Section
\>\"",
",",
RowBox[{
"ToExpression", "[", "mycmd1",
"]"}],
",", "\"\<.\>\"", ",",
RowBox[{
"ToExpression", "[", "mycmd2",
"]"}],
",", "\"\< \>\"", ",",
"posttext"}],
"]"}]}]}], ",", "\n",
"\t\t\t\t\t\t\t\t",
RowBox[{
"markertype", "==",
"\"\<Subsubsection\>\""}], ",", "\n",
"\t\t\t\t\t\t\t\t\t\t",
RowBox[{
RowBox[{"mycmd1", "=",
RowBox[{"StringJoin", "[",
RowBox[{
"\"\<CounterBox[\>\"", ",",
"\"\<Section\>\"", ",",
"\"\<,\\\"\>\"", ",",
"markervalue",
",", "\"\<\\\"]\>\""}], "]"}]}],
";",
"\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mycmd2", "=",
RowBox[{"StringJoin", "[",
RowBox[{
"\"\<CounterBox[\>\"", ",",
"\"\<Subsection\>\"", ",",
"\"\<,\\\"\>\"", ",",
"markervalue",
",", "\"\<\\\"]\>\""}], "]"}]}],
";",
"\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mycmd3", "=",
RowBox[{"StringJoin", "[",
RowBox[{
"\"\<CounterBox[\>\"", ",",
"\"\<Subsubsection\>\"", ",",
"\"\<,\\\"\>\"", ",",
"markervalue",
",", "\"\<\\\"]\>\""}], "]"}]}],
";",
"\n", "\t\t\t\t\t\t\t\t\t\t",
RowBox[{"mystring", "=",
RowBox[{"TextData", "[",
RowBox[{
"pretext", ",", "\"\<Section
\>\"",
",",
RowBox[{
"ToExpression", "[", "mycmd1",
"]"}],
",", "\"\<.\>\"", ",",
RowBox[{
"ToExpression", "[", "mycmd2",
"]"}],
",", "\"\<.\>\"", ",",
RowBox[{
"ToExpression", "[", "mycmd3",
"]"}],
",", "\"\< \>\"", ",",
"posttext"}],
"]"}]}]}]}], "]"}], ";", "\t",
"\n",
"\t\t\t\t\t\t",
RowBox[{"CellPrint", "[",
RowBox[{"Cell", "[",
RowBox[{"mystring", ",",
"\"\<Text\>\""}],
"]"}], "]"}], ";", "\n", "\t\t\t\t\t\t",
RowBox[{"NotebookFind", "[",
RowBox[{
"nb", ",", "\"\<deletetext\>\"", ",",
"All",
",", "CellTags"}], "]"}], ";", "\n",
"\t\t\t\t\t\t",
RowBox[{"NotebookDelete", "[", "nb", "]"}],
";",
"\n", "\t\t\t\t\t\t",
RowBox[{"NotebookClose", "[", "ref", "]"}],
";"}]}], "\n", "\t\t\t", "]"}]}]}], "]"}],
"]"}],
",", "\n", "\"\<Text\>\""}], "]"}], "]"}], ";"}]}]],
"Input"],
Cell[BoxData[GridBox[{
{
ButtonBox["SetReference",
ButtonFunction:>Module[ {markervalue, markertype,
Set[ nb,
SelectedNotebook[ ]],
Set[ ref,
NotebookCreate[ Visible -> False]]},
CompoundExpression[
Set[ result,
Options[
NotebookSelection[ nb], CellTags]],
If[
Greater[
Part[
Dimensions[ result], 1], 0],
Set[ markervalue,
ToExpression[
Part[
Part[
Part[
Part[
ToBoxes[ result], 1], 2], 1], 3]]],
CompoundExpression[
Set[ markervalue,
Random[ Real, {0, 1}, 20]],
SetOptions[
NotebookSelection[ nb], CellTags -> {
markervalue}]]],
Set[ markertype,
Part[
NotebookRead[ nb], 2]],
NotebookWrite[ ref,
Cell[
markertype, "Output", CellTags -> {"markertype"}]],
NotebookWrite[ ref,
Cell[
markervalue, "Output", CellTags ->
{"markervalue"}]],
NotebookSave[ ref, "/tmp/ActiveReference.nb"],
NotebookClose[ ref], Null]],
ButtonEvaluator->Automatic,
Active->True],
ButtonBox["ReferenceIt",
ButtonFunction:>
Module[ {pretext, posttext, markertype, markervalue,
mycmd,
mystring,
Set[ nb,
SelectedNotebook[ ]],
Set[ ref,
NotebookOpen[
"/tmp/ActiveReference.nb", Visible -> False]]},
CompoundExpression[
FrontEndTokenExecute[ "CellSplit"],
SelectionMove[ nb, Previous, CellContents],
Set[ pretext,
NotebookRead[ nb]],
SelectionMove[ nb, All, Cell],
SetOptions[
NotebookSelection[ nb], CellTags -> {"deletetext"}],
SelectionMove[ nb, Next, CellContents],
Set[ posttext,
NotebookRead[ nb]],
SelectionMove[ nb, All, Cell],
SetOptions[
NotebookSelection[ nb], CellTags -> {"deletetext"}],
NotebookFind[ ref, "markertype", All, CellTags],
Set[ markertype,
First[
First[
NotebookRead[ ref]]]],
NotebookFind[ ref, "markervalue", All, CellTags],
Set[ markervalue,
ToString[
First[
First[
NotebookRead[ ref]]]]],
Set[ mycmd,
StringJoin[
"CounterBox[", markertype, ",\"", markervalue,
"\"]"]],
Which[
Equal[ markertype, "NumberedEquation"],
Set[ mystring,
TextData[ pretext, "(",
ToExpression[ mycmd], ") ", posttext]],
Equal[ markertype, "Section"],
Set[ mystring,
TextData[ pretext, "Section ",
ToExpression[ mycmd], " ", posttext]],
Equal[ markertype, "Subsection"],
CompoundExpression[
Set[ mycmd1,
StringJoin[
"CounterBox[", "Section", ",\"", markervalue,
"\"]"]],
Set[ mycmd2,
StringJoin[
"CounterBox[", "Subsection", ",\"", markervalue,
"\"]"]],
Set[ mystring,
TextData[ pretext, "Section ",
ToExpression[ mycmd1], ".",
ToExpression[ mycmd2], " ", posttext]]],
Equal[ markertype, "Subsubsection"],
CompoundExpression[
Set[ mycmd1,
StringJoin[
"CounterBox[", "Section", ",\"", markervalue,
"\"]"]],
Set[ mycmd2,
StringJoin[
"CounterBox[", "Subsection", ",\"", markervalue,
"\"]"]],
Set[ mycmd3,
StringJoin[
"CounterBox[", "Subsubsection", ",\"",
markervalue,
"\"]"]],
Set[ mystring,
TextData[ pretext, "Section ",
ToExpression[ mycmd1], ".",
ToExpression[ mycmd2], ".",
ToExpression[ mycmd3], " ", posttext]]]],
CellPrint[
Cell[ mystring, "Text"]],
NotebookFind[ nb, "deletetext", All, CellTags],
NotebookDelete[ nb],
NotebookClose[ ref], Null]],
ButtonEvaluator->Automatic,
Active->True]}
},
RowSpacings->0,
ColumnSpacings->0,
GridFrame->True,
GridDefaultElement:>ButtonBox[ "\\[Placeholder]"]]], "Input"] },
FrontEndVersion->"X 3.0",
ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{731, 834},
WindowMargins->{{39, Automatic}, {5, Automatic}}, StyleDefinitions ->
"/home/don/thesis/mathematica/StyleSheets/thesis.nb" ]