sample-dynamic-3g.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Dynamic Preview of Textarea with MathJax Content</title>
  5. <!-- Copyright (c) 2012-2018 The MathJax Consortium -->
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  8. <style>
  9. .changed { color: red }
  10. </style>
  11. <script type="text/x-mathjax-config">
  12. MathJax.Hub.Config({
  13. TeX: {
  14. equationNumbers: {autoNumber: "AMS"},
  15. extensions: ["begingroup.js"],
  16. noErrors: {disabled: true}
  17. },
  18. showProcessingMessages: false,
  19. tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
  20. });
  21. //MathJax.Hub.signal.Interest(function (message) {console.log(message,MathJax.Hub.cancelTypeset)});
  22. </script>
  23. <script type="text/javascript" src="../MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  24. <script>
  25. var Preview = {
  26. typeset: null, // the typeset preview area (filled in by Init below)
  27. preview: null, // the untypeset preview (filled in by Init below)
  28. buffer: null, // the new preview to be typeset (filled in by Init below)
  29. par: [], // paragraph-specific data
  30. refs: [], // undefined references needing to be reprocessed
  31. updateNeeded: 0, // number of paragraphs needing update
  32. oldtext: '', // used to see if an update is needed
  33. pending: false, // true when a restart is in the MathJax queue
  34. highlight: true, // color setions that are changed
  35. noflicker: false, // reduce flicker
  36. flickertime: 1500, // processUpdateTime value used for noflicker
  37. classDelay: 400, // how long to leave changed paragraphs colored
  38. ctimeout: null, // timeout for changed style remover
  39. labelDelay: 1250, // how long to wait before reprocessing for label changes
  40. ltimeout: null, // timeout for changed labels
  41. keytimes: [], // tracks the times between keypresses
  42. keyrate: 100, // the average of the keytimes (default value)
  43. keyn: 0, // key index to replace next
  44. keysize: 10, // use this many keypresses
  45. //
  46. // Get the preview and buffer DIV's
  47. //
  48. Init: function () {
  49. this.typeset = document.getElementById("MathPreview");
  50. this.buffer = document.createElement("div");
  51. this.preview = document.createElement("div");
  52. for (var i = 0; i < this.keysize; i++) {this.keytimes[i] = this.keyrate}
  53. },
  54. //
  55. // This gets called when a key is pressed in the textarea.
  56. //
  57. Update: function (up) {
  58. if (up) {
  59. //
  60. // Determine the typing speed as a rolling average of the last few keystrokes
  61. //
  62. var time = new Date().getTime();
  63. if (this.lasttime) {
  64. var delta = time - this.lasttime;
  65. if (delta < 4*this.keyrate) {
  66. this.keyrate = (this.keysize*this.keyrate+delta-this.keytimes[this.keyn])/this.keysize;
  67. this.keytimes[this.keyn++] = delta;
  68. if (this.keyn === this.keysize) {this.keyn = 0}
  69. }
  70. }
  71. this.lasttime = time;
  72. }
  73. //
  74. // Get the new text and see if it has changed
  75. //
  76. var text = document.getElementById("MathInput").value;
  77. text = text.replace(/^\s+/,'').replace(/\s+$/,'').replace(/\r\n?/g,"\n");
  78. if (text !== this.oldtext) {
  79. if (this.rtimeout) {clearTimeout(this.rtimeout)}
  80. //
  81. // Save the text and if we don't already have a pending Restart
  82. // clear the timeouts for updating the colors and other updates
  83. // then set the update time based on the key rate (so it can be
  84. // interrupted better), cancel the current typesetting, if any,
  85. // and start a new typesetting run after a short delay (to allow
  86. // the editor to process more keystrokes).
  87. //
  88. this.oldtext = text;
  89. if (!this.pending) {
  90. this.pending = true;
  91. if (this.ctimeout) {clearTimeout(this.ctimeout); this.ctimeout = null}
  92. if (this.ltimeout) {clearTimeout(this.ltimeout); this.ltimeout = null}
  93. MathJax.Hub.processUpdateTime = (this.noflicker ? this.flickertime : this.keyrate / 2);
  94. MathJax.Hub.Cancel(); this.cancelled = true;
  95. // MathJax.Hub.Queue(
  96. // // allow a little time for additional typing
  97. //// ["Delay",MathJax.Callback,Math.min(100,Math.floor(this.keyrate/2)+1)],
  98. // ["Delay",MathJax.Callback,150],
  99. // ["Restart",this]
  100. // );
  101. }
  102. this.rtimeout = setTimeout(function () {
  103. this.rtimeout = null;
  104. MathJax.Hub.Queue(["Restart",Preview]);
  105. },Math.floor(this.keyrate*1.25))
  106. }
  107. },
  108. //
  109. // To restart typesetting, touch up the text string
  110. // and save it in the buffer. Then check if an update is
  111. // needed, and if so, queue the update.
  112. //
  113. Restart: function () {
  114. this.pending = false;
  115. var text = this.oldtext.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  116. var text = text.replace(/\n\n+/g,"<p>");
  117. this.buffer.innerHTML = text;
  118. var update = this.CompareBuffers();
  119. if (update.needed) {
  120. MathJax.Hub.Queue(
  121. ["CopyChanges",this,update],
  122. ["PreTypeset",this,update],
  123. ["Typeset",this,update],
  124. function () {if (MathJax.Hub.cancelTypeset) {console.log("cancel")}},
  125. ["PostTypeset",this,update]
  126. );
  127. }
  128. },
  129. CompareBuffers: function () {
  130. var b1 = this.buffer.childNodes,
  131. b2 = this.preview.childNodes,
  132. i, m1 = b1.length, m2 = b2.length, m = Math.min(m1,m2);
  133. //
  134. // Make sure all top-level elements are containers
  135. //
  136. for (i = 0; i < m1; i++) {
  137. var node = b1[i];
  138. if (typeof(node.innerHTML) === "undefined") {
  139. this.buffer.replaceChild(document.createElement("span"),node);
  140. b1[i].appendChild(node);
  141. }
  142. }
  143. //
  144. // Find first non-matching element, if any,
  145. // and the last non-matching element
  146. //
  147. for (i = 0; i < m; i++) {if (b1[i].innerHTML !== b2[i].innerHTML) break}
  148. if (i === m && m1 === m2) {return {needed: false}}
  149. while (m1 > i && m2 > i) {if (b1[--m1].innerHTML !== b2[--m2].innerHTML) break}
  150. return {needed:true, start:i, end1:m1, end2:m2};
  151. },
  152. CopyChanges: function (update) {
  153. var i = update.start, m1 = update.end1, m2 = update.end2, j, par;
  154. var b1 = this.buffer.childNodes,
  155. b2 = this.typeset.childNodes;
  156. update.indices = []; update.nodes = []; update.replace = true;
  157. //
  158. // Remove differing elements from typeset copy
  159. // Copy any predeeding old replacements from an interrupted typeset
  160. // Add in the new (untypeset) elements
  161. // Copy any following old replacements from an interrupted typeset
  162. //
  163. this.recordOldData(this.par.splice(i,m2+1-i),i); var tail = b2[m2+1];
  164. if (this.replace && i > this.start) {
  165. j = update.start = this.start;
  166. this.copyPrevious(j,i,i,update);
  167. }
  168. while (m2 >= i && b2[i]) {this.typeset.removeChild(b2[i]); m2--}
  169. while (i <= m1 && b1[i]) {
  170. this.par.splice(i,0,{number:0, labels:[], defs:[], refs:[], replaced:true, update:true});
  171. var node = b1[i].cloneNode(true); update.nodes.push(node);
  172. update.indices.push(i++); this.updateNeeded++;
  173. if (tail) {this.typeset.insertBefore(node,tail)} else {this.typeset.appendChild(node)}
  174. if (this.highlight) {this.addChanged(node)}
  175. }
  176. if (this.replace && i <= this.end) {
  177. update.end = this.end;
  178. this.copyPrevious(i,this.end,update.start,update);
  179. } else {update.end = i-1};
  180. //
  181. // Swap buffers and set up the new buffer for the next change
  182. //
  183. this.preview = this.buffer; this.buffer = document.createElement("div");
  184. },
  185. //
  186. // Copy data for replaced paragraphs from an interrupted
  187. // typesetting run. Replace the paragraph by their originals
  188. // to avoid any problems with partially completed math.
  189. //
  190. copyPrevious: function (i,end,start,update) {
  191. while (i <= end) {
  192. par = this.par[i];
  193. if (par.replaced && par.update) {
  194. this.typeset.replaceChild(this.preview.childNodes[i].cloneNode(true),
  195. this.typeset.childNodes[i]);
  196. update.nodes.push(this.typeset.childNodes[i]);
  197. update.indices.push(i);
  198. }
  199. this.recordOldData([par],i,start); par.replaced = true; i++;
  200. }
  201. },
  202. //
  203. // Save the labels, defintions, and equation numbers
  204. // for the removed paragraphs
  205. //
  206. recordOldData: function (par,p,q) {
  207. var AMS = MathJax.Extension["TeX/AMSmath"];
  208. var labels = [], defs = [], number = 0;
  209. for (var i = 0, m = par.length; i < m; i++) {
  210. if (!par.replace) {
  211. number += par[i].number;
  212. defs.push(par[i].defs.all);
  213. for (var j = 0, n = par[i].labels.length; j < n; j++) {
  214. delete AMS.labels[par[i].labels[j].split(/=/)[0]];
  215. labels.push(par[i].labels[j]);
  216. }
  217. }
  218. }
  219. labels = labels.join(''); defs = defs.join('');
  220. if (this.replace) {
  221. this.oldnumber += number;
  222. if (p < q) {
  223. this.oldlabels = labels + this.oldlabels;
  224. this.olddefs = defs + this.olddefs;
  225. } else {this.oldlabels += labels; this.olddefs += defs}
  226. } else {this.oldlabels = labels; this.olddefs = defs; this.oldnumber = number}
  227. },
  228. //
  229. // Save the data needed for the incremental typesetting
  230. // and reset the TeX begingroup stack
  231. //
  232. PreTypeset: function (update) {
  233. var TEX = MathJax.InputJax.TeX;
  234. //
  235. // Set the state variables
  236. //
  237. this.incremental = true; this.cancelled = false;
  238. this.i = this.j = 0; this.eqNum = 0;
  239. this.update = update.indices; this.replace = update.replace;
  240. if (this.replace) {this.start = update.start; this.end = update.end}
  241. //
  242. // Pop any left over \begingroups and push a new one
  243. // Reset the equation numbers (but not labels)
  244. //
  245. while (TEX.rootStack.top > 1) {TEX.rootStack.stack.pop(); TEX.rootStack.top--}
  246. TEX.rootStack.Push(TEX.nsStack.nsFrame());
  247. MathJax.Hub.cancelTypeset = false; // won't need to do this in the future
  248. },
  249. //
  250. // Typeset the given nodes
  251. //
  252. Typeset: function (update) {
  253. return MathJax.Hub.Typeset(update.nodes,{});
  254. },
  255. BeginMath: function () {
  256. //
  257. // Save the start time for this paragraph
  258. //
  259. this.time = new Date().getTime();
  260. },
  261. BeginInput: function () {
  262. //
  263. // Skip any paragraphs that aren't being updated, and
  264. // update the equation numbers and macro definitions
  265. // accordingly
  266. //
  267. var TEX = MathJax.InputJax.TeX, par;
  268. while (this.i < this.update[this.j]) {
  269. par = this.par[this.i++];
  270. this.eqNum += par.number;
  271. for (i = 0, m = par.defs.length; i < m; i++) {
  272. TEX.rootStack.Def.apply(TEX.rootStack,par.defs[i]);
  273. }
  274. }
  275. TEX.resetEquationNumbers(this.eqNum,true);
  276. //
  277. // Store new macro and label definitions here
  278. //
  279. par = this.par[this.i];
  280. if (par) {
  281. if (!par.replaced) {par.olddefs = par.defs.all; par.oldlabels = par.labels.join('')}
  282. par.defs = []; par.defs.all = [];
  283. par.labels = [];
  284. }
  285. },
  286. TeXFilter: function (data) {
  287. //
  288. // Get any new labels for this paragraph
  289. //
  290. var AMS = MathJax.Extension["TeX/AMSmath"];
  291. var labels = this.par[this.i].labels;
  292. for (var id in AMS.eqlabels) {if (AMS.eqlabels.hasOwnProperty(id)) {
  293. labels.push(id+"="+AMS.eqlabels[id])
  294. }}
  295. },
  296. TeXDef: function (def) {
  297. //
  298. // Record the definition
  299. //
  300. var defs = this.par[this.i].defs;
  301. defs.push(def); defs.all.push(def[0]+"{"+def[1]+"}");
  302. },
  303. EndInput: function () {
  304. //
  305. // Record the undefined references,
  306. // the new definitions, and the equation number
  307. // for this paragraph
  308. //
  309. var AMS = MathJax.Extension["TeX/AMSmath"];
  310. var par = this.par[this.i];
  311. if (par) {
  312. par.refs = AMS.refs; AMS.refs = [];
  313. par.defs.all = par.defs.all.join("");
  314. par.number = AMS.startNumber - this.eqNum;
  315. this.eqNum = AMS.startNumber;
  316. //
  317. // If this is an update to an existing paragraph,
  318. // check if it should force a refresh later
  319. //
  320. if (!par.replaced) {
  321. if (this.par.length !== this.update.length &&
  322. par.labels.join('') !== par.oldlabels) {this.refreshAll = true}
  323. if (this.update.length-this.j !== this.par.length-this.i &&
  324. par.defs.all !== par.olddefs) {this.refreshRest = true}
  325. delete par.olddefs; delete par.oldlabels;
  326. }
  327. }
  328. },
  329. EndMath: function () {
  330. //
  331. // Record the tie it took for this paragraph
  332. // and go on to the next one.
  333. //
  334. var par = this.par[this.i];
  335. if (par) {
  336. var time = new Date().getTime();
  337. par.time = time - this.time; this.time = time;
  338. delete par.update; this.updateNeeded--;
  339. if (this.refreshRest || this.refreshAll) {MathJax.Hub.Cancel()}
  340. }
  341. this.j++; this.i++;
  342. },
  343. //
  344. // Finish an incremental typesetting pass by checking if
  345. // there are any undefined references that need updating,
  346. // then checking if labels, defs, or equation numbers
  347. // have changed, which would require updating additional
  348. // paragraphs.
  349. //
  350. PostTypeset: function (update) {
  351. var incremental = this.incremental; this.incremental = false;
  352. if (MathJax.Hub.cancelTypeset && this.cancelled) return;
  353. //
  354. // Check if there are undefined references that might have been
  355. // defined in this update, and reprocess if so.
  356. //
  357. for (var i = 0, m = this.update.length; i < m; i++) {
  358. var par = this.par[this.update[i]];
  359. if (par.refs.length) {this.refs = this.refs.concat(par.refs); par.refs = []}
  360. }
  361. if (incremental && this.refs.length && !this.refreshAll && !this.refreshRest) {
  362. var queue = MathJax.Callback.Queue(
  363. ["Reprocess",MathJax.Hub,this.refs,{}],
  364. function () {if (!MathJax.Hub.cancelTypeset) {this.refs = []}}
  365. );
  366. return queue.Push(["PostTypeset",this,update]);
  367. }
  368. //
  369. // Set the timer for the color removal
  370. //
  371. if (this.highlight && !this.ctimeout)
  372. {this.ctimeout = setTimeout(this.Unmark,this.classDelay)}
  373. //
  374. //
  375. var labels = [], defs = [], number = 0;
  376. if (this.replace) {
  377. for (i = 0, m = this.update.length; i < m; i++) {
  378. var par = this.par[this.update[i]];
  379. if (par.replaced) {
  380. labels = labels.concat(par.labels.join(''));
  381. defs = defs.concat(par.defs.all);
  382. number += par.number;
  383. delete par.replaced;
  384. }
  385. }
  386. this.replace = false;
  387. this.loopCount = 0; // avoid any possibility of infinite loop
  388. // (shouldn't happen anyway, but I'm paranoid)
  389. //console.log("--------");
  390. //console.log(this.updateNeeded);
  391. //console.log(this.oldlabels,labels);
  392. //console.log(this.olddefs,defs);
  393. //console.log(this.oldnumber,number);
  394. }
  395. if (update.nodes.length !== this.preview.childNodes.length) {
  396. if (this.refreshAll || labels.join('') !== this.oldlabels) {
  397. this.MarkForUpdate(0); this.refreshAll = this.refreshRest = false; this.refs = [];
  398. } else if (this.refreshRest || defs.join('') !== this.olddefs) {
  399. this.MarkForUpdate(this.i); this.refreshRest = false;
  400. } else if (number !== this.oldnumber) {
  401. this.MarkForUpdate(this.i,true);
  402. }
  403. if (this.updateNeeded && this.loopCount++ < 10) {
  404. var delay = Math.min(this.labelDelay,3*this.keyRate);
  405. if (this.getTime() < 2*this.keyrate) {this.Refresh()}
  406. else {this.ltimeout = setTimeout(this.Refresh,delay)}
  407. }
  408. }
  409. },
  410. //
  411. // Mark the paragraphs from i onward for future updating
  412. //
  413. MarkForUpdate: function (i,numbered) {
  414. for (var m = this.par.length; i < m; i++) {
  415. if (!this.par[i].update && (!numbered || this.par[i].number)) {
  416. this.par[i].update = true; this.updateNeeded++;
  417. }
  418. }
  419. },
  420. //
  421. // Get the list of nodes needing update,
  422. // and their indices.
  423. //
  424. GetMarked: function () {
  425. var AMS = MathJax.Extension["TeX/AMSmath"];
  426. var nodes = [], indices = [], par = this.par;
  427. for (var i = 0, m = par.length; i < m; i++) {
  428. if (par[i].update) {
  429. var node = this.typeset.childNodes[i];
  430. nodes.push(node); indices.push(i);
  431. this.addChanged(node);
  432. for (var j = 0, n = par[i].labels.length; j < n; j++) {
  433. delete AMS.labels[par[i].labels[j].split(/=/)[0]];
  434. }
  435. }
  436. }
  437. return {nodes:nodes, indices:indices, replace:false};
  438. },
  439. //
  440. // Remove the colors from the updated paragraphs
  441. //
  442. Unmark: function () {
  443. Preview.ctimeout = null; var nodes = Preview.typeset.childNodes;
  444. for (var i = 0, m = nodes.length; i < m; i++) {Preview.removeChanged(nodes[i])}
  445. },
  446. //
  447. // Update the paragraphs that have pending updates
  448. //
  449. Refresh: function (method) {
  450. if (!method) {method = "Reprocess"}
  451. var update = Preview.GetMarked();
  452. Preview.ltimeout = null; this.replace = false;
  453. this.oldlabels = this.olddefs = ""; this.oldnumber = 0;
  454. if (update.nodes.length) {
  455. MathJax.Hub.Queue(
  456. ["PreTypeset",Preview,update],
  457. [method,MathJax.Hub,update.nodes,{}],
  458. ["PostTypeset",Preview,update]
  459. );
  460. }
  461. },
  462. //
  463. // Force a complete refresh of the preview
  464. //
  465. Redraw: function () {
  466. this.typeset.innerHTML = this.preview.innerHTML;
  467. this.MarkForUpdate(0); this.updateNeeded = this.par.length;
  468. this.Refresh("Typeset");
  469. },
  470. //
  471. // Find out how long updating the currently pending updates
  472. // would be likely to take.
  473. //
  474. getTime: function () {
  475. var time = 0, i = 0, m = this.par.length;
  476. while (i < m) {if (this.par[i].update) {time += this.par[i].time}; i++}
  477. return time;
  478. },
  479. //
  480. // Add the changed class to an element
  481. //
  482. addChanged: function (node) {
  483. if (!(node.className && node.className.toString().match(/(^| )changed( |$)/))) {
  484. if (node.className != "") {node.className += " changed"} else {node.className = "changed"}
  485. }
  486. },
  487. //
  488. // Remove the "changed" class from an element (leaving all other classes)
  489. //
  490. removeChanged: function (node) {
  491. if (node.className) {
  492. node.className = node.className.toString()
  493. .replace(/(^|\s+)changed(\s|$)/,"$2")
  494. .replace(/^\s+/,"");
  495. }
  496. }
  497. };
  498. //
  499. // Hook into the math signals
  500. //
  501. MathJax.Hub.Register.MessageHook("Begin Math",function () {
  502. if (Preview.incremental) {Preview.BeginMath()}
  503. });
  504. MathJax.Hub.Register.MessageHook("End Math",function () {
  505. if (Preview.incremental) {Preview.EndMath()}
  506. });
  507. MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  508. MathJax.InputJax.TeX.postfilterHooks.Add(function (data) {
  509. if (Preview.incremental) {Preview.TeXFilter(data)}
  510. });
  511. });
  512. MathJax.Hub.Register.MessageHook("Begin Math Input",function () {
  513. if (Preview.incremental) {Preview.BeginInput()}
  514. });
  515. MathJax.Hub.Register.MessageHook("End Math Input",function () {
  516. if (Preview.incremental) {Preview.EndInput()}
  517. },5); // priority = 5 to make sure it is before AMS.eqlabels are removed.
  518. //
  519. // Hook into the definition routines to record
  520. // new definitions that occur.
  521. //
  522. MathJax.Hub.Register.StartupHook("TeX begingroup Ready",function () {
  523. var STACK = MathJax.InputJax.TeX.eqnStack;
  524. var DEF = STACK.Def;
  525. STACK.Def = function () {
  526. if (Preview.incremental) {Preview.TeXDef([].slice.call(arguments,0))}
  527. DEF.apply(this,arguments);
  528. }
  529. //
  530. // Temporary hack to fix typo in begingroup.js
  531. //
  532. MathJax.InputJax.TeX.rootStack.stack[0].environments =
  533. MathJax.InputJax.TeX.Definitions.environment;
  534. });
  535. //
  536. // Set up MathJax to allow canceling of typesetting, if it
  537. // doesn't already have that.
  538. //
  539. (function () {
  540. var HUB = MathJax.Hub;
  541. HUB.Queue(function () {
  542. ready = true;
  543. HUB.processUpdateTime = 50; // reduce update time so that we can cancel easier
  544. HUB.Config({
  545. "HTML-CSS": {EqnChunk: 10, EqnChunkFactor: 1}, // reduce chunk for more frequent updates
  546. SVG: {EqnChunk: 10, EqnChunkFactor: 1}
  547. });
  548. });
  549. if (!HUB.Cancel) {
  550. HUB.cancelTypeset = false;
  551. var CANCELMESSAGE = "MathJax Canceled";
  552. HUB.Register.StartupHook("HTML-CSS Jax Config",function () {
  553. var HTMLCSS = MathJax.OutputJax["HTML-CSS"], TRANSLATE = HTMLCSS.Translate;
  554. HTMLCSS.Augment({
  555. Translate: function (script,state) {
  556. if (HUB.cancelTypeset || state.cancelled) {throw Error(CANCELMESSAGE)}
  557. return TRANSLATE.call(HTMLCSS,script,state);
  558. }
  559. });
  560. });
  561. HUB.Register.StartupHook("SVG Jax Config",function () {
  562. var SVG = MathJax.OutputJax["SVG"], TRANSLATE = SVG.Translate;
  563. SVG.Augment({
  564. Translate: function (script,state) {
  565. if (HUB.cancelTypeset || state.cancelled) {throw Error(CANCELMESSAGE)}
  566. return TRANSLATE.call(SVG,script,state);
  567. }
  568. });
  569. });
  570. HUB.Register.StartupHook("TeX Jax Config",function () {
  571. var TEX = MathJax.InputJax.TeX, TRANSLATE = TEX.Translate;
  572. TEX.Augment({
  573. Translate: function (script,state) {
  574. if (HUB.cancelTypeset || state.cancelled) {throw Error(CANCELMESSAGE)}
  575. return TRANSLATE.call(TEX,script,state);
  576. }
  577. });
  578. });
  579. var PROCESSERROR = HUB.processError;
  580. HUB.processError = function (error,state,type) {
  581. if (error.message !== CANCELMESSAGE) {return PROCESSERROR.call(HUB,error,state,type)}
  582. MathJax.Message.Clear(0,0);
  583. state.jaxIDs = []; state.jax = {}; state.scripts = [];
  584. state.i = state.j = 0;
  585. state.cancelled = true;
  586. return null;
  587. };
  588. HUB.Cancel = function () {this.cancelTypeset = true};
  589. }
  590. })();
  591. </script>
  592. </head>
  593. <body>
  594. Type text with embedded TeX in the box below:<br/>
  595. <textarea id="MathInput" cols="60" rows="10" onkeyup="Preview.Update(true)" onkeydown="Preview.Update()" style="margin-top:5px">
  596. abcd \ref{fred}
  597. \begin{equation}
  598. E=mc^2
  599. \end{equation}
  600. $\def\fred{}$
  601. $\def\fred{\tag{fred}\label{fred}}$
  602. xxx
  603. \begin{equation}
  604. aaa = bbb\fred
  605. \end{equation}
  606. \begin{equation}
  607. xxx = yyy
  608. \end{equation}
  609. </textarea>
  610. <br/><br/>
  611. <label><input type="checkbox" id="highlight" checked="true" onchange="Preview.highlight = this.checked"> Highlight changed sections</label>
  612. &nbsp; &nbsp;
  613. <label><input type="checkbox" id="noflicker" onchange="Preview.noflicker = this.checked"> Reduce flicker</label>
  614. &nbsp; &nbsp;
  615. <input type="button" value="Refresh Preview" onclick="Preview.Redraw()" />
  616. <div id="MoreMath"></div>
  617. <div id="MathPreview" style="border:1px solid; padding: 3px; width:660px; margin-top:5px"></div>
  618. <div style="display:none">Force loading: $x$</div>
  619. <script>
  620. Preview.Init();
  621. MathJax.Hub.Queue(["Update",Preview]);
  622. </script>
  623. </body>
  624. </html>
  625. <!--
  626. | There must be some missing constraints. If $\alpha_n$ is allowed to be negative, we get the following counterexample. $\smash{\rlap{\phantom{\Bigg(}}}$
  627. |
  628. | Define
  629. | $$
  630. | u_{n+1}=(1-\alpha_n)u_n+\beta_n\tag{1}
  631. | $$
  632. | and
  633. | $$
  634. | A_n=\prod_{k=1}^{n-1}(1-\alpha_k)\tag{2}
  635. | $$
  636. | By induction, it can be verified that
  637. | $$
  638. | u_n=A_n\left(u_1+\sum_{k=1}^{n-1}\frac{\beta_k}{A_{k+1}}\right)\tag{3}
  639. | $$
  640. | For $j\ge1$, define
  641. | $$
  642. | n_j=\left\{\begin{array}{}
  643. | 2^{j(j-1)/2}&\text{when }j\text{ is odd}\\
  644. | 2^{j(j-1)/2+1}&\text{when }j\text{ is even}
  645. | \end{array}\right.\tag{4}
  646. | $$
  647. | and for $n\ge1$,
  648. | $$
  649. | \alpha_n=\left\{\begin{array}{}
  650. | \frac{1}{n+1}&\text{for }n_j\le n< n_{j+1}\text{ when }j\text{ is odd}\\
  651. | -\frac1n&\text{for }n_j\le n< n_{j+1}\text{ when }j\text{ is even}
  652. | \end{array}\right.\tag{5}
  653. | $$
  654. | Obviously, $\displaystyle\lim_{n\to\infty}\alpha_n=0$.
  655. |
  656. | Using telescoping products, it is not difficult to show that
  657. | $$
  658. | \frac{A_{n_{j+1}}}{A_{n_j}}=\left\{\begin{array}{}
  659. | \frac{n_j}{n_{j+1}}=2^{-j-1}&\text{when }j\text{ is odd}\\
  660. | \frac{n_{j+1}}{n_j}=2^{j-1}&\text{when }j\text{ is even}
  661. | \end{array}\right.\tag{6}
  662. | $$
  663. | Equation $(6)$ yields
  664. | $$
  665. | A_{n_j}=\left\{\begin{array}{}
  666. | 2^{-(j-1)/2}&\text{when }j\text{ is odd}\\
  667. | 2^{-(3j-2)/2}&\text{when }j\text{ is even}
  668. | \end{array}\right.\tag{7}
  669. | $$
  670. | Furthermore, using the standard formula for the partial harmonic series, when $j$ is odd,
  671. | $$
  672. | \begin{align}
  673. | \sum_{n=n_j}^{n_{j+1}-1}\alpha_n
  674. | &=\log\left(\frac{n_{j+1}}{n_j}\right)+O\left(\frac{1}{n_j}\right)\\
  675. | &=(j+1)\log(2)+O\left(2^{-j(j-1)/2}\right)\tag{8}
  676. | \end{align}
  677. | $$
  678. | and when $j$ is even,
  679. | $$
  680. | \begin{align}
  681. | \sum_{n=n_j}^{n_{j+1}-1}\alpha_n
  682. | &=-\log\left(\frac{n_{j+1}}{n_j}\right)+O\left(\frac{1}{n_j}\right)\\
  683. | &=-(j-1)\log(2)+O\left(2^{-j(j-1)/2}\right)\tag{9}
  684. | \end{align}
  685. | $$
  686. | Combining $(8)$ and $(9)$ yields
  687. | $$
  688. | \sum_{n=1}^{n_j-1}\alpha_n=\left\{\begin{array}{}
  689. | \frac{j-1}{2}\log(2)+O(1)&\text{when }j\text{ is odd}\\
  690. | \frac{3j-2}{2}\log(2)+O(1)&\text{when }j\text{ is even}
  691. | \end{array}\right.\tag{10}
  692. | $$
  693. | Equation $(10)$ says that $\displaystyle\sum_{n=1}^\infty\alpha_n=\infty$.
  694. |
  695. | Define
  696. | $$
  697. | \beta_n=\left\{\begin{array}{}
  698. | 2^{-j}&\text{when }n=n_j-1\text{ for }j\text{ even}\\
  699. | 0&\text{otherwise}
  700. | \end{array}\right.\tag{11}
  701. | $$
  702. | Summing the geometric series yields $\displaystyle\sum_{n=1}^\infty\beta_n=\frac13$.
  703. |
  704. | Using $(3)$, we get
  705. | $$
  706. | \begin{align}
  707. | u_{n_{j+1}}
  708. | &=A_{n_{j+1}}\left(u_1+\sum_{k=1}^{n_{j+1}-1}\frac{\beta_k}{A_{k+1}}\right)\\
  709. | &\ge\frac{A_{n_{j+1}}}{A_{n_j}}\beta_{n_j-1}\\
  710. | &=2^{j-1}\cdot2^{-j}\\
  711. | &=\frac12\tag{12}
  712. | \end{align}
  713. | $$
  714. | when $j$ is even. $(12)$ says that $\displaystyle\lim_{n\to\infty}u_n\not=0$.
  715. -->