Commit Graph

43 Commits

Author SHA1 Message Date
Emily Eisenberg
925c96dbe2 Add square roots (\sqrt)
Summary:
Follow the TeXbook instructions on how to construct square roots. Using
makeCustomSizedDelim, this becomes nearly trivial.

Test Plan:
 - Make sure normal tests work
 - Make sure the new huxley test looks good, and other huxley tests haven't changed.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D12918
2014-09-06 15:08:23 -07:00
Emily Eisenberg
c3f758c319 Add support for \left and \right
Summary:
Added stacked delimiter support for more delimiters. Split out delimiter
functions into its own file, and split out some tree building functions into a
common file. Supports the empty `.` delimiter with \left and \right, and doesn't
try to produce huge /, \backslash, <, or > delimiters. Depends on D7844

Test input:

\left( \left) \left[ \left\lbrack \left] \left\rbrack \left\{ \left\lbrace
\left\} \left\rbrace \left\lfloor \left\rfloor \left\lceil \left\rceil
\left\langle \left\rangle \left/ \left\backslash \left| \left\vert \left\|
\left\Vert \left\uparrow \left\Uparrow \left\downarrow \left\Downarrow
\left\updownarrow \left\Updownarrow {x^{x^{x^{x^{x^{x^{x^{x^{x^{x^x}}}}}}}}}}
\right.\right.\right.\right.\right.\right.\right.\right.\right.\right.
\right.\right.\right.\right.\right.\right.\right.\right.\right.\right.
\right.\right.\right.\right.\right.\right.\right.\right.

