\def support (and \gdef and \global\def) (#1348)

* Nested environments of macro definitions

* Rename environment -> namespace

* \def support

* Clean up \df@tag at beginning

* \global\def support

* Fix \global via new setMacro helper

* Fix caching behavior and build array on top of it

Also avoid double lookup of macros

* Add tests

* Add argument tests

* Remove global pointer

* Note about macros object being modified

* add __defineMacro

* Add \global\def test

* More \global tests

* Constant-time lookup

Rewrite to use an "undo" stack similar to TeX, so get and set are
constant-time operations.  get() still has to check two objects: one with all
current settings, and then the built-ins.  Local set() sets the current value
and (when appropriate) adds an undo operation to the undo stack.  Global set()
still takes time linear in the number of groups, possibly changing the undo
operation at every level.

`Namespace` now refers to a space of things like macros or lengths.

* Add \def to-dos

* Put optional arguments in their own group

* Rename `pushNamespace` -> `beginGroup`

* Wrap each expression in a group namespace

* Add comments
This commit is contained in:
Erik Demaine
2018-05-28 15:58:57 -04:00
committed by Kevin Barabash
parent 3ec752f5f1
commit acccce801d
11 changed files with 273 additions and 56 deletions

View File

@@ -14,7 +14,8 @@ function init() {
input.addEventListener("input", reprocess, false);
permalink.addEventListener("click", setSearch);
const options = {displayMode: true, throwOnError: false, macros: {}};
const options = {displayMode: true, throwOnError: false};
const macros = {};
const query = queryString.parse(window.location.search);
if (query.text) {
@@ -65,7 +66,7 @@ function init() {
// `c=expansion`.
Object.getOwnPropertyNames(query).forEach((key) => {
if (key.match(/^\\|^[^]$/)) {
options.macros[key] = query[key];
macros[key] = query[key];
}
});
@@ -78,6 +79,8 @@ function init() {
}
function reprocess() {
// Ignore changes to global macros caused by the expression
options.macros = Object.assign({}, macros);
try {
katex.render(input.value, math, options);
} catch (e) {