Debian Codegraph @

unpacked/extensions/TeX/newcommand.js

raw
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3
4 /*************************************************************
5 *
6 * MathJax/extensions/TeX/newcommand.js
7 *
8 * Implements the \newcommand, \newenvironment and \def
9 * macros, and is loaded automatically when needed.
10 *
11 * ---------------------------------------------------------------------
12 *
13 * Copyright (c) 2009-2020 The MathJax Consortium
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 */
27
28 MathJax.Extension["TeX/newcommand"] = {
29 version: "2.7.9"
30 };
31
32 MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
33
34 var TEX = MathJax.InputJax.TeX;
35 var TEXDEF = TEX.Definitions;
36
37 TEXDEF.Add({
38 macros: {
39 newcommand: 'NewCommand',
40 renewcommand: 'NewCommand',
41 newenvironment: 'NewEnvironment',
42 renewenvironment: 'NewEnvironment',
43 def: 'MacroDef',
44 'let': 'Let'
45 }
46 },null,true);
47
48 TEX.Parse.Augment({
49
50 /*
51 * Implement \newcommand{\name}[n][default]{...}
52 */
53 NewCommand: function (name) {
54 var cs = this.trimSpaces(this.GetArgument(name)),
55 n = this.GetBrackets(name),
56 opt = this.GetBrackets(name),
57 def = this.GetArgument(name);
58 if (cs.charAt(0) === "\\") {cs = cs.substr(1)}
59 if (!cs.match(/^(.|[a-z]+)$/i)) {
60 TEX.Error(["IllegalControlSequenceName",
61 "Illegal control sequence name for %1",name]);
62 }
63 if (n) {
64 n = this.trimSpaces(n);
65 if (!n.match(/^[0-9]+$/)) {
66 TEX.Error(["IllegalParamNumber",
67 "Illegal number of parameters specified in %1",name]);
68 }
69 }
70 this.setDef(cs,['Macro',def,n,opt]);
71 },
72
73 /*
74 * Implement \newenvironment{name}[n][default]{begincmd}{endcmd}
75 */
76 NewEnvironment: function (name) {
77 var env = this.trimSpaces(this.GetArgument(name)),
78 n = this.GetBrackets(name),
79 opt = this.GetBrackets(name),
80 bdef = this.GetArgument(name),
81 edef = this.GetArgument(name);
82 if (n) {
83 n = this.trimSpaces(n);
84 if (!n.match(/^[0-9]+$/)) {
85 TEX.Error(["IllegalParamNumber",
86 "Illegal number of parameters specified in %1",name]);
87 }
88 }
89 this.setEnv(env,['BeginEnv',[null,'EndEnv'],bdef,edef,n,opt]);
90 },
91
92 /*
93 * Implement \def command
94 */
95 MacroDef: function (name) {
96 var cs = this.GetCSname(name),
97 params = this.GetTemplate(name,"\\"+cs),
98 def = this.GetArgument(name);
99 if (!(params instanceof Array)) {this.setDef(cs,['Macro',def,params])}
100 else {this.setDef(cs,['MacroWithTemplate',def].concat(params))}
101 },
102
103 /*
104 * Implements the \let command
105 */
106 Let: function (name) {
107 var cs = this.GetCSname(name), macro;
108 var c = this.GetNext(); if (c === "=") {this.i++; c = this.GetNext()}
109 //
110 // All \let commands create entries in the macros array, but we
111 // have to look in the various mathchar and delimiter arrays if
112 // the source isn't a macro already, and attach the data to a
113 // macro with the proper routine to process it.
114 //
115 // A command of the form \let\cs=char produces a macro equivalent
116 // to \def\cs{char}, which is as close as MathJax can get for this.
117 // So \let\bgroup={ is possible, but doesn't work as it does in TeX.
118 //
119 if (c === "\\") {
120 name = this.GetCSname(name);
121 macro = this.csFindMacro(name);
122 if (!macro) {
123 if (TEXDEF.mathchar0mi.hasOwnProperty(name)) {macro = ["csMathchar0mi",TEXDEF.mathchar0mi[name]]} else
124 if (TEXDEF.mathchar0mo.hasOwnProperty(name)) {macro = ["csMathchar0mo",TEXDEF.mathchar0mo[name]]} else
125 if (TEXDEF.mathchar7.hasOwnProperty(name)) {macro = ["csMathchar7",TEXDEF.mathchar7[name]]} else
126 if (TEXDEF.delimiter.hasOwnProperty("\\"+name)) {macro = ["csDelimiter",TEXDEF.delimiter["\\"+name]]} else
127 return;
128 }
129 } else {macro = ["Macro",c]; this.i++}
130 this.setDef(cs,macro);
131 },
132
133 /*
134 * Get a CS name or give an error
135 */
136 GetCSname: function (cmd) {
137 var c = this.GetNext();
138 if (c !== "\\") {
139 TEX.Error(["MissingCS",
140 "%1 must be followed by a control sequence", cmd])
141 }
142 var cs = this.trimSpaces(this.GetArgument(cmd));
143 return cs.substr(1);
144 },
145
146 /*
147 * Get a \def parameter template
148 */
149 GetTemplate: function (cmd,cs) {
150 var c, params = [], n = 0;
151 c = this.GetNext(); var i = this.i;
152 while (this.i < this.string.length) {
153 c = this.GetNext();
154 if (c === '#') {
155 if (i !== this.i) {params[n] = this.string.substr(i,this.i-i)}
156 c = this.string.charAt(++this.i);
157 if (!c.match(/^[1-9]$/)) {
158 TEX.Error(["CantUseHash2",
159 "Illegal use of # in template for %1",cs]);
160 }
161 if (parseInt(c) != ++n) {
162 TEX.Error(["SequentialParam",
163 "Parameters for %1 must be numbered sequentially",cs]);
164 }
165 i = this.i+1;
166 } else if (c === '{') {
167 if (i !== this.i) {params[n] = this.string.substr(i,this.i-i)}
168 if (params.length > 0) {return [n,params]} else {return n}
169 }
170 this.i++;
171 }
172 TEX.Error(["MissingReplacementString",
173 "Missing replacement string for definition of %1",cmd]);
174 },
175
176 /*
177 * Process a macro with a parameter template
178 */
179 MacroWithTemplate: function (name,text,n,params) {
180 if (n) {
181 var args = []; this.GetNext();
182 if (params[0] && !this.MatchParam(params[0])) {
183 TEX.Error(["MismatchUseDef",
184 "Use of %1 doesn't match its definition",name]);
185 }
186 for (var i = 0; i < n; i++) {args.push(this.GetParameter(name,params[i+1]))}
187 text = this.SubstituteArgs(args,text);
188 }
189 this.string = this.AddArgs(text,this.string.slice(this.i));
190 this.i = 0;
191 if (++this.macroCount > TEX.config.MAXMACROS) {
192 TEX.Error(["MaxMacroSub1",
193 "MathJax maximum macro substitution count exceeded; " +
194 "is there a recursive macro call?"]);
195 }
196 },
197
198 /*
199 * Process a user-defined environment
200 */
201 BeginEnv: function (begin,bdef,edef,n,def) {
202 if (n) {
203 var args = [];
204 if (def != null) {
205 var optional = this.GetBrackets("\\begin{"+name+"}");
206 args.push(optional == null ? def : optional);
207 }
208 for (var i = args.length; i < n; i++) {args.push(this.GetArgument("\\begin{"+name+"}"))}
209 bdef = this.SubstituteArgs(args,bdef);
210 edef = this.SubstituteArgs([],edef); // no args, but get errors for #n in edef
211 }
212 this.string = this.AddArgs(bdef,this.string.slice(this.i)); this.i = 0;
213 return begin;
214 },
215 EndEnv: function (begin,bdef,edef,n) {
216 var end = "\\end{\\end\\"+begin.name+"}"; // special version of \end for after edef
217 this.string = this.AddArgs(edef,end+this.string.slice(this.i)); this.i = 0;
218 return null;
219 },
220
221 /*
222 * Find a single parameter delimited by a trailing template
223 */
224 GetParameter: function (name,param) {
225 if (param == null) {return this.GetArgument(name)}
226 var i = this.i, j = 0, hasBraces = 0;
227 while (this.i < this.string.length) {
228 var c = this.string.charAt(this.i);
229 if (c === '{') {
230 if (this.i === i) {hasBraces = 1}
231 this.GetArgument(name); j = this.i - i;
232 } else if (this.MatchParam(param)) {
233 if (hasBraces) {i++; j -= 2}
234 return this.string.substr(i,j);
235 } else if (c === "\\") {
236 this.i++; j++; hasBraces = 0;
237 var match = this.string.substr(this.i).match(/[a-z]+|./i);
238 if (match) {this.i += match[0].length; j = this.i - i}
239 } else {
240 this.i++; j++; hasBraces = 0;
241 }
242 }
243 TEX.Error(["RunawayArgument","Runaway argument for %1?",name]);
244 },
245
246 /*
247 * Check if a template is at the current location.
248 * (The match must be exact, with no spacing differences. TeX is
249 * a little more forgiving than this about spaces after macro names)
250 */
251 MatchParam: function (param) {
252 if (this.string.substr(this.i,param.length) !== param) {return 0}
253 if (param.match(/\\[a-z]+$/i) &&
254 this.string.charAt(this.i+param.length).match(/[a-z]/i)) {return 0}
255 this.i += param.length;
256 return 1;
257 }
258
259 });
260
261 TEX.Environment = function (name) {
262 TEXDEF.environment[name] = ['BeginEnv',[null,'EndEnv']].concat([].slice.call(arguments,1));
263 TEXDEF.environment[name].isUser = true;
264 }
265
266 MathJax.Hub.Startup.signal.Post("TeX newcommand Ready");
267
268 });
269
270 MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/newcommand.js");