Mathematica versions 6 and 7

Introduction

Mathematica 6.0 is so large and complicated that it will take a long time for it to be exploited to the full – and version 7.0 is already upon is! Fortunately, this is more of an evolutionary step in the devolpment of the product. The following setions merely highlight a few prettier features, whilst the great ocean of truth lays undiscovered before us.

Manipulate

If you learn nothing else about the new interactive features of Mathematica, learn to use Manipulate.

Remember that this page is not itself dynamic, so what you see here are merely snapshots. Paste the corresponding Manipulate command into Mathematica and execute it to see these in operation.

Graphical manipulation

Manipulate[Plot[BesselJ[a,x],{x,0,20}],{a,{0,1,2,3,4,5,6}}]

In effect Manipulate creates a lower level interface automatically that lets you explore the properties of your plot. This lower level is itself documented, but is somewhat less easy to exploit successfully. Here are a few more examples of such constructions (which are not limited to graphics):

Algebraic manipulation

Manipulate[Expand[(a+b)^k],{k,1,10,1}]

Mixed algebraic and graphical manipulate

Manipulate[
Column[{
Plot[1/(x^2+1)^k,{x,-5,5},PlotRange->Full,ImageSize->300],
Expand[(x^2+1)^k]
   }],{k,1,5,1}]

Creating the Virtual Mathematica Book

The following code was supplied by Thomas Muench, and was first published by him in comp.soft-sys.math.mathematica .  If you execute the following code, using Mathematica version 6.0, a new notebook will be created, containing a copy of the Mathematica book for 6.0! When it is finally done, click on the new notebook and save it.

Click the link in the top right hand corner to download the notebook corresponding to this page to try this code.

It may seem a little strange to run a notebook in order to create another notebook, but this program assembles the "Virtual Mathematica Book" using information that is already part of the help system of your Mathematica installation. The resultant notebook should be considered to be copyright WRI, and be used on the machine that contains Mathematica.

The corresponding physical book for the previous version of Mathematica – version 5.2 – was already very thick, so I would you to only print the notebook in sections.

fn=FileNames["*Overview.nb",ToFileName[{$InstallationDirectory,"Documentation","English","System","Tutorials"}]];
(*Find "Overview.nb" files in the tutorials-directory*)

