### 12.5 The tegprog environment and the tegrun macro

The texgraph.sty package also provides the environment:

\begin{tegprog}[<options>]{name}
<code TeXgraph>
\end{tegprog}

That environment saves the program <name>.teg. That program is created to be executed by the command tegrun, the parameters that will be used will be in the global variable param (list) of the program. That program also has a macro Return(string), that is writing the string in an output file. That file will be automatically included by the command tegrun.

Possible options are:

• file: that option shows that the inside of the environment is a whole TeXgraph source file, if not, this is only a command.
• commandchars: with that option, the environment may contain calls to TEX commands if the backslash $\$ is replaced by # before the commands name, ex: #command{...}. If this command contains macros that must not be developped, they will have to be preceeded by \noexpand.
• preload = < {"<file1>";"<file2>";...} >: loads one or several packages before creating the graphic, eg: preload={"papiers.mod";"draw2d.mod"}.

Once saved, a program can be executed in a TEX document with the command:

\tegrun{name}{param1 param2 ...}, it is saving the parameters in the file <name>.prm, ask TeXgraph to execute the program <name>.teg, and include the result file <name>.res. Here is an example:

\begin{tegprog}{PrintPgcd}
a:=param[1], b:=param[2],
if a<b then Echange(a,b) fi,
Return("\begin{tabular}{|c|c|c|}\par\hline{}a&b&r\tabularnewline\hline"),
r:=b,
while r>0 do
r:=mod(a,b),
Return(Concat(a,"&",b,"&",r,"\tabularnewline\hline")),
a:=b, b:=r
od,
Return("\end{tabular}")
\end{tegprog}
\newcommand{\PrintPgcd}[2]{\tegrun{PrintPgcd}{#1 #2}}%

In that example, the program PrintPgcd.teg is created, it calculates the gcd of two integers $a$ and $b$ by giving all the steps of the Euclid algorithm in the form of a tabular. The parameters list is in the variable param1 . The macro Return write in the output file whose name is PrintPgcd.res.

Then a two arguments macro called PrintPgcd is created. That macro calls the command \tegrun{PrintPgcd}{#1 #2}, that command write the two arguments in the parameter file PrintPgcd.prm, and ask TeXgraph to execute the program PrintPgcd.teg, and finally includes the file PrintPgcd.res.

The execution of \PrintPgcd{456}{166} gives

 a b r 456 166 124 166 124 42 124 42 40 42 40 2 40 2 0
.