Luaplot
Luaplot
1 Introduction
The luaplot package is developed using Lua to plot graphs of real-valued functions of a real variable in
LaTeX. It is developed with the MetaPost system [4] and luamplib [3] and luacode [2] packages. It provides
an easy way for plotting graphs of standard mathematical functions and their finite combinations. There
is no particular environment in the package for plotting graphs. It also works inside floating environments
of LaTeX like tables and figures. The compilation time to plot several graphs in LaTeX using the luaplot
package is significantly less with LuaLaTeX engine.
The package is based on the core idea of loading mathematical functions inside Lua and determining plot
points using different methods available in Lua. After determining plot points in Lua, two different approaches
are used:
• parse plot points to the MetaPost system via luampblib.
• parse plot points to the tikz package.
The MetaPost system is based on the Metafont to produce precise technical illustrations. Donald Knuth
designed Metafont for TeX. John Hobby designed the MetaPost system to produce scalable PostScript or
scalable vector graphics. The output from MetaPost can be directly included with LaTeX. The first approach
thus offers a native way of plotting graphs inside LaTeX using Lua and MetaPost.
Tikz is designed by Till Tantau for producing vector graphics from different expressions. Drawing lines,
arrows, paths, geometric shapes, etcetera is possible using Tikz [5]. Tikz commands can be considered TeX
macros, but Tikz itself is a language. LaTeX users widely use Tikz to produce different graphics. The second
approach combines Lua and Tikz to plot graphs inside LaTeX.
1
\luaplot[optional parameters]{function(s)}
The only compulsory argument is the function(s). Multiple functions can be given as input separated by a
comma. The standard functions available from mathematics library are abs, acos, asin, atan, ceil, cos, exp,
floor, log, sin, sqrt, tan, etcetera. The custom functions can easily be defined inside the luacode package.
Table 1 lists optional parameters in the luaplot command with their brief description.
It can be scaled also. For all possible styles, the MetaPost package [4] can
be referred.
Specifies colors for plotting. Colors for different curves are to be given in
curly braces separated by semicolon. The default color is black. Colors
clr can be specified in a variety of ways. Standard colours can be specified
by using names such as “red”; “blue”; “green” etcetera. The RGB format
(r,g,b) can also be used.
Specifies different options for plotting. The input is to be in curly braces.
If the expression is a big string and multi-line then it can be enclosed in
two square brackets (string in Lua). The different plot options include
plotoptions grids, labels, legends, axes styles, point markings on axes, framing styles
etcetera. For all possible options, the MetaPost package documentation [4]
and guide [1] can be referred.
2
draw options, node options, tikz styles etcetera in tikz. The sequence of points is entirely determined using
Lua. The luatikzpath command in luaplot package has the following syntax.
\luaplot{function}{xmin}{xmax}{plot points}
The first compulsory argument is the function. The standard functions available from mathematics library:
abs, acos, asin, atan, ceil, cos, exp, floor, log, sin, sqrt, tan, etcetera can be input. The second and third
compulsory arguments are xmin and xmax. These specify the beginning and end values of x coordinate. The
last compulsory argument is the number of points used for plotting.
150
100
\luaplot{exp(x)}
50
0
0 1 2 3 4 5
1
\luaplot[ 0.5
xmin=-1, xmax=11,
clr={ 'blue' }, 0
plotsty={'dashed evenly'} −0.5
]{cos(x)}
−1
0 2 4 6 8 10
2
\luaplot[
1
xmin=-1, xmax=11,
ymin=-2, ymax=2, 0
plotpts=1000,
clr={ 'red; blue' } −1
]{cos(x),sin(x)}
−2
0 2 4 6 8 10
3
10
\luaplot[
5
clr={ 'red; green;
(0.58,0.2,0.62)' }, 0
xmin=-10,xmax=10,
ymin=-10,ymax=10] −5
{x^3,-x^3,x}
−10
−10 −5 0 5 10
10
5
\luaplot[xmin=-5,xmax=5,
clr={'red; blue'}] 0
{(2*(x^2)-1)/(x^2-1),1/x} −5
−10
−4 −2 0 2 4
Listing 1 illustrates the use of optional argument plotoptions in the luaplot command. It generates graphs
shown in Figure 1.
4
20
sin(x) + x
10
cos(x)
0
−10
−10 0 10 20
Some Graphs
y = log(x)
y = sin(x2 )
5
7 Known issues and limitations
The package does not use any external library supporting arbitrary precision arithmetic. The luaplot package
can handle big and small numbers within the range of Lua that it supports. However, the MetaPost system
does not support numbers in scientific notation. The coordinates of plot points produced in scientific notation
are rounded off to 12 significant decimal places within the package. This may cause slight deviations from
actual values. The same issue is not faced while parsing points to tikz as it supports input in scientific
notation.
References
[1] John D. Hobby. Drawing Graphs with MetaPost. url: https://tug.org/docs/metapost/mpgraph.pdf.
[4] MetaPost system. 2020. url: https : / / www . tug . org / docs / metapost / mpman . pdf (visited on
02/22/2022).