Test Plan:
 - Run the test input, see that it works
 - Run the tests, see that they work
 - Look at huxley screenshots (not here yet :( ) and make sure they look good

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D11602
2014-09-04 21:58:43 -07:00
Emily Eisenberg
f17bbf1b05 Add the '\rule' command for drawing boxes
Summary:
Supports the 'ex' and 'em' units for sizes. Doesn't support the optional depth
argument.

Test Plan:
 - See that the huxley test looks good, and nothing else changed
 - See that the tests pass

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D12777
2014-08-29 14:45:27 -07:00
Emily Eisenberg
fe6b67817c Add support for \overline
Summary:
Follow the instructions in the TeX book for drawing \overlines. This uses the
same code as fractions to produce the bars. Also added the ability to cramp
styles (i.e. T -> T' and T' -> T').

Test Plan:
 - Look at `\overline{x}`, `\overline{\dfrac{x}{y}+z}`, and
   `\blue{\overline{x}}` to make sure they look good.
 - Make sure the tests work
 - Make sure the huxley tests look good (Not here yet :( )

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D11604
2014-08-05 17:48:10 -07:00
Emily Eisenberg
100798847b Add delimiter sizing
Summary:
Make delimiter sizing work. This involved
 - Adding the symbols for the remaining delimiters (like `\lfloor` and `\{`)
 - Adding metrics for the size1, size2, size3, and size4 fonts
 - Parsing delimiter sizing functions
 - Using the big fonts when possible, otherwise building large copies of the
   delimiters from scratch

Test Plan:
 - See that
   `\bigl\uparrow\Bigl\downarrow\biggl\updownarrow\Biggl\Uparrow
    \Biggr\Downarrow\biggr\Updownarrow\bigm/\Bigm\backslash\biggm|
    \Biggm|\big\lceil\Big\rceil\bigg\langle\Bigg\rangle\bigl(\Bigl)
    \biggl[\Biggl]\Biggr\{\biggr\}\Bigr\lfloor\bigr\rfloor`
   parses correctly (this contains all of the delimiters, and all of the sizing
   modes)
 - See that the huxley tests didn't change, and the new one looks good
 - See the normal tests work

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D7844
2014-08-05 16:43:43 -07:00
Emily Eisenberg
02935f7dde Add an 'implicit group' parser, use with sizing
Summary:
Implicit groups are objects that act like groups but don't have brackets around
them. This is used for things like sizing functions or font-change functions
that can occur in the middle of the group, but act like they apply to a group
after them which stops when the current group stops. E.g. `Hello {world \Large
hello} world` produces normal, normal, Large, normal text. (Note, I just came up
with the name implicit group, I don't think this is actually how it is parsed in
LaTeX but it fits nicely with the current parsing scheme and seems to work
well).

For now, apply this to the sizing functions (we don't have any other functions
that act like this). Also note that this doesn't really do much practically
because we limit sizing functions to only be on the top level of the expression,
but it does let people do `\Large x` and get a large `x`, without having to add
braces.

Test Plan:
 - Run the tests, see they work
 - Make sure `abc \Large abc` looks correct

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D10876
2014-07-10 16:06:19 -07:00
Ben Alpert
e46dd418b3 Ignore whitespace following \text
Summary:
In something like `\text {hello, world}` we want to ignore the first space but not the second. This suggests to me that perhaps we shouldn't be in text-lexing mode until we actually see the first curly brace, so this changes us to do that.

Note that this disallows doing unbraced text things now; it seemed nontrivial to lex a single text character while ignoring spaces. We should probably fix it sometime, but for now we'll fall back to MathJax anyway so I'm not super concerned.

We should also add a huxley test for this.

Test Plan: `\text {2x 2}` rendered `2x 2` in a non-italic font.

Reviewers: jack, emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D9376
2014-05-30 12:29:32 -07:00
Ben Alpert
dc4857ac92 Fix typesetting of '
Not 100% sure this is the right fix but it appears to work.

Test Plan: Typeset $x'$ in Perseus.

Auditors: emily
2014-05-26 01:13:52 -07:00
Emily Eisenberg
e68cc472c6 Make errors more informative
Make error messages from the lexing and parsing stages be a bit more helpful. If
provided with the input and a position, the error will display the error
position, and the nearby input with the error position underlined (yay combining
marks). Also, standardize the errors a bit (remove doubled "Error:" strings)

Test plan:
 - Make sure the errors look totally sweet (before: {F15602}, after: {F15603})
 - Trigger every error (that can be triggered) in Parser, Lexer, and buildTree
   using the inputs:
  `a^`
  `a_`
  `a^x^x`
  `a_x_x`
  `\color f`
  `\blue `
  `\Huge`
  `\llap`
  `\text`
  `\dfrac`
  `\dfrac{x}`
  `\d`
  `\blue{`
  `\color{#f`
  `{\Huge{x}}`
 - See that the tests still work

Auditors: alpert
2014-03-30 23:05:37 -04:00
Emily Eisenberg
d729ba5281 Add a \color command for custom colors
Summary:
Keep track of the color inside the style now, and use that when we are
rendering things. Added a custom lexing mode to handle lexing colors correctly.
Prefixed the old katex colors (internally) with "katex-" to distinguish them
from CSS colors.

Test Plan:
 - Run the normal tests, see they work
 - Run the huxley tests, see that they didn't change except for the color one
   which looks right

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D7763
2014-03-27 12:34:45 -04:00
Emily Eisenberg
7723d3dcaf First attempt at \text function
Summary:
Make all of the parsing functions keep track of whether they are
parsing in math mode or text mode. Then, add a separate lexing function to lex
text mode, which is different than the normal mode because it does weird things
with spacing and allows a different set of characters.

Test Plan:
 - See that the normal tests work
 - See that the huxley screenshot looks reasonable
 - See that none of the other huxley screenshots changed

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D7578
2014-03-26 22:17:41 -04:00
Emily Eisenberg
2eca338e23 Replace our annoying lookup tables with a unified symbol table
Summary:
Before, there were a couple problems:
1. There were similar tables of functions in the buildTree and Parser modules,
   making it hard to add new functions, and code duplication is bad
2. We distinguished the font to use (mostly just ams vs normal) using a
   different parser class, which led to annoyance and some weird bugs
3. (Not actually in this code, but in D7578) We can't distinguish between
   functions that should be expanded in text mode and those that shouldn't.
To solve these problems, I've created an all-encompassing table in symbols.js
that fixes 1. and 2., and should fix 3. once it's merged in. The table keeps
track of each of the functions and their class, font, and possible replacement,
and then Parser and buildTree do lookups in this table.

Also added the functions for the new_definitions functions, to show how easy it
is to add new functions.

Test Plan:
1. Run the normal tests, see they all pass
2. Run huxley, see none of the screenshots changed
3. See that things like the main page loads
4. See that the new functions look correctly with
   '\barwedge\veebar\odot\oplus\otimes\oslash\circledcirc
    \boxdot\bigtriangleup\bigtriangledown\dagger\diamond
    \star\triangleleft\triangleright'

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D7704
2014-03-26 01:10:10 -04:00
Alex Lopatin
ba84964d6f Add \enspace (0.5em space)
Summary:
TeX spacing is complicated - there's \enskip and \enspace and the two are
subtly different in some weird way. But \enspace seems to be the preferred
half em space in LaTeX, and the only one which MathJax supports.

Test Plan: Parsed and rendered correctly.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D6437
2014-01-29 18:14:12 -08:00
Alex Lopatin
50c1242147 Added \! (negative thin space)
Test Plan: Parses and renders locally. \!\, and \,\! are now no-ops.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D6395
2014-01-28 16:20:14 -08:00
Alex Lopatin
8c179252c5 Added \cong + fixed comment copypasta.
Test Plan: Parses and renders locally.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D6393
2014-01-28 16:13:51 -08:00
Ben Alpert
fe346f56ca Add \langle and \rangle
Auditors: alex
2014-01-16 22:32:24 -08:00
Ben Alpert
99e7710020 Don't change global prototype: local utils.indexOf
Auditors: eater
2014-01-14 19:52:53 -08:00
Ben Eater
ee58c126fb Add an Array.indexOf polyfill and don't depend on underscore.js
Auditors: alpert
2014-01-14 17:40:21 -08:00
Ben Eater
c45685e4ea Don't use native indexOf
Test Plan: Use KaTeX to render "\Large{123}" in IE8

Auditors: alpert
2014-01-14 12:02:48 -08:00
Ben Alpert
330b1f5873 Add \le, \ge, \ne symbols
Auditors: eater
2014-01-13 13:37:27 -08:00
Ben Alpert
c8ba6a698f Add \in
Auditors: eater
2014-01-10 11:56:42 -08:00
Ben Alpert
302f5502ef Add \approx as a symbol
This is important because the interactive graph labels use \approx now for polygon sides and it feels really sluggish with MathJax.
2013-11-21 22:15:25 -08:00
Ben Alpert
f3225ee7ee Add percent sign
Auditors: emily
2013-09-13 16:10:34 -07:00
Ben Alpert
2efa794fd3 Add \triangle
Auditors: emily
2013-08-27 18:33:40 -07:00
Emily Eisenberg
387c159a8e Add sizing functions (like \small)
Summary:
Right now, when the size gets bigger, this still doesn't work, so there's a
check to prevent that. However, functions that go smaller (like `\small`,
`\tiny`, etc) do work. Also, we can't seem to use the sizing functions inside
of fractions (so something like `\dfrac{\small\frac{x}{y}}{z}` doesn't work).
However, the most prominent use case is `\small` as the outer-most object, so
this is still helpful. This commit has the parsing and stuff to handle all of
it, but it'll throw an error if you try to do something that doesn't work. (For
the record, "doesn't work" means "looks bad", not "throws an unexpected
error").

Test Plan:
Make sure things like `\small x` work, and things like `\Huge x` and
`\frac{\small x}{y}` don't.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3619
2013-08-21 20:22:24 -07:00
Emily Eisenberg
920dd0bef9 Add some AMS characters
Summary:
\nleq and \ngeq are AMS characters, so add support for parsing and
showing those. Also, add font metrics from the ams_regular font.

Test Plan:
View `\ngeq \geq`, make sure that they look the same but with a
slash through `\ngeq`. View `a \ngeq b`, make sure there's the right spacing
for a rel.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3547
2013-08-14 17:39:24 -07:00
Emily Eisenberg
0a3a2271f4 Add ability to calculate heights/depths
Summary:
This diff does a couple different things:

 - There is now a metrics/ folder, which contains the property files describing
   the metrics if the fonts, as well as a script for reading and printing the
   metrics in javascript.
 - Fractions and superscripts/subscripts are now rendered in slightly different
   ways now (notably, no use of inline-table). This allows for much more precise
   positioning of the superscripts, subscripts, numerators, and denominators,
   while still having an appropriate baseline. Also, there is no longer a
   sup/sub/supsub distinction, there are only supsubs with null sup/sub.
 - Using the new font metrics and by implementing the formulas found in The TeX
   Book, Appendix G, the heights and depths of all of the sub-expressions in a
   formula are now calculated. These are currently used to:
    - Correctly position superscripts, subscripts, numerators, and denominators
    - Adjust the height and depth of the overall expression so it takes up the
      appropriate space
 - Because we have to add attributes (height and depth) to every attribute, I
   have changed the way DOM nodes are assembled. Now, instead of assembling the
   DOM elements inline (which is a problem because we need to track
   height/depth, and we shouldn't (and can't in IE 8) attach raw attributes to DOM
   nodes), we assemble a pseudo-DOM structure with the extra information, and
   then actually assemble it at the very end.

The main page also now has an updated expression to show off and test the new
and improved parsing.

Test Plan:
View the main page, make sure that the expression renders. Make sure
that the tests pass. Make sure that expressions have the correct calculated
height (this is most easily tested by viewing them on the main page and making
sure that the top of the expression lines up with the bottom of the input box).

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3442
2013-08-13 18:16:43 -07:00
Emily Eisenberg
7e5e6a90d3 Add the \KaTeX command
Summary:
Add a command to print out a TeX-style logo for KaTeX. I think the
code is okay, but suggestions for the design of the logo are most welcome (I'll
post screenshots soon).

Test Plan: Make sure \KaTeX renders, and the tests still succeed.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3443
2013-08-09 20:51:05 -07:00
Ben Alpert
cab5af40b6 Always throw real ParseErrors
Also sneak in a colon.

Auditors: emily
2013-07-28 17:54:41 -07:00
Emily Eisenberg
79a50b3abe Make the linter happy
Auditors: alpert
2013-07-24 20:37:56 -07:00
Ben Alpert
dd8923deca Dollar sign
Auditors: emily
2013-07-20 18:23:15 -07:00
Ben Alpert
41ad81d831 Make uppercase Greek letters use roman font
Fixes T1228.

Test Plan:
In `\Omega \omega`, Omega is roman and omega is italic.

Auditors: emily
2013-07-18 22:55:43 -07:00
Emily Eisenberg
383ca01434 Sync with Khan-exercises
Summary:
Add some things in to make this more useful to khan-exercises.
Notably, make KaTeX.process create and clean up its own .katex node, and rename
.mathmathmath -> .katex.

Test Plan: Make test, make sure the main page still renders stuff

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3061
2013-07-17 15:43:55 -07:00
Ben Alpert
c08fadfaa9 Parse single superscripts and subscripts
Summary:
Add the ability to parse lone `^x` and `_y`, etc. This basically just
involves more checking of cases in the increasingly ugly `parseAtom` function.
Also, now we manually check for the cases of double superscripts and
subscripts.

Test Plan: Make sure the tests pass. Make sure things like `^x` and `_y` parse.

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3095
2013-07-16 22:00:54 -07:00
Emily Eisenberg
975234e813 Fix some minor things
Summary: Fix llap/rlap again, fix lexing errors

Auditors: alpert
2013-07-15 12:12:15 -07:00
Ben Alpert
09311de814 Add Greek letters
Summary: Fixes T1190.

Test Plan: `\alpha \beta \gamma \delta \epsilon \varepsilon \zeta \eta \theta \vartheta \iota \kappa \lambda \mu \nu \xi \omicron \pi \varpi \rho \varrho \sigma \varsigma \tau \upsilon \phi \varphi \chi \psi \omega \Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega`

Reviewers: emily

Reviewed By: emily

Maniphest Tasks: T1190

Differential Revision: http://phabricator.khanacademy.org/D3054
2013-07-14 22:54:01 -07:00
Ben Alpert
a5840aae4b Add more symbols
Summary:
', \angle, \circ, \gets, \infty, \leftarrow, \prime, \rightarrow, \times, \to

Fixes T1191.

Reviewers: emily

Reviewed By: emily

Maniphest Tasks: T1191

Differential Revision: http://phabricator.khanacademy.org/D3053
2013-07-14 22:53:43 -07:00
Ben Alpert
e29fe0c683 Grosser color support for prettier results
Test Plan: `\orange{x}\blue{-4}`, `\orange{x-}\blue{-4}` look the same after desaturation as `x-4` and `x--4`. In addition, `\orange{x}\blue{{-4}}` looks appropriately atrocious (i.e., has no space whatsoever).

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3050
2013-07-14 21:34:05 -07:00
Ben Alpert
e472b0ba9d Add TeX style support
Test Plan:
`\blue\frac12 + \frac{2(y-z)}{2+\frac1{7+\frac31}} \div \orange{\arctan x^{2+\frac43}_{2}} * 2^{2^{2^2}}` looks reasonable, as does
`\blue\frac12 + \dfrac{2(y-z)}{2+\frac1{7+\frac31}} \div \orange{\arctan x^{2+\frac43}_{2}} * 2^{2^{2^2}}`.

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3047
2013-07-14 16:55:46 -07:00
Emily Eisenberg
a831e94a51 Remove dependency on underscore
Summary:
Remove all uses of underscore. For the things we actually need, make a
"util" file and put them in there.

Test Plan:
Make sure the test still succeed, and that the main page still
works.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3043
2013-07-13 20:42:19 -07:00
Emily Eisenberg
0da85483c4 Remove Parser options usage
Summary: Now, have katex catch and print exceptions instead of the
parser

Auditors: alpert
2013-07-13 18:15:17 -07:00
Emily Eisenberg
20658a95b6 Add \ in front of functions in the parser
Auditors: alpert
2013-07-13 16:13:05 -07:00
Emily Eisenberg
4122fa2b99 Rewrite the parser
Summary:
Make our own parser that doesn't use jison, so that we can handle
funny TeX syntax, and to make it smaller.

Test Plan: Make sure the tests pass with the new parser.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3029
2013-07-12 23:16:30 -07:00