0% found this document useful (0 votes)
12 views6 pages

Luaplot Package for Graphs in LaTeX

Uploaded by

mir.cazasu27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Luaplot Package for Graphs in LaTeX

Uploaded by

mir.cazasu27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The luaplot package in LaTeX

Chetan Shirore and Ajit Kumar

July 20, 2023

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.

2 Installation and License


The installation of luaplot package is similar to plain latex package, where the .sty file is in LaTeX directory
of texmf tree. The package can be included with \usepackage{luaplot} command in the preamble of the
LaTeX document. A TeX file is to be compiled using the LuaLaTeX engine.
The luaplot package is released under the LaTeX Project Public License v1.3c or later. The complete license
text is available at [Link] It is developed in Lua. Lua is available as
a certified open-source software. Its license is simple and liberal, which is compatible with GPL.

3 The luaplot command


The luaplot command plots graph of a real valued function of a real variable in LaTeX using Lua and the
MetaPost system. The plot points are exclusively determined using Lua. The algorithm is quite efficient to
calculate coordinates of points for plotting in the MetaPost. It is deliberately written in Lua. The graphing is
done using the MetaPost system. The luaplot command has the following syntax. It uses xkeyval package
[6] for dealing with optional parameters.

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.

Optional Parameter Description


Specifies the minimum value of x coordinate used for calculating plot points.
xmin The default value of xmin is 0.
Specifies the maximum value of x coordinate used for calculating plot
xmax points. The default value of xmax is 5.
Specifies the minimum value of y coordinate used for calculating plot points.
ymin If not specified, it is determined by values of xmin and xmax.
Specifies the maximum value of y coordinate used for calculating plot
ymax points. If not specified, it is determined by values of xmin and xmax.
Specifies the number of plot points used to plot graphs of functions. The
plotpts default value of noofpoints is 100.
Specifies the width of plotting window. The default value of width is 5cm.
hor It can be specified in any unit that LaTeX accepts: pt(point), pc(pica),
in(inch), cm(centimeter), mm(millimeter), etcetera.
Specifies the height of plotting window. The default value of height is
ver 3cm. It can be specified in any unit that LaTeX accepts: pt(point),
pc(pica),in(inch), cm(centimeter), mm(millimeter), etcetera.
Specifies plotting styles of curves. Styles for different curves are to be given
in curly braces separated by comma. The basic syntax is

plotsty withpen<pen expression> | dashed<picture expression>

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.

Table 1: Optional parameters in the luaplot command

4 The luatikzpath command


The luatikzpath command produces a sequence of points for plotting that can be used inside tikz. The best
feature is that all plotting options in tikz remain with the user. The all custom formatting can be done with

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.

5 Plotting graphs with the luaplot command


Table 2 illustrates plotting graphs of functions with the luaplot command.

LaTeX Input Graph Produced

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

Table 2: Plotting graphs with the luaplot command

Listing 1 illustrates the use of optional argument plotoptions in the luaplot command. It generates graphs
shown in Figure 1.

Listing 1: Plotting with the luaplot command


\luaplot[
xmin=-10,
xmax=20,
plotpts=300,
hor='6cm',
ver='5cm',
clr={'red; blue'},
plotsty={'dashed evenly'},
plotoptions={
[[
glabel(btex ${sin(x)+x}$ etex,(7,11.5));
glabel(btex ${cos(x)}$ etex,(12,2));
[Link](btex Some Graphs etex, OUT);
autogrid([Link],) withcolor .85white;
autogrid([Link],) withcolor .85white;
[Link] evenly;
]]
}
]
{sin(x)+x,cos(x)}

4
20

sin(x) + x
10

cos(x)
0

−10
−10 0 10 20
Some Graphs

Figure 1: Plotting graphs with the luaplot package

6 Plotting graphs with the luatikzpath command


Listing 2 illustrates plotting graph of a function with the luatikzpath command. Multiple graphs can be
plotted in a single picture environment.

Listing 2: Plotting with the luatikzpath command


