Next: , Previous: , Up: MGL scripts   [Contents][Index]

8.3 LaTeX package

There is LaTeX package mgltex (was made by Diego Sejas Viscarra) which allow one to make figures directly from MGL script located in LaTeX file.

For using this package you need to specify --shell-escape option for latex/pdflatex or manually run mglconv tool with produced MGL scripts for generation of images. Don’t forgot to run latex/pdflatex second time to insert generated images into the output document. Also you need to run pdflatex third time to update converted from EPS images if you are using vector EPS output (default).

The package may have following options: draft, final — the same as in the graphicx package; on, off — to activate/deactivate the creation of scripts and graphics; comments, nocomments — to make visible/invisible commentaries contained inside mglcomment environments; jpg, jpeg, png — to export graphics as JPEG/PNG images; eps, epsz — to export to uncompressed/compressed EPS format as primitives; bps, bpsz — to export to uncompressed/compressed EPS format as bitmap (doesn’t work with pdflatex); pdf — to export to 3D PDF; tex — to export to LaTeX/tikz document.

The package defines the following environments:


It writes its contents to a general script which has the same name as the LaTeX document, but its extension is .mgl. The code in this environment is compiled and the image produced is included. It takes exactly the same optional arguments as the \includegraphics command, plus an additional argument imgext, which specifies the extension to save the image.

An example of usage of ‘mgl’ environment would be:

  new a 50 40 '0.6*sin(pi*(x+1))*sin(1.5*pi*(y+1))+0.4*cos(0.75*pi*(x+1)*(y+1))'
  new b 50 40 '0.6*cos(pi*(x+1))*cos(1.5*pi*(y+1))+0.4*cos(0.75*pi*(x+1)*(y+1))'

    fog 0.5
    call 'prepare2d'
    subplot 2 2 0 : title 'Surf plot (default)' : rotate 50 60 : light on : box : surf a

    subplot 2 2 1 : title '"\#" style; meshnum 10' : rotate 50 60 : box
    surf a '#'; meshnum 10

    subplot 2 2 2 : title 'Mesh plot' : rotate 50 60 : box
    mesh a

    new x 50 40 '0.8*sin(pi*x)*sin(pi*(y+1)/2)'
    new y 50 40 '0.8*cos(pi*x)*sin(pi*(y+1)/2)'
    new z 50 40 '0.8*cos(pi*(y+1)/2)'
    subplot 2 2 3 : title 'parametric form' : rotate 50 60 : box
    surf x y z 'BbwrR'

It adds its contents to the general script, without producing any image.


Is exactly the same as ‘mgl’, but it writes its contents verbatim to its own file, whose name is specified as a mandatory argument.


Is exactly the same as ‘mglcode’, but it doesn’t produce any image, nor accepts optional arguments. It is useful, for example, to create a MGL script, which can later be post processed by another package like "listings".


It writes its contents verbatim to a file, specified as a mandatory argument, and to the LaTeX document, and numerates each line of code.


Exactly the same as ‘mglblock’, but it doesn’t write to a file. This environment doesn’t have arguments.


Is used to define MGL functions. It takes one mandatory argument, which is the name of the function, plus one additional argument, which specifies the number of arguments of the function. The environment needs to contain only the body of the function, since the first and last lines are appended automatically, and the resulting code is written at the end of the general script, after the stop command, which is also written automatically. The warning is produced if 2 or more function with the same name is defined.


Is used to contain multiline commentaries. This commentaries will be visible/invisible in the output document, depending on the use of the package options comments and nocomments (see above), or the \mglcomments and \mglnocomments commands (see bellow).


If many scripts with the same code are to be written, the repetitive code can be written inside this environment only once, then this code will be used automatically every time the ‘\mglplot’ command is used (see below). It takes one optional argument, which is a name to be associated to the corresponding contents of the environment; this name can be passed to the ‘\mglplot’ command to use the corresponding block of code automatically (see below).

The package also defines the following commands:


It takes one mandatory argument, which is MGL instructions separated by the symbol ‘:’ this argument can be more than one line long. It takes the same optional arguments as the ‘mgl’ environment, plus an additional argument setup, which indicates the name associated to a block of code inside a ‘mglsetup’ environment. The code inside the mandatory argument will be appended to the block of code specified, and the resulting code will be written to the general script.

An example of usage of ‘\mglplot’ command would be:

    box '@{W9}' : axis
  box : axis
  grid 'xy' ';k'
  rotate 50 60
  box : axis : grid 'xyz' ';k'
  \mglplot[scale=0.5]{new a 200 'sin(pi*x)' : plot a '2B'}
    fplot 'sin(pi*x)' '2B' :
    fplot 'cos(pi*x^2)' '2R'
  \mglplot[setup=3d]{fsurf 'sin(pi*x)+cos(pi*y)'}

This command takes the same optional arguments as the ‘mgl’ environment, and one mandatory argument, which is the name of a MGL script. This command will compile the corresponding script and include the resulting image. It is useful when you have a script outside the LaTeX document, and you want to include the image, but you don’t want to type the script again.


This is like ‘\mglgraphics’ but, instead of creating/including the corresponding image, it writes the contents of the MGL script to the LaTeX document, and numerates the lines.


This command can be used in the preamble of the document to specify a directory where LaTeX will save the MGL scripts and generate the corresponding images. This directory is also where ‘\mglgraphics’ and ‘\mglinclude’ will look for scripts.


Adjust the quality of the MGL graphics produced similarly to quality.

\mgltexon, \mgltexoff

Activate/deactivate the creation of MGL scripts and images. Notice these commands have local behavior in the sense that their effect is from the point they are called on.

\mglcomment, \mglnocomment

Make visible/invisible the contents of the mglcomment environments. These commands have local effect too.


It just pretty prints the name of the package.

As an additional feature, when an image is not found or cannot be included, instead of issuing an error, mgltex prints a box with the word ‘MGL image not found’ in the LaTeX document.

Next: , Previous: , Up: MGL scripts   [Contents][Index]