Debian Codegraph @

texmf-dist/doc/lualatex/luaplot/luaplot.tex

raw
1 \documentclass{article}
2 \usepackage{listings,color,parskip,booktabs,longtable,array,hyperref,luaplot}
3 \usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
4 \hypersetup{colorlinks,urlcolor=blue}
5 \lstset{frame=none,
6 language=[LaTeX]{TeX},
7 aboveskip=3mm,
8 belowskip=3mm,
9 showstringspaces=false,
10 columns=flexible,
11 basicstyle={\ttfamily},
12 numbers=none,
13 numberstyle=\tiny\color{gray},
14 stringstyle=\color{mauve},
15 breaklines=true,
16 breakatwhitespace=true,
17 tabsize=1
18 }
19 \usepackage{microtype,graphicx,amsmath,amssymb,float}
20 \usepackage[backend=bibtex]{biblatex}
21 \setlength\bibitemsep{\baselineskip}
22 \addbibresource{luaplot}
23 \begin{document}
24 \title{The luaplot package in LaTeX}
25 \author{Chetan Shirore\thanks{Email id: [email protected]} \space and Ajit Kumar}
26 \maketitle
27
28 \section{Introduction}
29 The \verb|luaplot| package is developed using Lua to plot graphs of real-valued functions of a real variable in LaTeX. It is developed with the \verb|MetaPost| system \cite{online.metapost} and \verb|luamplib| \cite{online.luamplib} and \verb|luacode| \cite{online.luacode} 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.
30
31 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:
32 \begin{itemize}
33 \item parse plot points to the MetaPost system via \verb|luampblib|.
34 \item parse plot points to the \verb|tikz| package.
35 \end{itemize}
36
37 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.
38
39 Tikz is designed by Till Tantau for producing vector graphics from different expressions. Drawing lines, arrows, paths, geometric shapes, etcetera is possible using Tikz \cite{online.pgf}. 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.
40
41 \section{Installation and License}
42
43 The installation of \verb|luaplot| package is similar to plain latex package, where the \texttt{.sty} file is in LaTeX directory of texmf tree. The package can be included with \verb|\usepackage{luaplot}| command in the preamble of the LaTeX document. A TeX file is to be compiled using the LuaLaTeX engine.
44
45 The \verb|luaplot| package is released under the LaTeX Project Public License v1.3c or later. The complete license text is available at \url{http://www.latex-project.org/lppl.txt}. 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.
46
47 \section{The luaplot command}
48 The \verb|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 \verb|luaplot| command has the following syntax. It uses \verb|xkeyval| package \cite{online.xkeyval} for dealing with optional parameters.
49 \begin{center}
50 \begin{lstlisting}[caption={[]}]
51 \luaplot[optional parameters]{function(s)}
52 \end{lstlisting}
53 \end{center}
54 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 \emph{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 \ref{tbl:paraluaplot} lists optional parameters in the \verb|luaplot| command with their brief description.
55 \begin{center}
56 \begin{longtable}{lm{11.3cm}}
57 \toprule
58 \multicolumn{1}{l}{\textcolor{blue}{Optional Parameter}} & \multicolumn{1}{c}{\textcolor{blue}{Description}} \\
59 \toprule
60 \begin{lstlisting}[caption={[]}]
61 xmin
62 \end{lstlisting}
63 & Specifies the minimum value of \(x \) coordinate used for calculating plot points. The default value of xmin is \(0 \). \\
64 \midrule
65 \begin{lstlisting}[caption={[]}]
66 xmax
67 \end{lstlisting}
68 & Specifies the maximum value of \(x \) coordinate used for calculating plot points. The default value of xmax is \(5 \).\\
69 \midrule
70 \begin{lstlisting}[caption={[]}]
71 ymin
72 \end{lstlisting}
73 & Specifies the minimum value of \(y \) coordinate used for calculating plot points. If not specified, it is determined by values of xmin and xmax. \\
74 \midrule
75 \begin{lstlisting}[caption={[]}]
76 ymax
77 \end{lstlisting}
78 & Specifies the maximum value of \(y \) coordinate used for calculating plot points. If not specified, it is determined by values of xmin and xmax.\\
79 \midrule
80 \begin{lstlisting}[caption={[]}]
81 plotpts
82 \end{lstlisting}
83 & Specifies the number of plot points used to plot graphs of functions. The default value of \texttt{noofpoints} is 100.\\
84 \midrule
85 \begin{lstlisting}[caption={[]}]
86 hor
87 \end{lstlisting}
88 & Specifies the width of plotting window. The default value of width is 5cm. It can be specified in any unit that LaTeX accepts: \emph{pt}(point), \emph{pc}(pica), \emph{in}(inch), \emph{cm}(centimeter), \emph{mm}(millimeter), etcetera.\\
89 \midrule
90 \begin{lstlisting}[caption={[]}]
91 ver
92 \end{lstlisting}
93 & Specifies the height of plotting window. The default value of height is 3cm. It can be specified in any unit that LaTeX accepts: \emph{pt}(point), \emph{pc}(pica),\emph{in}(inch), \emph{cm}(centimeter), \emph{mm}(millimeter), etcetera.\\
94 \midrule
95 \begin{lstlisting}[caption={[]}]
96 plotsty
97 \end{lstlisting}
98 & Specifies plotting styles of curves. Styles for different curves are to be given in curly braces separated by comma. The basic syntax is \begin{lstlisting}[caption={[]}]
99 withpen<pen expression> | dashed<picture expression>
100 \end{lstlisting} It can be scaled also. For all possible styles, the MetaPost package \cite{online.metapost} can be referred.\\
101 \midrule
102 \begin{lstlisting}[caption={[]}]
103 clr
104 \end{lstlisting}
105 & Specifies colors for plotting. Colors for different curves are to be given in curly braces separated by semicolon. The default color is black. Colors 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. \\
106 \midrule
107 \begin{lstlisting}[caption={[]}]
108 plotoptions
109 \end{lstlisting}
110 & 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 grids, labels, legends, axes styles, point markings on axes, framing styles etcetera. For all possible options, the MetaPost package documentation \cite{online.metapost} and guide \cite{article.hobby} can be referred. \\
111 \bottomrule \\
112 \caption{Optional parameters in the luaplot command}
113 \label{tbl:paraluaplot}
114 \end{longtable}
115 \end{center}
116
117 \section{The luatikzpath command}
118 The \verb|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 draw options, node options, tikz styles etcetera in tikz. The sequence of points is entirely determined using Lua. The \verb|luatikzpath| command in luaplot package has the following syntax.
119 \begin{center}
120 \begin{lstlisting}[caption={[]}]
121 \luaplot{function}{xmin}{xmax}{plot points}
122 \end{lstlisting}
123 \end{center}
124 The first compulsory argument is the function. The standard functions available from mathematics library: \emph{abs, acos, asin, atan, ceil, cos, exp, floor, log, sin, sqrt, tan,} etcetera can be input. The second and third compulsory arguments are \verb|xmin| and \verb|xmax|. These specify the beginning and end values of \(x\) coordinate. The last compulsory argument is the number of points used for plotting.
125
126
127 \section{Plotting graphs with the luaplot command}
128 Table \ref{tbl:luaplotex} illustrates plotting graphs of functions with the \verb|luaplot| command.
129 \begin{center}
130 \begin{longtable}{m{6cm}m{10cm}}
131 \toprule
132 \multicolumn{1}{l}{\textcolor{blue}{LaTeX Input}} & \multicolumn{1}{l}{\textcolor{blue}{Graph Produced}} \\
133 \toprule
134 \begin{lstlisting}[caption={[]}]
135 \luaplot{exp(x)}
136 \end{lstlisting} & \newline \luaplot{exp(x)} \\ \midrule
137 \begin{lstlisting}[caption={[]}]
138 \luaplot[
139 xmin=-1, xmax=11,
140 clr={ 'blue' },
141 plotsty={'dashed evenly'}
142 ]{cos(x)}
143 \end{lstlisting} & \newline \luaplot[
144 xmin=-1, xmax=11,
145 clr={ 'blue' },
146 plotsty={'dashed evenly'}
147 ]{cos(x)} \\ \midrule
148 \begin{lstlisting}[caption={[]}]
149 \luaplot[
150 xmin=-1, xmax=11,
151 ymin=-2, ymax=2,
152 plotpts=1000,
153 clr={ 'red; blue' }
154 ]{cos(x),sin(x)}
155 \end{lstlisting} & \newline \luaplot[
156 xmin=-1, xmax=11,
157 ymin=-2, ymax=2,
158 plotpts=1000,
159 clr={ 'red; blue' }
160 ]{cos(x),sin(x)} \\ \midrule
161
162 \begin{lstlisting}[caption={[]}]
163 \luaplot[
164 clr={ 'red; green;
165 (0.58,0.2,0.62)' },
166 xmin=-10,xmax=10,
167 ymin=-10,ymax=10]
168 {x^3,-x^3,x}
169 \end{lstlisting} & \newline
170 \luaplot[
171 clr={ 'red; green; (0.58,0.2,0.62)' },
172 xmin=-10,xmax=10,ymin=-10,ymax=10]
173 {x^3,-x^3,x} \\ \midrule
174 \begin{lstlisting}[caption={[]}]
175 \luaplot[xmin=-5,xmax=5,
176 clr={'red; blue'}]
177 {(2*(x^2)-1)/(x^2-1),1/x}
178 \end{lstlisting} & \newline \luaplot[xmin=-5,xmax=5,
179 clr={'red; blue'}]
180 {(2*(x^2)-1)/(x^2-1),1/x} \\
181 \bottomrule \\
182 \caption{Plotting graphs with the luaplot command}
183 \label{tbl:luaplotex}
184 \end{longtable}
185 \end{center}
186
187 Listing \ref{code:luaplotopt} illustrates the use of optional argument \verb|plotoptions| in the \verb|luaplot| command. It generates graphs shown in Figure \ref{fig:luaplot2}.
188 \begin{lstlisting}[label={code:luaplotopt}, caption={Plotting with the luaplot command}]
189 \luaplot[
190 xmin=-10,
191 xmax=20,
192 plotpts=300,
193 hor='6cm',
194 ver='5cm',
195 clr={'red; blue'},
196 plotsty={'dashed evenly'},
197 plotoptions={
198 [[
199 glabel(btex ${sin(x)+x}$ etex,(7,11.5));
200 glabel(btex ${cos(x)}$ etex,(12,2));
201 glabel.bot(btex Some Graphs etex, OUT);
202 autogrid(grid.bot,) withcolor .85white;
203 autogrid(grid.lft,) withcolor .85white;
204 frame.dashed evenly;
205 ]]
206 }
207 ]
208 {sin(x)+x,cos(x)}
209 \end{lstlisting}
210
211 \begin{figure}
212 \hspace{4cm}
213 \luaplot[
214 xmin=-10,
215 xmax=20,
216 plotpts=300,
217 hor='6cm',
218 ver='5cm',
219 clr={'red; blue'},
220 plotsty={'dashed evenly'},
221 plotoptions={
222 [[
223 glabel(btex ${sin(x)+x}$ etex,(7,11.5));
224 glabel(btex ${cos(x)}$ etex,(12,2));
225 glabel.bot(btex Some Graphs etex, OUT);
226 autogrid(grid.bot,) withcolor .85white;
227 autogrid(grid.lft,) withcolor .85white;
228 frame.dashed evenly;
229 ]]
230 }
231 ]
232 {sin(x)+x,cos(x)}
233 \caption{Plotting graphs with the luaplot package}
234 \label{fig:luaplot2}
235 \end{figure}
236
237 \section{Plotting graphs with the luatikzpath command}
238 Listing \ref{code:luatikzpath} illustrates plotting graph of a function with the \verb|luatikzpath| command. Multiple graphs can be plotted in a single picture environment.
239 \begin{lstlisting}[label={code:luatikzpath}, caption={Plotting with the luatikzpath command}]
240 \begin{tikzpicture}
241 \draw[thin,->] (-4.2,0)--(5,0)node[right]{$x$};
242 \draw[thin,->] (0,-3)--(0,2.5)node[above]{$y$};
243 \draw[red] \luatikzpath{sin(x^2)}{-4}{4}{100} node at (1,1.3) {$y=\sin(x^2)$};
244 \draw[blue] \luatikzpath{log(x)}{0.1}{4.5}{100} node at (4,1.7) {$y=\log(x)$};
245 \end{tikzpicture}
246 \end{lstlisting}
247
248 Listing \ref{code:luatikzpath} generates graphs shown in Figure \ref{fig:luatikzpath}.
249
250 \begin{figure}[H]
251 \centering
252 \begin{tikzpicture}
253 \draw[thin,->] (-4.2,0)--(5,0)node[right]{$x$};
254 \draw[thin,->] (0,-3)--(0,2.5)node[above]{$y$};
255 \draw[red] \luatikzpath{sin(x^2)}{-4}{4}{100} node at (1,1.3) {$y=\sin(x^2)$};
256 \draw[blue] \luatikzpath{log(x)}{0.1}{4.5}{100} node at (4,1.7) {$y=\log(x)$};
257 \end{tikzpicture}
258 \caption{Plotting with the luatikzpath command}
259 \label{fig:luatikzpath}
260 \end{figure}
261 \section{Known issues and limitations}
262 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.
263 \printbibliography
264 \end{document}