Section 2.3 The <definition>
element and user namespace
Most diagrams will contain some graphical components that are related to one another. For instance, our example diagram in Figure 1.1.1 contains the tangent line to a graph at a point \(a\) and the point \((a,f(a))\text{.}\)
To help coordinate these relationships, PreFigure provides authors with a namespace in which to store important pieces of data (this is not to be confused with an XML namespace). The PreText source is again given in Listing 2.3.2.
<diagram dimensions="(300, 300)" margins="5">
<definition> a=1 </definition>
<definition> f(x)=exp(x/3)*cos(x) </definition>
<coordinates bbox="[-4,-4,4,4]">
<grid-axes xlabel="x" ylabel="y"/>
<graph function="f"/>
<tangent-line function="f" point="a"/>
<point p="(a, f(a))">
<m>(a,f(a))</m>
</point>
</coordinates>
</diagram>
Notice that Line 2 includes an element that defines a quantity \(a=1\text{.}\) This has the effect of assigning the value \(1\) to the symbol
a
and provides the capability to refer to this symbol again in later elements. For example, Lines 7 and 8 introduce the tangent line and the point in terms of a
.Furthermore, Line 3 includes the definition of the function \(f(x)\) that is reused in constructing both the graph and the tangent line.
Behind the scenes, everything to the right of the equals sign in a
<definition>
is interpreted as an expression from a small subset of the Python programming language with the exception that a ^ is interpreted as exponentation. Expressions are validated to make sure they do not introduce malicious code.The left side of the equals sign is interpreted as a symbol that refers to the result of evaluating the expression on the right side of the equals sign.
Lists and tuples are interpreted as
numpy
arrays, which enables authors to perform vector operations. Matrix multiplication is represented by @
. Figure 2.3.3 demonstrates with a linear combination of two vectors.The PreFigure source is shown in Listing 2.3.4. Notice how the vectors \({\mathbf
v}\) and \({\mathbf w}\) are defined and then combined in Line 4 to form the vector
comb
.<diagram dimensions="(300,300)" margins="5">
<definition>v=(2,-1)</definition>
<definition>w=(-2,3)</definition>
<definition>comb=2*v+w</definition>
<coordinates bbox="(-4,-4,4,4)">
<grid-axes/>
<vector v="v"/>
<vector v="w"/>
<vector v="comb" stroke="red"/>
<label p="v" alignment="se"><m>{\mathbf v}</m></label>
<label p="w" alignment="ne"><m>{\mathbf w}</m></label>
<label p="comb" alignment="ne"><m>2{\mathbf v}+{\mathbf w}</m></label>
</coordinates>
</diagram>
Another type of definition is given by the
<derivative>
tag as demonstrated by the following diagram and its PreFigure source.<diagram dimensions="(300,300)" margins="5">
<definition>f(x)=x^3-3*x+1</definition>
<derivative name="fp" function="f"/>
<coordinates bbox="(-3,-3,3,3)">
<grid-axes/>
<graph function="f"/>
<graph function="fp" stroke="green"/>
<label p="(1.8,f(1.8))" alignment="se"><m>f(x)</m></label>
<label p="(0.5,fp(0.5))" alignment="se"><m>f'(x)</m></label>
</coordinates>
</diagram>