Debian Codegraph @

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

raw
1 % Permission is granted to copy, distribute and/or modify this
2 % software under the terms of the LaTeX Project Public License
3 % (LPPL), version 1.3c or any later version.
4 %
5 % This software is provided 'as is', without warranty of any kind,
6 % either expressed or implied, including, but not limited to, the
7 % implied warranties of merchantability and fitness for a
8 % particular purpose.
9
10 \NeedsTeXFormat{LaTeX2e}
11 \ProvidesPackage{luacas}
12 [2023/05/26 v1.0.2 CAS written in Lua for LaTeX]
13
14 \RequirePackage{iftex}
15 \ifluatex
16 \RequirePackage{luacode}
17 \else
18 {\PackageError{luacas}
19 {Not running under LuaLaTeX}
20 {This package requires LuaLaTeX. Try compiling this document with\MessageBreak 'lualatex' instead of 'latex'. This is a fatal error; I'm aborting now.}%
21 }\stop
22 \fi
23
24 %Required Packages
25 \RequirePackage{xparse}
26 \RequirePackage{pgfkeys}
27 \RequirePackage{verbatim}
28 \RequirePackage{tikz}
29 \RequirePackage{xcolor}
30 \RequirePackage{mathtools}
31
32 %These files contain Lua code for parsing luacas output; they also initialize the CAS itself
33 \directlua{require('test.luacas-parser')
34 require('test.luacas-helper')
35 }
36
37 \NewDocumentEnvironment{CAS}%
38 {+b}%
39 {\luaexec{CASparse([[#1]])}}%
40 {}
41
42 \newcommand{\get}%
43 [2][true]%
44 {\directlua{disp(#2, #1)}}
45
46 \newcommand{\fetch}[1]{
47 \directlua{tex.print(tostring(#1))}
48 }
49
50 \NewDocumentCommand{\store}{m O{#1}}{
51 \expandafter\def\csname #2\endcsname{%
52 \directlua{
53 input = #1
54 if not input[1] then
55 tex.sprint{tostring(input)}
56 else
57 tex.sprint("{")
58 for _,entry in ipairs(input) do
59 tex.sprint(tostring(entry),",")
60 end
61 tex.sprint("}")
62 end
63 }%
64 }%
65 }%
66
67 \NewDocumentCommand{\yoink}{m O{#1}}{%
68 \expandafter\def\csname #2\endcsname{%
69 \directlua{
70 tex.print(tostring(#1))
71 }%
72 }%
73 }
74
75 %\newcommand{\eval}[1]{\luaexec{tex.print(parse('#1'):tolatex())}}
76
77 %%%%%%%%%%
78 %% Core %%
79 %%%%%%%%%%
80
81 %pretty print
82
83 \NewDocumentCommand{\print}{s m}{%
84 \IfBooleanTF{#1}{%
85 \directlua{
86 local sym = #2
87 if sym then
88 tex.print(sym:autosimplify():tolatex())
89 else
90 tex.print('nil')
91 end
92 }%
93 }{%
94 \directlua{
95 local sym = #2
96 if sym then
97 tex.print(sym:tolatex())
98 else
99 tex.print('nil')
100 end
101 }%
102 }%
103 }
104
105 %verbatim print
106
107 \NewDocumentCommand{\vprint}{s m}{%
108 \IfBooleanTF{#1}{%
109 \directlua{
110 local sym = #2
111 tex.sprint([[\unexpanded{\begin{verbatim}]]..tostring(sym)..[[\end{verbatim}}]])
112 }%
113 }{%
114 \directlua{
115 local sym = #2
116 tex.sprint([[\unexpanded{\begin{verbatim}]]..tostring(sym:autosimplify())..[[\end{verbatim}}]])
117 }%
118 }%
119 }
120
121 \NewDocumentCommand{\lprint}{m O{nil,nil}}{%
122 \luaexec{
123 local tbl = #1
124 local low,upp = #2
125 local tmp =0
126 if tbl[0] == nil then
127 tmp = 1
128 end
129 upp = upp or \#tbl
130 low = low or tmp
131 for i=low,upp do
132 tex.print(tbl[i]:tolatex())
133 if tbl[i+1] then
134 tex.print(",")
135 end
136 end
137 }
138 }
139
140 %prints the first level of an expression tree; for use within a tikzpicture environment
141
142 \NewDocumentCommand{\printshrub}{s m}{%
143 \IfBooleanTF{#1}{%
144 \directlua{
145 local sym = #2
146 sym = sym:autosimplify()
147 tex.print("\\node [label=90:",whatis(sym),"] {",nameof(sym),"}")
148 tex.print(sym:gettheshrub())
149 tex.print(";")
150 }%
151 }{%
152 \directlua{
153 local sym = #2
154 tex.print("\\node [label=90:",whatis(sym),"] {",nameof(sym),"}")
155 tex.print(sym:gettheshrub())
156 tex.print(";")
157 }%
158 }
159 }
160
161 %prints the full expression tree; for use within a tikzpicture environment
162
163 \NewDocumentCommand{\printtree}{s m}{%
164 \IfBooleanTF{#1}{%
165 \luaexec{
166 local sym = #2
167 sym = sym:autosimplify()
168 tex.print("\\node {",nameof(sym),"}")
169 tex.print(sym:getthetree())
170 tex.print(";")
171 }%
172 }{%
173 \luaexec{
174 local sym = #2
175 tex.print("\\node {",nameof(sym),"}")
176 tex.print(sym:getthetree())
177 tex.print(";")
178 }%
179 }
180 }
181
182 %parses an expression tree for use within the forest environment; result is stored in \forestresult
183
184 \NewDocumentCommand{\parseforest}{s m}{%
185 \IfBooleanTF{#1}{%
186 \luaexec{
187 local sym = #2
188 sym = sym:autosimplify()
189 tex.print("\\def\\forestresult{")
190 tex.print("[")
191 tex.print(nameof(sym))
192 tex.print(sym:gettheforest())
193 tex.print("]")
194 tex.print("}")
195 }%
196 }{%
197 \luaexec {
198 local sym = #2
199 tex.print("\\def\\forestresult{")
200 tex.print("[")
201 tex.print(nameof(sym))
202 tex.print(sym:gettheforest())
203 tex.print("]")
204 tex.print("}")
205 }%
206 }
207 }
208
209 \NewDocumentCommand{\parseshrub}{s m}{%
210 \IfBooleanTF{#1}{%
211 \luaexec{
212 local sym = #2
213 sym = sym:autosimplify()
214 tex.print("\\def\\shrubresult{")
215 tex.print("[")
216 tex.print(nameof(sym))
217 tex.print(", tikz+={\\node[anchor=south] at (.north) {test};}")
218 tex.print(sym:getthefancyshrub())
219 tex.print("]")
220 tex.print("}")
221 }%
222 }{%
223 \luaexec{
224 local sym = #2
225 tex.print("\\def\\shrubresult{")
226 tex.print("[")
227 tex.print(nameof(sym))
228 tex.print(", tikz+={\\node[anchor=south,font=\\ttfamily\\footnotesize,gray] at (.north) {",longwhatis(sym),"};}")
229 tex.print(sym:getthefancyshrub())
230 tex.print("]")
231 tex.print("}")
232 }%
233 }
234 }
235
236 \NewDocumentCommand{\whatis}{m}{%
237 \luaexec{
238 tex.sprint("{\\ttfamily",longwhatis(#1),"}")
239 }%
240 }
241
242 \NewDocumentCommand{\freeof}{s m m}{%
243 \IfBooleanTF{#1}{%
244 \luaexec{
245 local sym1 = #2
246 local sym2 = #3
247 if sym1:freeof(sym2) then
248 tex.print(1)
249 else
250 tex.print(0)
251 end
252 }
253 }{%
254 \luaexec{
255 local sym1 = #2
256 local sym2 = #3
257 sym1 = sym1:autosimplify()
258 sym2 = sym2:autosimplify()
259 if sym1:freeof(sym2) then
260 tex.print(1)
261 else
262 tex.print(0)
263 end
264 }%
265 }%
266 }
267
268 \NewDocumentCommand{\isatomic}{s m}{%
269 \IfBooleanTF{#1}{
270 \luaexec{
271 local sym = #2
272 if sym:isatomic() then
273 tex.print(1)
274 else
275 tex.print(0)
276 end
277 }
278 }{%
279 \luaexec{
280 local sym = #2
281 sym = sym:autosimplify()
282 if sym:isatomic() then
283 tex.print(1)
284 else
285 tex.print(0)
286 end
287 }%
288 }%
289 }
290
291 \NewDocumentCommand{\isconstant}{s m}{%
292 \IfBooleanTF{#1}{%
293 \luaexec{
294 local sym = #2
295 if sym:isconstant() then
296 tex.print(1)
297 else
298 tex.print(0)
299 end
300 }%
301 }{%
302 \luaexec{%
303 local sym = #2
304 sym = sym:autosimplify()
305 if sym:isconstant() then
306 tex.print(1)
307 else
308 tex.print(0)
309 end
310 }%
311 }%
312 }