Debian Codegraph @

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

raw
1 % The luaplot package.
2 % Version 1.5.
3 % Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.
4 %Authors: Chetan Shirore and Ajit Kumar
5
6 \ProvidesPackage{luaplot}[1.5]
7 \RequirePackage{xkeyval}
8 \RequirePackage{luacode}
9 \RequirePackage{tikz}
10 \RequirePackage{luamplib}
11 \begin{luacode}
12 -- compatibility of number formats with the MetaPost system.
13 function checknumber(x)
14 if string.find(x, "e") then
15 return string.format("%.12f",x)
16 else
17 return x
18 end
19 end
20 -- Determining plot points with Lua for the MetaPost system.
21 function luapath (f,a,b,n,myclr)
22 local mytbl ={}
23 local s = ""
24 local finalstr = ""
25 local y = f(a)
26 local mycnt = 1
27 if y == y and not (y==math.huge or y==-math.huge ) then
28 s = s.."(" .. a .."," .. f(a) ..")"
29 end
30 for i = 1, n do
31 local y = f((b - a) / n * i + a)
32 if y == y and not (y==math.huge or y==-math.huge ) then
33 if s~='' then
34 s= s .. "-- (" ..checknumber(((b - a) / n * i + a)) ..",".. checknumber(y)..")"
35 else
36 s= s .. "(" ..checknumber(((b - a) / n * i + a)) ..",".. checknumber(y)..")"
37 end
38 mytbl[mycnt] = s
39 else
40 if s~='' then
41 mytbl[mycnt] = s
42 mycnt = mycnt + 1
43 s = ''
44 end
45 end
46
47 end
48 for j=1, #mytbl do
49 finalstr = finalstr .."gdraw " ..tostring(mytbl[j]).."withcolor " .. myclr..";"
50 end
51 return finalstr
52 end
53 -- Determining plot points with Lua for the tikz package.
54 function luatikzpath (f,a,b,n)
55 local s = ""
56 s = s.."(" .. a .."," .. f(a) ..")"
57 for i = 1, n do
58 s= s .. " -- (" ..((b - a) / n * i + a) ..",".. f((b - a) / n * i + a)..")"
59 end
60 return s
61 end
62 \end{luacode}
63
64 % ========= KEY DEFINITIONS =========
65 \define@key{someop}{xmin}{\def\mop@one{#1}}%
66 \define@key{someop}{xmax}{\def\mop@two{#1}}%
67 \define@key{someop}{ymin}{\def\mop@three{#1}}%
68 \define@key{someop}{ymax}{\def\mop@four{#1}}%
69 \define@key{someop}{plotpts}{\def\mop@five{#1}}%
70 \define@key{someop}{hor}{\def\mop@six{#1}}%
71 \define@key{someop}{ver}{\def\mop@seven{#1}}%
72 \define@key{someop}{clr}{\def\mop@eight{#1}}%
73 \define@key{someop}{plotsty}{\def\mop@nine{#1}}%
74 \define@key{someop}{plotoptions}{\def\mop@ten{#1}}%
75 % ========= KEY DEFAULTS =========
76 \setkeys{someop}{xmin=0}%
77 \setkeys{someop}{xmax=5}%
78 \setkeys{someop}{ymin='whatever'}%
79 \setkeys{someop}{ymax='whatever'}%
80 \setkeys{someop}{plotpts=100}%
81 \setkeys{someop}{hor='5cm'}%
82 \setkeys{someop}{ver='3cm'}%
83 \setkeys{someop}{clr='black'}%
84 \setkeys{someop}{plotsty=''}%
85 \setkeys{someop}{plotoptions=';'}%
86 % ========= Defining Command =========
87 \newcommand{\luaplot}[2][]{{%
88 \setkeys{someop}{#1}%
89 \directlua{%
90 local begincode = [[
91 \string\begin{mplibcode}
92 input graph ;
93 beginfig(0)
94 draw begingraph(runscript("mp.print(\mop@six)"),runscript("mp.print(\mop@seven)"));
95 setrange(runscript("mp.print(\mop@one)"), runscript("mp.print(\mop@three)"),runscript("mp.print(\mop@two)"),runscript("mp.print(\mop@four)"));
96 color clr;
97 ]]
98 local endcode = [[
99 endgraph;
100 endfig;
101 \string\end{mplibcode}
102 ]]
103 local expr={}
104 local countexp = 1
105 for subexp in string.gmatch(\luastring{#2}, '([^,]+)') do
106 tempsubexp = "("..subexp..")"
107 local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)()
108 expr[countexp] = f
109 countexp = countexp + 1
110 end
111 local colortbl={}
112 local countclr = 1
113 for funcolor in string.gmatch(\mop@eight, '[^;]*') do
114 if funcolor == nil or funcolor=='' then funcolor='black' end
115 colortbl[countclr] = funcolor
116 countclr = countclr + 1
117 end
118 local plotstytbl={}
119 local countplotsty = 1
120 for funplotsty in string.gmatch(\mop@nine, '[^,]*') do
121 plotstytbl[countplotsty] = funplotsty
122 countplotsty = countplotsty + 1
123 end
124 tex.print(begincode)
125 for k=1, countexp-1 do
126 local clr = colortbl[k] or 'black';
127 local w = plotstytbl[k] or '';
128 mainstr="drawoptions("..w..");"..luapath(expr[k],\mop@one,\mop@two,\mop@five, clr)..'drawoptions();'
129 tex.print(mainstr)
130 end
131 tex.print(tostring(\mop@ten))%
132 tex.print(endcode)%
133 }%
134 }%
135 }%
136
137 \def\luatikzpath#1#2#3#4{%
138 \directlua{%
139 local expr={}
140 local countexp = 1
141 tempsubexp = "("..\luastring{#1}..")"
142 local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)()
143 mainstr = luatikzpath(f,#2,#3,#4)
144 tex.print(mainstr)
145 }
146 }
147
148 \endinput