nameList={StringTake[#,{StringPosition[#,$PathnameSeparator][[-1,1]]+1,-12}],"paclet:tutorial/"<>StringTake[#,{StringPosition[#,$PathnameSeparator][[-1,1]]+1,-4}],#}&/@fn;
(*create a list of strings needed for future code*)

Do[nameList[[i,1]]="New: "<>nameList[[i,1]],{i,{3,6,7,8,18,28,29,40,41,46,48}}];
(*mark the chapter not present in v5.2 with "New: "*)

orderOfChapters={{36,3,8,31,4,47,1,39,32,14,20,16,18,17,11,43},{10,13,35,48,22,45,9,27,38,42,44,23,12,26,15,40,41},{30,25,2,21,5,7,37,19,33,34,28,29,6,46,24}};
(*The file list is alphabetical-this is the order in which the chapters should appear*)

structure=Table[nameList[[i]],{k,3},{i,orderOfChapters[[k]]}];
(*needed for the code further down-represents the structure of the Mathematica book*)

mainChapters={"Getting Started","A Practical Introduction to Mathematica","Principles of Mathematica","Advanced Mathematics in Mathematica","Reference Guide","Further Tutorials","Packages"};
(*The three main headings*)

beautify[ugly_]:=StringReplace[StringReplace[ugly,{l1_?LowerCaseQ~~l2_?UpperCaseQ:>l1<>" "<>l2}],{" And "->" and "," Of "->" of "," In "->" in "," The "->" the "," For "->" for "}];
(*based on code by Selwyn Hollis-Thanks!*)

d[x_,style_: "Section",margin_: {{36,0},{5,5}}]:=Module[{sectitle=beautify[x[[1]]]},Cell[BoxData[ButtonBox[PaneSelectorBox[{False->sectitle,True->StyleBox[sectitle,"HyperlinkActive"]},Dynamic[CurrentValue["MouseOver"]]],BaseStyle->{"Link",style},ButtonData->x[[2]],ButtonNote->x[[2]]]],style,CellDingbat->None,ShowGroupOpener->True,CellMargins->margin]]
(*Cell representing a hyperlink to a file*)

nb=NotebookPut[];SetOptions[nb,StyleDefinitions->ToFileName["Wolfram","Reference.nb"]];
(*Open new Notebook*)
NotebookWrite[nb,{Cell[Row[{ToBoxes[Import[ToFileName["ExampleData","spikey.tiff"],"Graphics",ImageSize->70]],Cell["The Virtual Mathematica Book (v6)","Title",TextAlignment->Center]}],"Title",TextAlignment->Center],Cell["A collection of links to the tutorials present in the Mathematica 6.0 Documentation Center, arranged in the order of chapters in \"The Mathematica Book\".","Text",TextAlignment->Center]}];
(*Print the titel*)

Do[NotebookWrite[nb,Cell[StringJoin[Table["xxxxxxxxxx",{k}]],"Text"]];
If[2≤k≤4,Do[NotebookWrite[nb,d[i]];
With[{nb1=NotebookOpen[i[[3]],Visible->False]},SelectionMove[nb1,All,Notebook];
NotebookWrite[nb,NotebookRead[nb1]//.{"TOCChapter"->Sequence@@{"Subsection",CellDingbat->None,ShowGroupOpener->True,CellMargins->{{48,0},Inherited}},"TOCSection"->Sequence@@{"Subsubsection",CellDingbat->None,ShowGroupOpener->True,CellMargins->{{58,0},Inherited}},"TOCSubsection"->Sequence@@{"Subsubsubsection",CellDingbat->None,ShowGroupOpener->True,CellMargins->{{68,0},Inherited}},"TOCSubsubsection"->Sequence@@{"Text",CellDingbat->None,ShowGroupOpener->True,CellMargins->{{78,0},Inherited}},ShowCellBracket->True}];
NotebookClose[nb1]],{i,structure[[k-1]]}]],{k,Length[mainChapters]}];
(*write all the links to the main chapters,as well as to sub-chapters*)

Do[NotebookFind[nb,cellsToDelete,All,CellStyle];
NotebookDelete[nb],{cellsToDelete,{"SymbolColorBar","AnchorBarGrid","FooterCell","TOCDocumentTitle"}}];
(*Delete unnecessary styling cells that are part of the tutorial notebooks*)

SetOptions[nb,StyleDefinitions->ToFileName["Book","Textbook.nb"]];
Do[NotebookFind[nb,StringJoin[Table["xxxxxxxxxx",{k}]],All];
SelectionMove[nb,All,Cell];
NotebookWrite[nb,Cell[mainChapters[[k]],"Subtitle",ShowGroupOpener->True,CellMargins->{{10,0},{5,10}},CellTags->"Section"<>ToString[k],CellFrame->If[MemberQ[{1,6,7},k],{{0,0},{0,2}},False]]],{k,Length[mainChapters],1,-1}];
(*Finishing touches*)

(* -==-Add links to tutorials that are not linked yet-==-*)

SelectionMove[nb,All,Notebook];
links=StringCases[ToString[NotebookRead[nb],InputForm],Shortest[("\"paclet:tutorial/"~~y__~~"\"")]:>y];
(*Extract all links to tutorials present in "The Book"...*)

linkedFiles=Union[Flatten[If[StringFreeQ[#,"#"~~DigitCharacter...],#<>".nb",StringCases[#,(y__~~"#"~~DigitCharacter...):>y<>".nb"]]&/@links]];
(*... and their filenames*)

allTutorials=Flatten[StringTake[#,{StringPosition[#,$PathnameSeparator][[-1,1]]+1,-1}]&/@FileNames["*.nb",ToFileName[{$InstallationDirectory,"Documentation","English","System","Tutorials"}]]];
(*all tutorials in the tutorial directory*)

notUsed=Complement[allTutorials,linkedFiles];
(*all tutorials not linked to in "The Book" so far (in alphabetical order);these are 76 tutorial notebooks*)

nameList={StringTake[#,{1,-4}],"paclet:tutorial/"<>StringTake[#,{1,-4}],ToFileName[{$InstallationDirectory,"Documentation","English","System","Tutorials"}]<>#}&/@notUsed;
(*create a list of strings needed for future code*)

GettingStarted={21,18,75};GraphPlotting={27,28,36,71};MathematicaReferenceGuide={7,31,64,19,57,20,37,33,30};MatrixAndVectorOperations={10,26,6,3,73,16,42,45,62,52,65,69};Statistics={11,15,13,58};DynamicFeatures={35,2,34,1};SystemSpecificTopics={17,32,60,50,51,53,41,44,38,39,40};FurtherNDSolveTutorials={46,47,48,49};FrontendAndFormattingTopics={5,22,24,25,29,67,70,74};
topics={GettingStarted,MathematicaReferenceGuide,GraphPlotting,MatrixAndVectorOperations,Statistics,DynamicFeatures,SystemSpecificTopics,FurtherNDSolveTutorials,FrontendAndFormattingTopics};
FurtherTopics=Complement[Range[Length[notUsed]],Flatten[topics]];
topics=Join[topics,{FurtherTopics}];
(*trying to get some order into these tutorials*)

topicNames={"Getting Started","Mathematica Reference Guide","Graph Plotting","Matrix And Vector Operations","Statistics","Dynamic Features","System Specific Topics","Further NDSolve Tutorials","FrontEnd and Formatting Topics","Others"};

NotebookFind[nb,"Section1",After,CellTags];SelectionMove[nb,After,Cell];
Do[NotebookWrite[nb,d[nameList[[i]],"Subsection",{{48,0},Inherited}]],{i,GettingStarted}];
(*Add links to "getting Started" topics*)

NotebookFind[nb,"Section5",After,CellTags];SelectionMove[nb,After,Cell];
Do[NotebookWrite[nb,d[nameList[[i]],"Subsection",{{48,0},Inherited}]],{i,MathematicaReferenceGuide}];
(*Add links to "Refrence Guides" topics*)

NotebookFind[nb,"Section6",After,CellTags];SelectionMove[nb,After,Cell];
Do[NotebookWrite[nb,Cell[topicNames[[i]],"Section",CellDingbat->None,ShowGroupOpener->True,CellMargins->{{36,0},{5,5}}]];
Do[NotebookWrite[nb,d[nameList[[k]],"Subsection",{{48,0},Inherited}]],{k,topics[[i]]}],{i,3,Length[topics]}];
(*Add links to all other topics*)

(* -==-Add links to Packages-==-*)

fnpdir=FileNames["*",ToFileName[{$InstallationDirectory,"Documentation","English","Packages"}]];
(*Directory names of the Packages*)

fnptut=FileNames["*.nb",ToFileName[{#,"Documentation","English","Tutorials"}],Infinity]&/@fnpdir;
(*tutorial notebooks of the Packages*)

fnpguid=FileNames["*.nb",ToFileName[{#,"Documentation","English","Guides"}],Infinity]&/@fnpdir;
(*guide notebooks of the Packages*)

NotebookFind[nb,"Section7",After,CellTags];SelectionMove[nb,After,Cell];
Do[With[{packageName=StringTake[#,{StringPosition[#,$PathnameSeparator][[-1,1]]+1,-1}]&@fnpdir[[package]]},NotebookWrite[nb,Cell[beautify@packageName,"Section",CellDingbat->None,ShowGroupOpener->True,CellMargins->{{36,0},{5,5}}]];
If[Length[fnptut[[package]]]>0,level=If[StringTake[fnptut[[package]],{1,-1}+(Take[#[[1]],{-2,-1}]&@Transpose[StringPosition[#,$PathnameSeparator]]&/@fnptut[[package]])[[1]]][[1]]=="Tutorials",1,2];
nameList={"Tutorial: "<>StringTake[#,{StringPosition[#,$PathnameSeparator][[-level,1]]+1,-4}],"paclet:"<>packageName<>"/tutorial/"<>StringReplace[StringTake[#,{StringPosition[#,$PathnameSeparator][[-level,1]]+1,-4}],"\\"->"/"]}&/@fnptut[[package]];
Do[NotebookWrite[nb,d[nameList[[k]],"Subsection",{{48,0},Inherited}]],{k,Length[fnptut[[package]]]}]];
If[Length[fnpguid[[package]]]>0,level=If[StringTake[fnpguid[[package]],{1,-1}+(Take[#[[1]],{-2,-1}]&@Transpose[StringPosition[#,$PathnameSeparator]]&/@fnpguid[[package]])[[1]]][[1]]=="Guides",1,2];
nameList={"Guide: "<>StringTake[#,{StringPosition[#,$PathnameSeparator][[-1,1]]+1,-4}],"paclet:"<>packageName<>"/guide/"<>StringReplace[StringTake[#,{StringPosition[#,$PathnameSeparator][[-level,1]]+1,-4}],"\\"->"/"]}&/@fnpguid[[package]];
Do[NotebookWrite[nb,d[nameList[[k]],"Subsection",{{48,0},Inherited}]],{k,Length[fnpguid[[package]]]}]]],{package,Length[fnpdir]}];
(*Add links to package tutorials and guides*)

SelectionMove[nb,All,Notebook];FrontEndExecute[FrontEndToken["SelectionCloseAllGroups"]];
FrontEndExecute[FrontEndToken["OpenCloseGroup"]];

Adding points to a graph with the mouse

The following code creates a graph that can be populated by clicking where you need a point. There is a guide at the bottom of the display to indicate exactly where the mouse is positioned, and you can display a small menu by performing a Shift-click. This enables you to remove a point.

dataPoints={{1,1}};

deletePoint[]:=Module[{s},
s=Nearest[dataPoints,currentPoint];
If[s=!={},dataPoints=DeleteCases[dataPoints,s[[1]]]];
extras={}
];
processClick[]:=Module[{},
If[extras=!={},Return[]];
currentPoint=MousePosition["Graphics",Graphics];
If[CurrentValue["ShiftKey"],
extras=Inset[ActionMenu["Click action",{"Delete nearest point":>deletePoint[],"Cancel":>(extras={})}],currentPoint,{Left,Top}],
extras={};
dataPoints=Append[dataPoints,MousePosition["Graphics",Graphics]]
]
];
pt1=Graphics[{Antialiasing->False,Line[{{-1,0},{1,0}}],Line[{{0,-1},{0,1}}]},ImageSize->{10,10}];
extras={};
Column[{EventHandler[Dynamic[Deploy[ListPlot[dataPoints,PlotRange->{{0,6},{0,6}},ImageSize->{800,600},
PlotMarkers->pt1,Epilog->Dynamic[extras]]]],{{"MouseDown",1}:>processClick[]},PassEventsDown->True],
SequenceForm["Click to add points shift-click for other options, mouse at: ",Dynamic[MousePosition["Graphics",Graphics]]]
}]

This code could be elaborated, but as with my SWP examples, I want to show something that can help jump - start your own development.I have also not wrapped this construction in a DynamicModule – again to keep it easy to read.

Notice in particular that the variable' extras' starts out as an empty list and is displayed dynamically, so that the menu control can be added and removed as required.

This example obviously bears a strong resemblance to example 1 for the Super Widget Package! In that example, I used right mouse clicks to activate a context menu.Although it is possible to trap the right mouse with Mathematica 6, I found that trapping Shift - click seemed more reliable.

General purpose graphic zoom palette

This is an example, kindly provided by Daniel Huber of Metohm Ltd. It creates a palette which can be used to zoom into any 2D graphics object in a notebook. Run the code to create the palette, then select any graphics object by clicking on it, and press the zoom button to view an enlarged version in a separate (temporary) notebook. This example uses the fact that graphics objects in 6.0 have the same structure in the FrontEnd as they do in the kernel – and can thus be picked out of a notebook and manipulated further. For this reason, you cannot expand graphics created with Mathematica version 5.2 or earlier.

You will see the graphics displayed twice in the temporary notebook. Use the two locators in the top graphic to selected the rectangular zoom region to be displayed in the second window. Note that the temporary notebook can be stretched or maximised as desired.

Zoom2D[g0_]:=Module[{g,pp1,pp2},
If[!MatchQ[Head[g0],Graphics|GraphicsBox],CreateDialog[{
TextCell["Zoom:\n\nNo Input available."],
DefaultButton[]
}
];
Return[];
];
g=If[Head[g0]=!=Graphics,
Replace[g0,head_[x1_,x2___]:>Graphics[{x1},{x2}]],
g0];
g=g/.NCache[_,x_]:>x;
{pp1,pp2}=FullOptions[g,PlotRange]//Transpose;
g1=DynamicModule[{p1=pp1,p2=pp2,lp={0,0}},Column[{
LocatorPane[Dynamic[{p1,p2}],Show[{g,Graphics[{Opacity[0.1],Rectangle[Dynamic[p1],Dynamic[p2]]}]},ImageSize->Small]],
EventHandler[Show[g,PlotRange:>Dynamic[Transpose[{p1,p2}]],Frame->True,ImageSize->Full],{"MouseClicked":>(lp=MousePosition["Graphics"])}],
Dynamic[lp]
},Center]
];
CreateDocument[g1,WindowSize->{Small,All},WindowTitle->"Zoom-DH",WindowElements->{},WindowFrame->"Palette",BoxForm`ClosingSaveDialog->False];
];



CreatePalette[Button["Zoom",Zoom2D[NotebookRead[SelectedNotebook[]]]],WindowTitle->""];

It is important to realise that the graph is not re-creted each time it is zoomed, so for example, you may end up with too few sample points on a graph created using Plot. Note that because Mathematica adds axes to a plot automatically, the axes on the zoomed plot do reflect the zoomed ranges.