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