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