\begin{tikzpicture}
\draw[thin,->] (-4.2,0)--(5,0)node[right]{$x$};
\draw[thin,->] (0,-3)--(0,2.5)node[above]{$y$};
\draw[red] \luatikzpath{sin(x^2)}{-4}{4}{100} node at (1,1.3) {$y=\sin(x^2)$};
\draw[blue] \luatikzpath{log(x)}{0.1}{4.5}{100} node at (4,1.7) {$y=\log(x)$};
\end{tikzpicture}

Listing 2 generates graphs shown in Figure 2.

y = log(x)
y = sin(x2 )

Figure 2: Plotting with the luatikzpath command

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: [Link]

[2] luacode package. visited on 2022-03-10. 2012. url: [Link]


latex/luacode/[Link].

[3] luamplib package. 2022. url: [Link]


[Link] (visited on 02/22/2022).

[4] MetaPost system. 2020. url: https : / / www . tug . org / docs / metapost / mpman . pdf (visited on
02/22/2022).

[5] pgf Package. visited on 2021-01-10. url: [Link]

[6] xkeyval Package. visited on 2021-03-10. url: [Link]

Common questions

Powered by AI

Optional parameters in the luaplot command significantly enhance control over graph dimensions and styles. Parameters like xmin, xmax, ymin, ymax, plotpts, hor, and ver allow users to define coordinate boundaries, specify the graph's resolution, and adjust plot dimensions. Additionally, plotsty and clr provide control over curve styles and colors, enabling detailed customization and ensuring that users can tailor their graphs to specific presentation needs .

Lua determines the plot points in the luaplot package by evaluating real-valued functions and calculating coordinates efficiently. This approach leverages Lua's computational capabilities, improving the plotting process's speed and accuracy by offloading mathematical calculations to Lua, which operates seamlessly within the LuaLaTeX engine .

The luaplot package utilizes Lua by loading mathematical functions inside Lua and determining plot points using the methods available in Lua, which significantly reduces the compilation time for plotting multiple graphs when using the LuaLaTeX engine .

The luaplot package's known issues include the inability to handle numbers in scientific notation within the MetaPost system, which results in rounded plot point coordinates. This limitation can slightly deviate from expected values when precision is critical. However, this problem does not occur when using Tikz for parsing, as Tikz accommodates scientific notation, thus ensuring more accurate plotting for specific mathematical graphing needs .

The luaplot package's use of MetaPost focuses on rendering precise technical illustrations in scalable vector graphics, leveraging the Metafont-inspired system designed for integration with LaTeX. Conversely, its use of Tikz emphasizes flexibility and extensive customization with stylistic graphics commands, allowing for intricate designs. MetaPost is more focused on accuracy and scale, while Tikz provides broader artistic freedom and the ease of using LaTeX commands .

The luaplot command allows for extensive customization when used with Tikz, as it retains all of Tikz's options for drawing, node, and style configurations. The sequence of plot points, determined by Lua, integrates seamlessly with Tikz, enabling users to apply Tikz's custom formatting and stylistic capabilities, which makes the package highly flexible and customizable for diverse plotting needs .

In the luaplot package, users specify custom colors for curves using the clr optional parameter, accepting colors as standard names (e.g., 'red', 'blue', 'green') or in RGB format (e.g., (0.58,0.2,0.62)). This flexibility allows users to personalize the aesthetic of each plotted curve, supporting creativity and distinction among multiple plots in a graph .

The luaplot package uses two methods to handle plot points: i) Parsing plot points to the MetaPost system via luampblib, which offers native plotting within LaTeX using scalable vector graphics; ii) Parsing plot points to the tikz package, allowing users to benefit from Tikz's extensive formatting and styling options for vector graphics .

The luaplot package guarantees precision by parsing plot points determined in Lua either directly into MetaPost or Tikz. However, a limitation arises with MetaPost, which does not support scientific notation, leading to rounding off to 12 significant decimal places. This can cause slight deviations from actual values, though Tikz, which supports scientific notation, does not face this issue .

The luatikzpath command enhances the plotting capabilities by allowing the use of Lua sequences of points within the Tikz environment, giving users the ability to manipulate plotting options extensively with Tikz's expansive styling and node options. This integration makes complex customizations and multi-graph plots feasible within a single LaTeX document, leveraging the power of Tikz for precision and beauty .

You might also like