summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/appendix/luacas.dat
blob: 7f2efd0a10e9e3390a35318fa3385e781db2aa0b (plain)
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
% Copyright (c) ???
%
% 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/11/15 v1.0.1 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
    }%
  }%
}%

%%%%%%%%%%
%% 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
    }%
  }%
}

\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) {",longwhati  (sym),"};}")
      tex.print(sym:getthefancyshrub())
      tex.print("]")
      tex.print("}")
    }%
  }
}

\NewDocumentCommand{\whatis}{m}{%
  \luaexec{
    tex.sprint("{\\ttfamily",longwhatis(#1),"}")
  }%
}