Debian Codegraph @

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

raw
1 \documentclass{article}
2 \renewcommand{\baselinestretch}{1.2}
3 \usepackage{float,listings,color,booktabs,longtable,array,hyperref,luamaths,amsmath,amssymb}
4 \usepackage[top=1in, bottom=1in, left=1.1in, right=1.1in]{geometry}
5 \hypersetup{colorlinks,urlcolor=blue}
6 \lstset{frame=none,
7 language=[LaTeX]{TeX},
8 aboveskip=3mm,
9 belowskip=2mm,
10 showstringspaces=false,
11 columns=flexible,
12 basicstyle={\ttfamily},
13 numbers=none,
14 numberstyle=\tiny\color{gray},
15 stringstyle=\color{mauve},
16 breaklines=true,
17 breakatwhitespace=true,
18 tabsize=1
19 }
20 \usepackage{microtype,graphicx,amsmath,amssymb,float}
21 \usepackage[backend=bibtex]{biblatex}
22 \addbibresource{luamaths}
23 \begin{document}
24 \title{The luamaths package in LaTeX}
25 \author{Chetan Shirore\thanks{Email id: [email protected]} \space and Ajit Kumar}
26 \maketitle
27 \section{Introduction}\label{section:introduction}
28 The \verb|luamaths| package is developed to perform standard mathematical operations inside LaTeX documents using Lua. It provides an easy way to perform standard mathematical operations. There is no particular environment in the package for performing mathematical operations. The package commands can be used in any environment (including the mathematics environment). It is written in Lua, and tex file is to be compiled with LuaLatex engine. The time required for operations is not an issue while compiling with LuaLaTeX. There is no need to install Lua on the users system as tex distributions (TeXLive or MikTeX) come bundled with LuaLaTeX. It is useful to perform standard operations (addition, subtraction, multiplication, division, powers, roots etc.) in tex file itself. The package supports the nesting of commands for multiple operations. It can be modified or extended by writing custom Lua programs.
29
30 The Lua \cite{online.luaorg} programming language is a scripting language that can be embedded across platforms. With\verb|LuaTeX| \cite{online.luatex} and\verb|luacode| \cite{online.luacode} packages, it is possible to use Lua in LaTeX. The TeX \cite{online.tex} or LaTeX has scope for programming \cite{online.texscript}. However, with the weird internals of TeX, there are several limitations, especially for performing calculations on numbers in LaTeX documents. There are packages like \emph{pgf} \cite{online.pgf} and \emph{xparse} \cite{online.xparse} in LaTeX which provides some programming capabilities inside LaTeX documents. However, such packages do not provide the complete programming structure that Lua programming languages provide. The \verb|luacode| \cite{online.luacode} package is used in its development apart from the \verb|luacode| package. There is no need to install Lua on users system as TeX distributions (TeXLive or MikTeX) come bundled with LuaLaTeX. The package can be modified or extended by writing custom Lua programs.
31
32
33
34
35 \section{Installation and License}
36
37 The installation of \verb|luamaths| 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{luamaths}| command in the preamble of the LaTeX document. The TeX file is to be compiled using the LuaLaTeX engine.
38
39
40 The \verb|luamaths| 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.
41
42 \section{Commands in the luamaths package}
43 Table \ref{tbl:opluamaths} lists operations in the \verb|luamaths| package.
44 \begin{center}
45 \begin{longtable}{llm{6.5cm}}
46 \toprule
47 \multicolumn{1}{c}{\textcolor{blue}{Function}} & \multicolumn{1}{l}{\textcolor{blue}{Command Format}} & \multicolumn{1}{c}{\textcolor{blue}{Description}} \\
48 \toprule
49 \begin{lstlisting}
50 \mathOp
51 \end{lstlisting} &
52 \begin{lstlisting}
53 \mathOp{expression}
54 \end{lstlisting} & Evaluates mathematical expression. For example, \lstinline|\mathop{2+3}| will evaluate to 5. It supports usual operations like\lstinline| -, *, \, ^ | etcetera, apart from the standard functions. \\
55 \midrule
56 \begin{lstlisting}
57 \mathAbs
58 \end{lstlisting} &
59 \begin{lstlisting}
60 \mathAbs{number}
61 \end{lstlisting} & Gives the absolute value of a real number.\\
62 \midrule
63 \begin{lstlisting}
64 \mathAcos
65 \end{lstlisting} &
66 \begin{lstlisting}
67 \mathAcos{number}
68 \end{lstlisting} & Gives the arc cosine of a real number in radians.\\
69 \midrule
70 \begin{lstlisting}
71 \mathAsin
72 \end{lstlisting} &
73 \begin{lstlisting}
74 \mathAsin{number}
75 \end{lstlisting} & Gives the arc sine of a real number in radians.\\
76 \midrule
77 \begin{lstlisting}
78 \mathAtan
79 \end{lstlisting} &
80 \begin{lstlisting}
81 \mathAtan{number}
82 \end{lstlisting} & Gives the arc tangent of a real number in radians.\\
83 \midrule
84 \begin{lstlisting}
85 \mathCeil
86 \end{lstlisting} &
87 \begin{lstlisting}
88 \mathCeil{number}
89 \end{lstlisting} & Gives the smallest integer greater than or equal to a real number.\\
90 \midrule
91 \begin{lstlisting}
92 \mathCos
93 \end{lstlisting} &
94 \begin{lstlisting}
95 \mathCos{number}
96 \end{lstlisting} & Gives the cosine of a real number.Here the number is in radians.\\
97 \midrule
98 \begin{lstlisting}
99 \mathExp
100 \end{lstlisting} &
101 \begin{lstlisting}
102 \mathExp{x}
103 \end{lstlisting} & Calculates \(\mathrm{e}^x \). Here \(\mathrm{e} \) is the Euler number.\\
104 \midrule
105 \begin{lstlisting}
106 \mathFloor
107 \end{lstlisting} &
108 \begin{lstlisting}
109 \mathFloor{number}
110 \end{lstlisting} & Gives the smallest integer less than or equal to a real number.\\
111 \midrule
112 \begin{lstlisting}
113 \mathInf
114 \end{lstlisting} & \begin{lstlisting}
115 \mathInf
116 \end{lstlisting} & The infinity.\\
117 \midrule
118 \begin{lstlisting}
119 \mathLog
120 \end{lstlisting} &
121 \begin{lstlisting}
122 \mathLog{x}
123 \end{lstlisting} & Calculates \(\log x \). Here the logarithm is to the natural base \(\mathrm{e} \).\\
124 \midrule
125 \begin{lstlisting}
126 \mathMax
127 \end{lstlisting} &
128 \begin{lstlisting}
129 \mathMax\end{lstlisting}\{\(x_1,x_2,\ldots,x_n\)\}
130 & Gives the maximum of numbers \(x_1,x_2,\ldots,x_n\).\\
131 \midrule
132 \begin{lstlisting}
133 \mathMin
134 \end{lstlisting} &
135 \begin{lstlisting}
136 \mathMin\end{lstlisting}\{\(x_1,x_2,\ldots,x_n\)\}
137 & Gives the minimum of numbers \(x_1,x_2,\ldots,x_n\).\\
138 \midrule
139 \begin{lstlisting}
140 \mathPi
141 \end{lstlisting} &
142 \begin{lstlisting}
143 \mathPi
144 \end{lstlisting} & Gives the numeric value of the constant \(\pi \). \\
145 \midrule
146 \begin{lstlisting}
147 \mathRad
148 \end{lstlisting} &
149 \begin{lstlisting}
150 \mathRad{degree}
151 \end{lstlisting} & Converts degrees into radians. \\
152 \midrule
153 \begin{lstlisting}
154 \mathRandom
155 \end{lstlisting} & \begin{lstlisting}
156 \mathRandom
157 \end{lstlisting} & Generates a random number between \(0 \) and \(1 \). \\
158 \midrule
159 \begin{lstlisting}
160 \mathRound
161 \end{lstlisting} &
162 \begin{lstlisting}
163 \mathRound{number}{digits}
164 \end{lstlisting} & It rounds off a real number to a specified number of decimal places. \\
165 \midrule
166 \begin{lstlisting}
167 \mathSin
168 \end{lstlisting} &
169 \begin{lstlisting}
170 \mathSin{number}
171 \end{lstlisting} & Gives the sine of a real number. Here the number is in radians.\\
172 \midrule
173 \begin{lstlisting}
174 \mathSqrt
175 \end{lstlisting} &
176 \begin{lstlisting}
177 \mathSqrt{number}
178 \end{lstlisting} & Gives the non-negative square root of a non-negative real number.\\
179 \midrule
180 \begin{lstlisting}
181 \mathTan
182 \end{lstlisting} &
183 \begin{lstlisting}
184 \mathTan{number}
185 \end{lstlisting} & Gives the tangent of a real number. Here the number is in radians.\\
186 \midrule
187 \begin{lstlisting}
188 \complexRound
189 \end{lstlisting} &
190 \begin{lstlisting}
191 \complexRound{cx_num}{digits}
192 \end{lstlisting} & It rounds off a complex number to a specified number of decimal places.\\
193 \midrule
194 \begin{lstlisting}
195 \luaChkeqnum
196 \end{lstlisting} &
197 \begin{lstlisting}
198 \luaChkeqnum{x}{y}
199 \end{lstlisting} & It returns true if x = y, otherwise returns false.\\
200
201
202 \bottomrule \\
203 \caption{Operations in the luamaths package}
204 \label{tbl:opluamaths}
205 \end{longtable}
206 \end{center}
207 Numbers can also be entered in scientific notation. For example, the number \(1860000\) can be entered as \(1.86e6\), and the number \(0.000186\) can be entered as \(1.86e-4\).
208 \subsection*{Illustrations of commands in the luamaths package}
209 Table \ref{tbl:illluamaths} illustrates operations in the \verb|luamaths| package with examples.
210 \begin{center}
211 \begin{longtable}{lc}
212 \toprule
213 \multicolumn{1}{c}{\textcolor{blue}{LaTeX input}} &\multicolumn{1}{c}{\textcolor{blue}{Output}} \\
214 \toprule
215 \begin{lstlisting}
216 \mathOp{(3+3)^4-4}
217 \end{lstlisting} & \(\mathOp{(3+3)^4-4}\)\\
218 \midrule
219 \begin{lstlisting}
220 \mathAbs{-6.96}
221 \end{lstlisting} & \(\mathAbs{-6.96}\)\\
222 \midrule
223
224
225 \begin{lstlisting}
226 \mathFloor{-6.9}
227 \end{lstlisting} & \(\mathFloor{-6.9}\) \\
228 \midrule
229
230 \begin{lstlisting}
231 \mathFloor{9.9}
232 \end{lstlisting} & \(\mathFloor{9.9}\)\\
233 \midrule
234
235
236 \begin{lstlisting}
237 \mathCeil{-9.9}
238 \end{lstlisting} & \(\mathCeil{-9.9}\) \\
239 \midrule
240
241 \begin{lstlisting}
242 \mathCeil{6.3}
243 \end{lstlisting} &\(\mathCeil{6.3}\)\\
244 \midrule
245
246 \begin{lstlisting}
247 \mathRound{9.678884}{3}
248 \end{lstlisting} & \(\mathRound{9.678884}{3}\)\\
249 \midrule
250
251 \begin{lstlisting}
252 \mathMax{20,30,100}
253 \end{lstlisting} &\(\mathMax{20,30,100}\) \\
254 \midrule
255
256 \begin{lstlisting}
257 \mathMin{20,30,100}
258 \end{lstlisting} &\(\mathMin{20,30,100}\)\\
259 \midrule
260
261
262 \begin{lstlisting}
263 \mathPi
264 \end{lstlisting} & \(\mathPi\)\\
265 \midrule
266
267
268 \begin{lstlisting}
269 \mathRound{\mathPi}{6}
270 \end{lstlisting} & \(\mathRound{\mathPi}{6}\)\\
271 \midrule
272
273 \begin{lstlisting}
274 \mathInf
275 \end{lstlisting} & \(\mathInf\)\\
276 \midrule
277
278 \begin{lstlisting}
279 \mathOp{\mathInf/2}
280 \end{lstlisting} &\(\mathOp{\mathInf/2}\) \\
281 \midrule
282
283 \begin{lstlisting}
284 \mathSqrt{9}
285 \end{lstlisting} & \(\mathSqrt{9}\)\\
286 \midrule
287
288 \begin{lstlisting}
289 \mathLog{2}
290 \end{lstlisting} & \(\mathLog{2}\)\\
291 \midrule
292
293 \begin{lstlisting}
294 \mathExp{3}
295 \end{lstlisting} & \(\mathExp{3}\) \\
296 \midrule
297
298 \begin{lstlisting}
299 \mathRad{1800}
300 \end{lstlisting} & \(\mathRad{1800}\)\\
301 \midrule
302
303
304 \begin{lstlisting}
305 \mathSin{0}
306 \end{lstlisting} &\(\mathSin{0}\)\\
307 \midrule
308
309 \begin{lstlisting}
310 \mathCos{0}
311 \end{lstlisting} & \(\mathCos{0}\)\\
312 \midrule
313
314
315 \begin{lstlisting}
316 \mathTan{0}
317 \end{lstlisting} & \(\mathTan{0}\)\\
318 \midrule
319
320 \begin{lstlisting}
321 \mathAsin{0}
322 \end{lstlisting} & \(\mathAsin{0}\)\\
323 \midrule
324
325 \begin{lstlisting}
326 \mathAcos{0}
327 \end{lstlisting} & \(\mathAcos{0}\)\\
328 \midrule
329
330 \begin{lstlisting}
331 \mathAtan{0}
332 \end{lstlisting} & \(\mathAtan{0}\)\\
333 \midrule
334
335 \begin{lstlisting}
336 \mathRound{\mathOp{9+\mathSin{4}}}{6}
337 \end{lstlisting} & \(\mathRound{\mathOp{9+\mathSin{4}}}{6}\)\\
338 \midrule
339
340 \begin{lstlisting}
341 \complexRound{lcomplex(\mathPi,6)}{4}
342 \end{lstlisting} & \(\complexRound{lcomplex(\mathPi,6)}{4}\)\\
343 \midrule
344
345 \begin{lstlisting}
346 \luaChkeqnum{\mathCos{0}}{1}
347 \end{lstlisting} & \luaChkeqnum{\mathCos{0}}{1}\\
348 \bottomrule \\
349 \caption{Illustrations of commands in the luamaths package}
350 \label{tbl:illluamaths}
351 \end{longtable}
352 \end{center}
353
354 The package supports fractions; numerators and denominators must be integers. A fraction can be specified with the Lua function: \verb|lfrac|. This function has the syntax \verb|lfrac(n,d,mode)|: \(n\) is an integer and \(d\) is a non-zero integer. The mode is optional. It can be \verb|fracs| or \verb|nofracs|. The default mode is \verb|fracs|. If fractions are input, the package will display vectors and matrices in fraction mode wherever possible. The package does not attempt to convert floats into fractions. If fractions are expected, then the input should contain fractions. If fractions are input and answers are expected in numbers, the mode can be specified as \verb|nofracs|.
355
356 The Lua function \verb|lcomplex| defines the complex numbers. It has the syntax \verb|lcomplex(x,y)|, where \(x\) is a real part, and \(y\) is an imaginary part. \(x\) and \(y\) can also be fractions (numerators and denominators should be integers). The package has a command \verb|\imUnit| which provides typesetting for the imaginary unit. Its default value is \verb|\mathrm{i}|. It can be redefined. For example, one can redefine it as \verb|\renewcommand{\imUnit}{j}}|.
357
358 Table \ref{tbl:illluamaths2} illustrates calculations on fractions and complex numbers using the \verb|luamaths| package. The numbers \(x, y, c\) and \(d\) are defined using the following commands.
359 \begin{lstlisting}
360 \def\x{lfrac(1,2)}
361 \def\y{lfrac(-3,4)}
362 \def\c{lcomplex(lfrac(3,4),6)}
363 \def\d{lcomplex(3,-9)}
364 \end{lstlisting}
365
366 \subsection*{Some Illustrations}
367 \def\x{lfrac(1,2)}
368 \def\y{lfrac(-3,4)}
369 \def\c{lcomplex(lfrac(3,4),6)}
370 \def\d{lcomplex(3,-9)}
371 \begin{center}
372 \begin{longtable}{lc}
373 \toprule
374 \multicolumn{1}{c}{\textcolor{blue}{LaTeX input}} &\multicolumn{1}{c}{\textcolor{blue}{Output}} \\
375 \toprule
376 \begin{lstlisting}
377 \(x + y = \mathOp{\x + \y}\)
378 \end{lstlisting}
379 & \(x + y = \mathOp{\x + \y}\) \\ \hline
380
381 \begin{lstlisting}
382 \(x - y = \mathOp{\x - \y}\)
383 \end{lstlisting}
384 & \(x - y = \mathOp{\x - \y}\) \\ \hline
385
386 \begin{lstlisting}
387 \(xy = \mathOp{\x*\y}\)
388 \end{lstlisting}
389 & \(xy = \mathOp{\x*\y}\) \\ \hline
390
391 \begin{lstlisting}
392 \(\frac{x}{y} = \mathOp{\x / \y}\)
393 \end{lstlisting}
394 & \(\frac{x}{y} = \mathOp{\x / \y}\) \\ \hline
395
396 \begin{lstlisting}
397 \(c + d = \mathOp{\c + \d}\)
398 \end{lstlisting}
399 & \(c + d = \mathOp{\c + \d}\) \\ \hline
400
401 \begin{lstlisting}
402 \(c - d = \mathOp{\c - \d}\)
403 \end{lstlisting}
404 & \(c - d = \mathOp{\c - \d}\) \\ \hline
405
406 \begin{lstlisting}
407 \(cd = \mathOp{\c*\d}\)
408 \end{lstlisting}
409 & \(cd = \mathOp{\c*\d}\) \\ \hline
410
411 \begin{lstlisting}
412 \(\frac{c}{d} = \mathOp{\c / \d}\)
413 \end{lstlisting}
414 & \(\frac{c}{d} = \mathOp{\c / \d}\) \\ \hline
415
416 \begin{lstlisting}
417 \(x + c - y + d = \mathOp{\x + \c - \y + \d}\)
418 \end{lstlisting}
419 & \(x + c - y + d = \mathOp{\x + \c - \y + \d}\) \\
420
421 \bottomrule \\
422 \caption{Calculations on fractions and complex numbers}
423 \label{tbl:illluamaths2}
424 \end{longtable}
425 \end{center}
426 \printbibliography
427 \end{document}