Next: Defining a subroutine
Up: Splice and template files
Previous: Splice and template files
Unstructured communication provides a one-way link for sending ordinary data and text. This process can be used to format Mathematica expressions and pass the results to an external file. This is much more flexible than translating code fragments and avoids the tedious (and sometimes error prone) step of manipulation using a text editor. Furthermore, Mathematica can be used to specify additional program information, ensuring a more efficient and flexible construction than is otherwise possible.
The Mathematica command Splice to processes a template file consisting of a mixture of (active) Mathematica and (passive) compiled language source code and produces an object-code output file. The active code is distinguished by statement delimiters (usually <* and *>) which is taken as Mathematica input. The file is processed by Splice which passes over the file, evaluates the active parts and replaces them with Mathematica output, before moving onto the next active block of code. The passive code is translated verbatim. The beauty of this approach is that results are substituted at the exact point in the file which was previously occupied by the active code.
The default format for the output file is determined by the extension of the input file. As an example, the command Splice[ myfile.mtex ], processes the file myfile.mtex with TeXForm and puts the result in the file myfile.tex. An output file name may be specified as a second argument to Splice.
E.g. Splice[myfile.mtex ,outfile.tex ].
Here is a simple example of the use of Splice.
In[1]:= !!example.mf (* Display the template file. *) main example real x,y x = 1. y = <* D[x^x Sin[x],x] *> write(*,*) y stop end
In[2]:= Splice["example.mf"]; (* Generate the source code file. *) In[3]:= !!example.f (* Display the source code file. *) main example real x,y x = 1. y = x**x*Cos(x) + (x**x + x**x*Log(x))*Sin(x) write(*,*) y stop end
The Splice option FormatType can be used to override default output styles. It is emphasised that the Assign functions require the setting FormatType->OutputForm as an option of Splice in order to ensure correct formatting of the output file.
Jorge Romao