Programmatic Content Generation
New subpackages and commands within the DocumentTools package now allow fully programmatic functionality to create and make use of Worksheet and Document content, including:
Customized Tables
Applications containing Embedded Components and Code Edit Regions
Sections and subsections, Document Blocks and Execution Groups, customized text, and input and output
Insertion into the current document, and launching or saving as a new document
This functionality is already in use by several other Maple commands, including Explore, ImageTools:-Embed, and the new Tabulate command.
Layout elements
Embedded Components and application authoring
The Tabulate command
The DocumentTools:-Layout subpackage contains commands for constructing basic elements of a Document or Worksheet.
Commands currently available and documented are as indicated by the following command to load the subpackage:
with⁡DocumentTools:-Layout
Cell,Column,DocumentBlock,Equation,Font,Group,Image,InlinePlot,Input,Output,Row,Section,Table,Textfield,Worksheet
These commands can be used to construct a structure of nested Maple function calls which comprise an XML format representation of a complete Document or Worksheet. The resulting document content can be inserted, launched, or saved.
The following is a simple example of construction and insertion of such content. This example uses the constructor commands Worksheet, Table, Row, Textfield, and Equation.
E1:=Equation⁡'∑i=1ni',style=TwoDimInput:E2≔Equationfactor⁡∑i=1ni,style=TwoDimInput,typesetting=extended:T≔TextfieldThe sum of the first n positive integers ,E1, is equal to ,E2,alignment=centered:xml≔WorksheetTable⁡Row⁡T,alignment=center,width=50:
The InsertContent command can be used to insert this content directly into the current document.
DocumentTools:-InsertContent⁡xml:
The sum of the first n positive integers ∑i=1ni is equal to n⁢n+12
The DocumentTools:-Components subpackage contains commands for constructing Embedded Components as content.
with⁡DocumentTools:-Components
Button,CheckBox,CodeEditRegion,ComboBox,DataTable,Dial,Label,ListBox,MathContainer,Meter,Microphone,Plot,RadioButton,RotaryGauge,Shortcut,Slider,Speaker,TextArea,ToggleButton,VideoPlayer,VolumeGauge
An interactive application can be completely programmatically constructed as content.
The following example consists of constructing a Plot and a Button component, each with their own action code. Each time the mouse cursor is clicked within the Plot component a new data point is added to the list assigned to the global name L, and a least squares linear fit of this data is plotted. Clicking on the Button clears the plot.
The application is inserted into the current document for this first example. This example uses the commands Plot and Button to construct the components' contents.
This example also uses two procedures, update and clear, which must be defined (and left as defined) for the inserted application to function. These procedures are called whenever the respective components are clicked.
update:=proccomp local cx,cy,v; global L; use DocumentTools, plots, CurveFitting incx,cy:=Docomp⁡clickx,Docomp⁡clicky;iftypeL,listlistthenL:=L,cx,cyelseL:=cx,cyend if;Docomp=display⁡plot⁡L,style=point,symbol=solidcircle,symbolsize=15,color=blue,axes=normal,plot⁡LeastSquares⁡L,v,v=0..10,view=0..10,0..10; end use:end proc:
clear:=proccomp DocumentTools:-Docomp=plot⁡0,0,color=white,axes=normal,view=0..10,0..10end proc:
Next, the two components are constructed. Their respective action code, executed whenever the components are clicked upon, call the aforementioned procedures.
P:=Plot⁡plot⁡0,0,color=white,axes=normal,view=0..10,0..10,identity=Plot0,clickdefault,clickaction=update(%Plot0);:B:=Button⁡Clear,identity=Button0,action=unassign(':-L'); clear(%Plot0);:
The content construction is then completed, forming a full worksheet in XML representation. Next, this is inserted into the current worksheet.
xmlapp:=Worksheet⁡Group⁡Input⁡Textfield⁡B,P:DocumentTools:-InsertContentxmlapp:
In the second example, the same application is regenerated, but now the defining of the supporting procedures update and clear is done within the constructed application itself. This allows the application to function even if it is launched or saved as a new worksheet.
In the following revision, the procedure definitions are part of the action code of the two components. An alternative approach is to put those procedure definitions into a Code Edit Region constructed as an additional part of the content.
P2:=Plotplot0,0,color=white,axes=normal,view=0..10,0..10, identity=Plot0,clickdefault, clickaction=catsprintfupdate:=%a;,evalupdate,update(%Plot0);:
B2≔ButtonClear,identity=Button0, action=catsprintf⁡clear:=%a;,evalclear,unassign(':-L'); clear(%Plot0);:
xmlapp2:=Worksheet⁡Group⁡Input⁡Textfield⁡B2,P2:
The ContentToString command is used to create a text string of an actual worksheet. This string can be launched directly in a new window, or saved as a worksheet file.
appstring:=DocumentTools:-ContentToString⁡xmlapp2:
This worksheet string can be launched directly in a new window using the Display command from the Worksheet package. In such a new window,the application functions independently of any definitions inside the current worksheet.
The following invocation uses the global name syntax :-Worksheet in order to reference the package by that name rather than the export by that same name in the previously loaded Layout package.
:-Worksheet:-Display⁡appstring
The test string of the worksheet can also be saved as a worksheet file.
fname:=FileTools:-TemporaryFile⁡:FileTools:-Text:-WriteFile⁡fname,appstring:
The saved file is an application that functions independently of any definitions made in the current worksheet.
:-Worksheet:-DisplayFile⁡fname
The Tabulate command allows indexed structures, such as lists, Vectors, Matrices, or Arrays to be formatted with their elements becoming the entries in the cells of an inserted Table GUI element.
M:=LinearAlgebra:-RandomMatrix⁡6,2,generator=0..100
DocumentTools:-Tabulate⁡M,widthmode=pixels,width=100
99
91
30
63
22
89
55
86
77
76
82
49
A:=plot⁡sin⁡xx,∑i=1Nf⁡i,plot3d⁡y2⁢sin⁡x,x=−π..π,y=−2..2:
DocumentTools:-Tabulate⁡A,width=60
∑i=1N⁡f⁡i
DocumentTools:-Tabulate⁡plot3d⁡y2⁢sin⁡x,x=−π..π,y=−2..2,axes=none,fillcolor=black,width=40
Download Help Document