-
Notifications
You must be signed in to change notification settings - Fork 2
/
asciimth.sty
656 lines (589 loc) · 20.9 KB
/
asciimth.sty
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{asciimth}[2009/12/11 v0.1 ASCIIMath support for LaTeX]
\RequirePackage{amsmath}% for \matrix, others?
\RequirePackage{amsfonts}% for \mathbb et. al.
\def\appendToks@#1#2{%
\edef\tmpA{\the#1}%
#1=\expandafter{\tmpA#2}%
}
% Do this before setting the catcode of `, so we can refer to "`\@".
\def\setupASCII@{%
\catcode32=\active% space
\catcode126=12% "~"
\def\sspace{ }% Do it here so the inside space is lexed as active
\newlinechar=`\$\relax
\defineTokTypes@
}
\def\makeQuoteActive{\catcode96=13}
\def\makeQuoteOther{\catcode96=12}
\makeQuoteActive
\protected\def`{%
\begingroup% don't pollute the environment with macros defined in ifNextTokIs@.
\ifNextTokIs@`%
{\endgroup\char96\char96}%
{\setupASCII@\startParse@}%
}
\newtoks\lexedTokens@
\def\addLexed@{\appendToks@\lexedTokens@}
\def\addBlock@#1{\addLexed@{\block0{}{#1}}}
\newtoks\parsedTokens@
\def\startParse@#1`{%
\ifmmode\let\mathdelim=\relax\else\let\mathdelim=$\fi
\def\useParse##1{\mathdelim##1\mathdelim\endgroup}%
\runParse@{#1}%
}
\def\runParse@#1{%
\lexedTokens@={}\lexTokens@#1\endScan@
%\message{Lexed: \the\lexedTokens@.$}%
\expandafter\readOList@\the\lexedTokens@\endScan@
%\message{Parsed: \the\parsedTokens@.$}%
\expandafter\useParse\expandafter{\the\parsedTokens@}%
}
% Usage: \newMathEnv{alignA*}{align*}
% Defines a new math environment that takes ASCIIMath input.
\def\newMathEnv@#1#2{%
\newenvironment{#1}{%
\begingroup\setupASCII@
\def\useParse####1{\begin{#2}####1\end{#2}\endgroup\end{#1}}%
\collectInput@
}{\ignorespacesafterend}%
}
% Scans everything until the next \end{..},
% and calls \runParse@ on it.
% Callers of this should set \useParse@.
% Correctly handles nested environments, using tricks that I learned/borrowed
% from the amsmath package.
\def\collectInput@{%
\asciiInput@={}%
\def\bStack{bx}%
\def\emptyStack{x}%
\loopCollectInput@
}
\newtoks\asciiInput@
\def\addAsciiInput@#1{%
\edef\tmpA{\the\asciiInput@}%
\global\asciiInput@=\expandafter{\tmpA#1}%
}
% Usage: \pushBegins@...\begin\end
% (internals is assumed to not contain an end)
% Expands to a sequence of b's, one for each \begin in the "..."
\def\scanBegins@#1\begin#2{%
\ifx\end#2\else b\expandafter\scanBegins@\fi
}
\def\gobbleOne@#1{}
\def\loopCollectInput@#1\end#2{%
% Turn all of the begins in #1 into b's, add them to the stack,
% and delete one b from the stack.
\edef\bStack{\scanBegins@#1\begin\end\expandafter\gobbleOne@\bStack}%
% If the stack is empty, we're done.
% Otherwise, we need to read more \ends, so keep going.
\ifx\bStack\emptyStack
\addAsciiInput@{#1}%
\def\next{\expandafter\runParse@\expandafter{\the\asciiInput@}}%
\else
\addAsciiInput@{#1\end{#2}}%
\loopCollectorInput@
\fi
\next
}
%\def\collectInput@#1\end#2{\runParse@{#1}}
\newMathEnv@{alignA}{align}
\newMathEnv@{alignA*}{align*}
\newMathEnv@{asciimth}{equation}
\newMathEnv@{asciimth*}{equation*}
% sentinel value
% Since we do comparisons, I try to make sure this is unique.
\def\endScan@{\errmessage{Oops, went over the end!}\let\fooAM@=\endScan@}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Lexing
%
% TODO (maybe these shouldn't go here):
% * Register \sqrt (maybe that shouldn't go here?)
% * Try using operator precedence: a^2/b_3 == (a^2)/(b_3)
% * also a^2_1 should work better
%
\begingroup
\catcode32=13
%\catcode40=13
\gdef\sspace@{ }
\endgroup
\def\lexTokens@{\futurelet\nextTok\lexTokensA@}
% First, check if the next token starts a {...} subexpression :
\def\lexTokensA@{%
\ifx\nextTok\bgroup\let\next=\lexTokenGroup@
\else\let\next=\lexTokensB@
\fi\next
}
\def\lexTokenGroup@#1{%
\begingroup
\def\useParse##1{\endgroup\addLexed@{\subblock{##1}}\lexTokens@}%
\runParse@{#1}
}
\let\lbraceTok@=\{
\let\rbraceTok@=\}
\def\lexTokensB@#1{%
\ifx#1\endScan@\let\next=\relax
\else\if\sspace@\noexpand#1\let\next=\lexTokens@% ignore spaces
\else\ifcat\noexpand#1a\def\next{\parseLetterOrCS@#1}%
\else\ifcat\noexpand#1^\def\next{\addLexed@{^}\lexTokens@}%
\else\ifcat\noexpand#1_\def\next{\addLexed@{_}\lexTokens@}%
\else\ifx#1\lbraceTok@\def\next{\addLexed@{\startParen{\lbraceTok@}}\lexTokens@}%
\else\ifx#1\rbraceTok@\def\next{\addLexed@{\endParen{\rbraceTok@}}\lexTokens@}%
\else\ifcat\noexpand#1/\def\next{\processOtherChar@#1}%
\else\def\next{\addBlock@{#1}\lexTokens@}%
\fi\fi\fi\fi\fi\fi\fi\fi
\next
}%
\def\processOtherChar@#1{%
\ifx#1/\let\next=\scanFracOrDiv@
\else\ifx#1(\def\next{\addLexed@{\startParen(}\lexTokens@}%
\else\ifx#1[\def\next{\addLexed@{\startParen[}\lexTokens@}%
\else\ifx#1)\def\next{\addLexed@{\endParen)}\lexTokens@}%
\else\ifx#1]\def\next{\addLexed@{\endParen]}\lexTokens@}%
\else\ifx#1"\let\next\scanRomanText@%
\else
\ifisdigitchar@#1{\def\next{\scanNumber@{#1}}}%
{\def\next{\scanOtherChars@{#1}}}%
\fi\fi\fi\fi\fi\fi
\next
}
\def\scanFracOrDiv@{\futurelet\nextTok\scanFracOrDivA@}
\def\scanFracOrDivA@{%
\ifx\nextTok/\def\next##1{\addBlock@{/}\lexTokens@}%
\else\def\next{\addLexed@{/}\lexTokens@}%
\fi
\next
}
\def\scanRomanText@#1"{\addBlock@{\text{#1}}\lexTokens@}
% There must be a more efficient way to do this...
\newif\ifisDigitCharCond@
\def\ifisdigitchar@#1#2#3{%
\isDigitCharCond@false
\ifx0#1\isDigitCharCond@true\fi
\ifx1#1\isDigitCharCond@true\fi
\ifx2#1\isDigitCharCond@true\fi
\ifx3#1\isDigitCharCond@true\fi
\ifx4#1\isDigitCharCond@true\fi
\ifx5#1\isDigitCharCond@true\fi
\ifx6#1\isDigitCharCond@true\fi
\ifx7#1\isDigitCharCond@true\fi
\ifx8#1\isDigitCharCond@true\fi
\ifx9#1\isDigitCharCond@true\fi
\ifisDigitCharCond@#2\else#3\fi
}
\def\scanNumber@#1{\def\todo{\scanNumberA@{#1}}\futurelet\nextTok\todo}
\def\scanNumberA@#1{%
\ifisdigitchar@\nextTok{\def\next##1{\scanNumber@{#1##1}}}%
{\def\next{\addBlock@{#1}\lexTokens@}}%
\next
}
\newtoks\csNameStr@
\def\parseLetterOrCS@#1{%
% First check whether the next unparsed token is a letter.
\def\todo{\ifcat\noexpand\nextTok a\def\next{\csNameStr@={#1}\parseCS@}%
\else\def\next{\addBlock@#1\lexTokens@}\fi\next}%
\futurelet\nextTok\todo
}
\def\parseCS@{\futurelet\nextTok\parseCSa@}
\def\parseCSa@{%
\ifcat\noexpand\nextTok a\let\next=\addToCSAndCont@%
\else\let\next=\emitCSAndLex@%
\fi\next
}
\def\addToCSAndCont@#1{%
\appendToks@\csNameStr@#1%
\parseCS@
}
\def\leftName@{@left@asciimacro@}
\def\rightName@{@right@asciimacro@}
\def\matrixName@{@matrix@asciimacro@}
\def\rawName@{@raw@asciimacro@}
\def\envName@{@env@asciimacro@}
\def\labelName@{@label@asciimacro@}
\def\emitCSAndLex@{%
\edef\asciiCmdName{@\the\csNameStr@ @asciimacro@}%
% First, check whether this name has been set with \addASCIIMathCmd:
\expandafter\ifcsname\asciiCmdName\endcsname
\def\next{\expandafter\addPredefinedName@\expandafter{\asciiCmdName}%
\lexTokens@}%
% Next, check if it's left or right:
\else\ifx\asciiCmdName\leftName@\def\next{\lexDelim@\startParen}%
\else\ifx\asciiCmdName\rightName@\def\next{\lexDelim@\endParen}%
% Next, check if it's the start of a matrix:
\else\ifx\asciiCmdName\matrixName@\def\next{\addLexed@\startMatrix\lexTokens@}%
% Next, check if it's a label:
\else\ifx\asciiCmdName\labelName@\def\next##1{\addBlock@{\label{##1}}\lexTokens@}%
% Next, check if it's a "raw" command:
\else\ifx\asciiCmdName\rawName@\def\next##1{\addBlock@{##1}\lexTokens@}%
% Next, check if it's an "env" command:
\else\ifx\asciiCmdName\envName@
\def\next##1{\addLexed@{\asciiCmd{\latexEnv@{##1}}{1}}\lexTokens@}%
% Otherwise, check if it's set as a normal macro:
\else\expandafter\ifcsname\the\csNameStr@\endcsname
\def\next{%
\expandafter\expandafter\expandafter
\addBlock@\expandafter\csname\the\csNameStr@\endcsname
\lexTokens@
}%
\else\def\next{%
\errmessage{Oops, command "\the\csNameStr@" is not defined!}
}%
\fi\fi\fi\fi\fi\fi\fi\fi\next
}
% If the longest possible sequence of "other" characters is defined,
% set it to a name.
% Otherwise, output each as a block (though any "-" should be on their own).
%
\def\scanOtherChars@#1{\csNameStr@={#1}\loopOtherChars@}
\def\loopOtherChars@{\futurelet\nextTok\loopOtherCharsA@}
\def\loopOtherCharsA@{%
\ifcat\noexpand\nextTok+%
% TODO: annoying to duplicate the list from processOtherChar@ here.
\ifx\nextTok/\let\next=\processOtherChars@
\else\ifx\nextTok(\let\next=\processOtherChars@
\else\ifx\nextTok[\let\next=\processOtherChars@
\else\ifx\nextTok)\let\next=\processOtherChars@
\else\ifx\nextTok]\let\next=\processOtherChars@
\else\ifisdigitchar@\nextTok{\let\next=\processOtherChars@}%
{\let\next=\addOtherToCS@}%
\fi\fi\fi\fi\fi
\else\let\next=\processOtherChars@
\fi\next
}
\def\addOtherToCS@#1{\appendToks@\csNameStr@#1\loopOtherChars@}
\def\processOtherChars@{%
\edef\asciiCmdName{@\the\csNameStr@ @asciimacro@}%
% First, check whether this name has been set with \addASCIIMathCmd:
\expandafter\ifcsname\asciiCmdName\endcsname
\def\next{\expandafter\addPredefinedName@\expandafter{\asciiCmdName}}%
% Otherwise, emit each token in turn:
\else\def\next{\expandafter\emitLoneOtherChars@%
\the\csNameStr@\endScan@}%
\fi\next\lexTokens@
}
% TODO: have to dupe processOtherChar@ here...
% Annoying. Must be better abstraction.
\def\emitLoneOtherChars@#1{%
\ifx#1\endScan@\let\next=\relax
\else\ifx#1-\def\next{\addLexed@{-}\emitLoneOtherChars@}%
\else\ifx#1,\def\next{\addLexed@{,}\emitLoneOtherChars@}%
\else\ifx#1;\def\next{\addLexed@{;}\emitLoneOtherChars@}%
\else\def\next{\addBlock@{#1}\emitLoneOtherChars@}%
\fi\fi\fi\fi\next
}
\def\addPredefinedName@#1{%
\expandafter\expandafter\expandafter\addPredefinedNameA@\csname#1\endcsname
}
\def\addPredefinedNameA@#1#2{%
\addLexed@{\asciiCmd{#1}{#2}}%
}
% TODO: I'd like to be able to peek first, but I can't figure out how
% to get the delcode of a token.
\def\lexDelim@#1#2{\addLexed@{#1{#2}}\lexTokens@}%
%%%%%%%%%%%%%%
%% Parsing lexed tokens into a large structure
%
% Grammar is:
%
% E -> O E | <empty>
% O -> S | S / S
% S -> T | S_Tm | S^Tm
% Tm -> T | -T
% T -> <block> or (E) | <func> T_1 ... T_n
% Each <func> was defined by addASCIIMathCmd
% and expects a fixed number of arguments.
%
% Note that an expression like 2^x^x will be accepted by asciimth,
% and passed to TeX which will complain with a useful error message.
%
% Objects inside are:
% Raw operations /,^,_,-
% \block#1#2#3: #1 is 1 if this should be wrapped with parens when not a
% subexpr of a fraction, exponent, etc., and 0 otherwise.
% #2 is eg "()" or "[)" if this should be wrapped
% #3 is the internals of the block.
% \subblock#1: block of stuff surrounded by {}. Treated like a <block>.
% \startParen#1: #1 is '(', '[', etc.
% \endParen#1: #1 is ')', ']', etc.
% \startMatrix: A parenthesized expression follows.
% \asciiCmd#1#2: #1 is code, #2 is number of args.
%
% These definitions should never actually be expanded. However, they
% are necessary for \ifx to distinguish between the tokens.
\def\defineTokTypes@{%
\def\block{\readO@\block}%
\def\startParen{\readO@\startParen}%
\def\endParen##1{\errmessage{Oops; found an unmatched "##1".}}%
\def\startMatrix{\readO@\startMatrix}%
\def\asciiCmd{\readO@\asciiCmd}%
\def\subblock{\readO@\subblock}%
}
\def\readOList@{%
\parsedTokens@={}%
\def\useO##1{\appendToks@\parsedTokens@{##1}\readOListLoop@}%
\readOListLoop@
}
\def\readOListLoop@{\ifNextTokIs@\endScan@\relax\readO@}
\def\readO@{\let\useS=\maybeDoFraction@\readS@}
\def\maybeDoFraction@#1#2#3{%
\ifNextTokIs@/{\readFraction@{#3}}
{\let\useBlock=\useO\renderBlock@#1{#2}{#3}}%
}
% Checks whether the next token is #1. If so, eats it and runs #2.
% Otherwise, leaves it alone and runs #3.
\def\ifNextTokIs@#1#2#3{%
\def\todo{\ifx#1\nextTok\def\next####1{#2}\else\def\next{#3}\fi\next}%
\futurelet\nextTok\todo
}
% Given the numerator to a fraction (#1)
% parses the next S and returns the resulting fraction.
\def\readFraction@#1{%
\def\useS##1##2##3{\useO{\frac{#1}{##3}}}%
\readS@
}
% Reads an S nonterminal and passes the result to useS.
\def\readS@{\let\useT=\addSubSupScript@\readT@}
% Given an already-read block, read more ^ and _ if possible.
% Note: if they do eg x_y_y it will parse and TeX will complain, but
% it's the same error they'd get with $..$.
\def\addSubSupScript@#1#2#3{%
\ifNextTokIs@_%
{\def\useBlock{\doSubSupScript@_}\renderBlock@#1{#2}{#3}}%
{\ifNextTokIs@^%
{\def\useBlock{\doSubSupScript@^}\renderBlock@#1{#2}{#3}}%
{\useS#1{#2}{#3}}}%
}
% #1 is _ or ^. #2 is the rendered base.
\def\doSubSupScript@#1#2{\ifNextTokIs@-%
{\doNegatedScript@#1{#2}}%
{\doSimpleScript@#1{#2}}%
}
\def\doNegatedScript@#1#2{%
\def\useT##1##2##3{%
\def\useBlock####1{\addSubSupScript@0{}{#2#1{-####1}}}%
\renderBlock@##1{##2}{##3}%
}%
\readT@
}
\def\doSimpleScript@#1#2{%
\def\useT##1##2##3{\addSubSupScript@0{}{#2#1{##3}}}%
\readT@
}
% Wraps the argument #3 in the parentheses (#2) unless there are none (#1==0).
% Passes the result to \useBlock.
\def\renderBlock@#1#2#3{%
\ifx0#1\def\next{\useBlock{#3}}%
\else\def\next{\expandafter\useBlock\expandafter{\wrapBlock@#2{#3}}}%
\fi\next
}
% #1 is the open-delim, #2 is the close-delim.
% Note: using \left..\right makes the object in parentheses act like
% \mathinner, which isn't great since this happens to EVERY parenthesis.
% For example, in $f\left(x\right)$ there's too much space between
% the "f" and the "(".
% Fix this by making it act like a \mathopen+\mathclose.
% Note we do still lose a tiny bit of kern between $f(x)$ and `f(x)`, but
% it's less noticeable.
% TODO: we could try checking the height of the inner formula ourselves, and
% use plain "(" and ")" if it's no bigger than those characters.
\def\wrapBlock@#1#2#3{\mathopen{}\mathclose{\left#1#3\right#2}}
% Reads a "T" terminal (a lone block or a parenthesized expression)
% and then passes it as an argument to \useT.
\def\readT@#1{%
\ifx#1\block\let\next=\readTBlock@
\else\ifx#1-\def\next{\useT0{}{#1}}%
\else\ifx#1,\def\next{\useT0{}{#1}}%
\else\ifx#1;\def\next{\useT0{}{#1}}%
\else\ifx#1\startParen\let\next=\readTParen@
\else\ifx#1\startMatrix\let\next=\readTMatrix@
\else\ifx#1\asciiCmd\let\next=\readTASCIICmd@
\else\ifx#1\subblock\let\next=\parseSubblock@
\else\ifx#1/\def\next{\useT0{}{#1}}%
% Give a more helpful error message if we hit the end:
\else\def\next{\reportError@{#1}}%
\fi\fi\fi\fi\fi\fi\fi\fi\fi
\next
}
\def\reportError@#1{%
\ifx#1\endScan@\def\next{%
\errmessage{Oops: Ran into the end of input;
may be missing a closing delimiter}}%
\else\ifx#1\endParen\def\next##1{%
\errmessage{Oops: Extra unmatched right delimiter '\noexpand##1'}}%
\else\def\next{\errmessage{Oops: ran into a non-block:\noexpand#1}}%
\fi\fi
\next
}
\def\readTBlock@#1#2#3{\useT#1{#2}{#3}}
\def\parseSubblock@#1{\useT0{}{{#1}}}
% OK, this uses a couple of tricks:
% 1) The #1 of asciiCmdLoop@ is a partial application of the macro.
% So appending something to that parameter means applying the macro
% to an additional argument.
%
% 2) This function recursively calls readT. So it saves the relevant
% parameters by putting the readT call in a new group.
\newcount\cmdArgsNeeded@
\def\readTASCIICmd@#1#2{\cmdArgsNeeded@=#2\relax\asciiCmdLoop@{#1}}
\def\asciiCmdLoop@#1{%
\ifnum\cmdArgsNeeded@=0%
\def\next{\useT0{}{#1}}%
\else
\def\next{%
\advance\cmdArgsNeeded@ by -1\relax%
% start a new group since we need to keep around the old
% version of useT and cmdArgsNeeded.
\bgroup
\def\useT####1####2####3{%
\egroup
\asciiCmdLoop@{#1{####3}}%
}%
\readT@
}%
\fi\next
}
% Read parentheses in a similar style as above:
% Keep a token list of the parenthesis contents
% And continually read O's and append them into the token list
% until you hit an endParen.
% (Nested endParens will be automatically gobbled up by the
% recursive call to readO).
\newtoks\parenContents@
\def\readTParen@#1{%
\def\parenStarter{#1}%
\parenContents@={}%
\loopForEndParen@%
}
\def\loopForEndParen@{\ifNextTokIs@\endParen
{\expandafter\finishParen@\parenStarter}%
{\readMoreParenContents@}%
}
\def\readMoreParenContents@{%
\begingroup
\def\useO##1{%
\endgroup
\appendToks@\parenContents@{##1}%
\loopForEndParen@%
}%
\readO@
}
\def\finishParen@#1#2{%
\expandafter\useT\expandafter1\expandafter{%
\expandafter#1\expandafter#2\expandafter}%
\expandafter{\the\parenContents@}%
}
% Read matrices, basically the same way as for parentheses.
% (TODO: don't duplicate so much code.)
% The lexer is supposed to return:
% \startMatrix\startParen(...\endParen)
% where the stuff inside of the ... is a sequence of O's, commas and semicolons.
% Each comma/semicolon is replaced by "&" or "\\".
\def\readTMatrix@\startParen#1{%
\def\parenStarter{#1}%
\parenContents@={}%
\loopTillEndMatrix@
}
\def\loopTillEndMatrix@{\futurelet\nextTok\loopTillEndMatrixA@}
\def\loopTillEndMatrixA@{%
\ifx\nextTok\endParen
\def\next##1##2{\expandafter\finishMatrix@\parenStarter##2}%
\else\ifx\nextTok,%
\def\next##1{\appendToks@\parenContents@{&}\loopTillEndMatrix@}%
\else\ifx\nextTok;%
\def\next##1{\appendToks@\parenContents@{\\}\loopTillEndMatrix@}%
\else\def\next{\anotherMatrixElt@}%
\fi\fi\fi
\next
}
\def\anotherMatrixElt@{%
\begingroup
\def\useO##1{%
\endgroup
\appendToks@\parenContents@{##1}%
\loopTillEndMatrix@
}%
\readO@
}
\def\amBeginMatrix@{\begin{matrix}}
\def\amEndMatrix@{\end{matrix}}
% matrix(a,b,c) should expand to:
% \useT 1{()}{\amBeginMatrix@ a&b&c \amEndMatrix@}
\def\finishMatrix@#1#2{%
\expandafter\useT\expandafter1%
\expandafter{\expandafter#1\expandafter#2\expandafter}%
\expandafter{\expandafter\amBeginMatrix@\the\parenContents@
\amEndMatrix@}%
}
%%%%%%%%%%%%%
% Using TeX macros inside of ASCIIMath:
%
% A macro which takes no arguments (for example, \alpha or \Sum) can always
% be referred to by name. And a build-in macro like \sqrt will work. But
% other macros which take an argument will not work by default.
%
% The following macro tells the ASCIIMath engine that a macro takes arguments:
% It works by setting a corresponding macro name which the engine will look up.
% Outermost parentheses of arguments will be eliminated (just like for sqrt).
% The format is:
% <name> -> @name@asciimacro@
% The macro definition will internally keep track of the number of args.
%
% Usage example:
%
% \newcommand{\myroot}[2]{\sqrt[#1]{#2}}
% \addASCIIMathCmd{root}{\myroot}{2}
%
\def\addASCIIMathCmd#1#2#3{%
\begingroup
\makeatletter
\edef\name{@#1@asciimacro@}%
\def\macroValue{{#3}#2}%
\expandafter\global\expandafter\let\csname\name\endcsname=\macroValue
\endgroup
}
% Start off with a couple of definitions:
% sqrt, root, hat, bb@
\addASCIIMathCmd{sqrt}{1}{\sqrt}
\def\myrootn@#1#2{{\sqrt[#1]{#2}}}
\addASCIIMathCmd{root}{2}{\myrootn@}
\addASCIIMathCmd{bb}{1}{\mathbb}
\addASCIIMathCmd{bf}{1}{\mathbf}
\addASCIIMathCmd{cal}{1}{\mathcal}
\addASCIIMathCmd{hat}{1}{\hat}
\addASCIIMathCmd{tilde}{1}{\tilde}
\addASCIIMathCmd{bar}{1}{\bar}
\addASCIIMathCmd{widehat}{1}{\widehat}
\addASCIIMathCmd{overline}{1}{\overline}
% Symbols
\addASCIIMathCmd{,...,}{0}{,\ldots,}
\addASCIIMathCmd{,...}{0}{,\ldots}
\addASCIIMathCmd{...,}{0}{\ldots,}
\addASCIIMathCmd{...}{0}{\cdots}
\addASCIIMathCmd{->}{0}{\to}
\addASCIIMathCmd{<-}{0}{\leftarrow}
\addASCIIMathCmd{>=}{0}{\ge}
\addASCIIMathCmd{<=}{0}{\le}
\addASCIIMathCmd{=>}{0}{\implies}
\addASCIIMathCmd{<=>}{0}{\iff}
\addASCIIMathCmd{|->}{0}{\mapsto}
\addASCIIMathCmd{!=}{0}{\ne}
\addASCIIMathCmd{==}{0}{\equiv}
\addASCIIMathCmd{+-}{0}{\pm}
\begingroup
\catcode126=12\relax% "~"
\addASCIIMathCmd{~}{0}{\sim}
\addASCIIMathCmd{~=}{0}{\cong}
\addASCIIMathCmd{~~}{0}{\approx}
\addASCIIMathCmd{~-}{0}{\simeq}
\endgroup
\addASCIIMathCmd{substack}{1}{\substack}
% Embedded LaTeX environments:
% Goal is for `env{pmatrix}(...)` to turn into \begin{pmatrix}...\end{pmatrix}.
% The command "env" is hard-coded in \emitCSAndLex@.
\def\latexEnv@#1#2{\begin{#1}#2\end{#1}}
% OK, plan is:
% big(..), Big[...]
% will scale both delimiters.
% My idea is for "big" to be lexed as a macro,
% and have addASCIIMathCmd hard-code the handling of parentheses.
% (Actually, that's a useful thing to have, e.g. if you use sin[x] vs sin(x)).