
KaTeX – Fast math typesetting library for the web - tosh
https://katex.org/
======
bfirsh
We tried using KaTeX for [https://www.arxiv-vanity.com](https://www.arxiv-
vanity.com) but unfortunately it doesn’t support a lot of commonly used stuff.
I forget what most of that was now... over/underbrace is one I recall.

In the end we used MathJax pre-rendered on the server, which is the same speed
as pre-rendered KaTeX: [https://github.com/mathjax/MathJax-
node](https://github.com/mathjax/MathJax-node)

We’ve also done lots of tweaking to make it support as much stuff on the arXiv
as possible. Our MathJax config is a fun read: [https://github.com/arxiv-
vanity/engrafo/blob/master/src/conv...](https://github.com/arxiv-
vanity/engrafo/blob/master/src/converter/math.js)

~~~
est31
Wow, TIL about arxiv-vanity. On a first glance, it's awesome. Not saying that
I particularly dislike PDF files, but it's still good to have choice. Already
the ability to extract images from PDF files is incredibly valuable to me. I
wasn't aware how to do it properly. Screenshots are limited at the display
size and when you are zooming you don't know when upsampling algos of the PDF
reader set in and PDF readers often have maximum zoom levels and if the actual
resolution of the image is above that, you still won't get the full
resolution. Now I can extract images at the full resolution via browser
tools... I hope it's actually the full resolution, isn't it? It's kinda not
what arxiv vanity was made for but still, thank you for that!

~~~
bfirsh
Bitmapped images should be full size. Extracting proper vector graphics is a
work in progress: [https://github.com/arxiv-
vanity/engrafo/issues/247](https://github.com/arxiv-vanity/engrafo/issues/247)

If you want to extract figures/images from PDFs, the best way to do it is to
use Adobe Illustrator (or equivalent... Inkscape maybe?). That’ll let you
extract full sized bitmapped graphics or arbitrary parts of the PDF as SVG
vector images.

------
stabbles
What's cool about KaTeX is that you can integrate it nicely with static
website generators so that you can skip the latex parsing on the client side;
the client only has to load a font and some CSS, no JS required. I've used
that in a blog here:
[http://stoppels.blog/posts/gsoc-2017-summary](http://stoppels.blog/posts/gsoc-2017-summary)

~~~
svat
MathJaX has server-side rendering too. The speed differences (for the reader)
vanish once you're no longer doing everything client-side.

------
ktpsns
This is fantastic. A real concurrent to mathjax, which is notoriously slow on
pages like mathoverflow - exactly the shown example :-)

Even better, both are open source. Amazing, thanks for all of that, lovely
world!

~~~
raphlinus
A huge amount of the speed problem in MathJax is just the default
configuration, which does the math typesetting in a deferred callback and
throttles the work, basically guaranteeing that the browser will go through a
full relayout and repaint many times if there are lots of math on the page.
It's possible to change this, but I don't think many people do.

Another reminder of the power of defaults.

~~~
enriquto
Can you please elaborate a bit more on that? How can you configure it to be
faster? I'm considering leaving MathJax for pre-rendered png because it is
unusably slow (with the default configuration at least). My use case is a
whole book of math on a single html page. Pdf viewers have no problem with
that. Yet, firefox+mathjax cannot cope.

~~~
raphlinus
Yes, you want to set processSectionDelay to 0. Here's an example:
[https://github.com/raphlinus/ghilbert/blob/master/src/htmlou...](https://github.com/raphlinus/ghilbert/blob/master/src/htmlout.rs#L162)

------
fxfan
Side:

I would love to see a Macro system on top of TeX which has non-fixed-width
page as it's base. And generate dvi's that I can convert to epub.

It won't directly be on top of TeX because TeX itself assumes fixed width but
except for the width related directives, all directives would be shared with
TeX. Basically everything would be such that existing TeX documents can be
converted with little work.

~~~
svat
Martin Ruckert (of MMIX fame) has been working on something like this:
[http://tug.org/practicaltex2018/slides/ruckert.pdf](http://tug.org/practicaltex2018/slides/ruckert.pdf)
/
[https://www.tug.org/TUGboat/Contents/listauthor.html#Ruckert...](https://www.tug.org/TUGboat/Contents/listauthor.html#Ruckert,Martin)
It seems he has a talk at the upcoming TUG conference to follow up on this.

~~~
jimhefferon
For folks who don't know, that's the TeX Users Group. Worth checking out.

------
s9w
I used KaTeX for a project of mine a couple of years back. It's fantastic!
It's vastly faster than MathJax. Also, and maybe more important: It has a
render('latex-string') function. At least way back, MathJax did't have that,
and was very opposed to adding one, making it a pain to work with.

------
tabbott
We've been really happy using KaTeX as the LaTeX rendering engine for Zulip;
it's super fast, and the fact that you can do the rendering on the backend
(and just serve HTML + CSS at display time) is really nice.

------
anatoly
If both mathjax and katex are open source, what is it that makes katex so much
faster? Is there a hidden tradeoff?

~~~
megalodon
Just because they are both open source does not mean they share a common goal
or methodology. My guess is that KaTeX was written to overcome the
deficiencies of mathjax.

------
burtonator
We're planning on adding KaTexX into Polar so that people can annotate their
notes with mathematics forumlas:

[https://getpolarized.io/](https://getpolarized.io/)

Apparently it's supported with a summernote plugin but I haven't yet had time
to implement it...

------
tu7001
This is a great stuff!

------
enriquto
I am very impressed by the speed, but the typographic quality is notoriously
ugly. Does it need to be that way? Does it happen only for the interactive
demo? The screenshots look a bit better.

~~~
kevinb7
KaTeX strives to match LaTeX's behavior so it may actual be an issue with the
screen readability of LaTeX. We're currently discussing ways to improve this
in
[https://github.com/KaTeX/KaTeX/issues/1848](https://github.com/KaTeX/KaTeX/issues/1848).
If you're seeing other issues please file a bug report.

------
graycat
I am a heavy TeX user and great fan of TeX, just Knuth's TeX with just his
Plain.tex macro package, just as in his _The TeXBook_. Great fan.

No, I don't use LaTeX -- (a) I would no longer know how to write macros, (b)
there is more to learn, (c) there is much more documentation to read, and (d)
IMHO the LaTeX documentation is not written nearly as well as Knuth's _The
TeXBook_. So, net, I don't use LaTeX.

Sorry, guys, there's no law saying I should get to TeX with a graphical user
interface (GUI) with "what you see is what you get".

In fact, one of the things I like most about TeX is that I don't have to use a
GUI. Instead, I type into TeX the same way I type as much of my writing as I
can, into my favorite programmable text editor KEdit for which I have 200+
macros, many to help with typing TeX.

I'm happy as a clam. KEdit's functionality with my macros lets me get my work
done with blinding speed, much faster than with any GUI which is "what you see
is all you've got" and _direct manual manipulation with no hope of automation_
\-- bummer.

If I had to use TeX only via a GUI, I'd scream bloody murder loudly enough to
blow off the roof of my house and the other houses in the neighborhood.

To each his own. A good GUI for TeX could be a lot of work. Sorry, guys, I
just want to type into KEdit.

~~~
tomsmeding
Are you replying to the wrong post? This is about a JS library for typesetting
a small subset of the TeX formula macro's for inclusion in a webpage.

And besides, you say one of the reasons why you aren't writing LaTeX is
because you like to work in a plain text editor without any extra graphical
capabilities. I'm not saying you must use LaTeX -- far from it, but I do want
to say that it's not an argument to use plain TeX: I'm happily writing my
LaTeX documents in Vim, no problems whatsoever.

~~~
graycat
Okay, it's only for a small subset of TeX for output on a Web page. My guess
was that it was a GUI for all of TeX with output a Web page.

Then that would be a GUI for typing in TeX for math, and I prefer to type in
TeX for math with just a good text editor. I'm on Windows, using my favorite
text editor KEdit, close to IBM's old editor XEDIT, done by an IBM guy in
Paris WITHOUT support of IBM's management, instead of Linux, but I've heard
that VIM and Emacs are really good text editors.

At times I've included TeX output in some Web pages: I just format the TeX
with a lot of magnification in TeX (e.g., \magnification=1800), have TeX
format the TeX input that I typed, have my favorite program for displaying a
TeX output DVI file put the results from TeX on the screen, capture the
screen, use the old Microsoft PhotoDraw to crop the screen capture and make a
GIF file, and use the GIF file in a blog post and, thus, a Web page.

BUT!!!! There is much more to TeX than just a displayed expression for, say,
the inverse Fourier transform in _display mode_ , and I see no hope getting
HTML, CSS, and JavaScript to do well with all or even nearly all of TeX.

But PDF can do well with TeX output. So, net, if want to put TeX formatted
math on the Internet, then it seems there are just two main approaches: (1)
Just do something clumsy, slow, and small scale like I do and just described,
i.e., a screen capture or (2) use PDF, displayed by code in the Web browser or
Adobe's Acrobat or whatever -- just send PDF. Okay, so the OP here is for a
nicer approach to what I do with (1). Okay.

On LaTeX, for your

> And besides, you say one of the reasons why you aren't writing LaTeX is
> because you like to work in a plain text editor without any extra graphical
> capabilities. I'm not saying you must use LaTeX -- far from it, but I do
> want to say that it's not an argument to use plain TeX: I'm happily writing
> my LaTeX documents in Vim, no problems whatsoever.

No. A GUI editor would not motivate me to use LaTeX either. Instead, I don't
use LaTeX because:

> (a) I would no longer know how to write macros, (b) there is more to learn,
> (c) there is much more documentation to read, and (d) IMHO the LaTeX
> documentation is not written nearly as well as Knuth's The TeXBook.

For (a) I would no longer know how to write macros for TeX because I would not
know how to coexist peacefully with the tweaks to TeX that permitted the extra
features of LaTeX. E.g., I was in the TeX Users Group, TUG, from the beginning
of TeX, way back there, and recall the remarks of Knuth on the tweaks to TeX
to permit good mathematician Leslie Lamport to do LaTeX.

My take on LaTeX versus TeX: In TeX, often must essentially write software,
code, must program, to get what want. E.g., with an analogy, can program in C,
and the program can automate the baking of cookies, but there is nothing in
the documentation of C from Kernighan and Ritchie about baking cookies and
instead have to program the cookie part. Looking at the C code without the
comments, won't see much of anything about cookies unless have used lots of
identifiers, e.g., variable names, with mnemonics about cookies. So, have to
write C code while thinking about cookies.

With this analogy, LaTeX is closer to a language about automating making
cookies. E.g., some of my TeX macros have to do with tables of contents, cross
references, numbering sections and subsections, references to entries in the
bibliography, annotation of figures (bring in a BMP file but overlay TeX
output on the figure), various cases of boxes, e.g., for foils, etc. Looking
at my corresponding TeX macros, it just looks like code. E.g., I've
implemented a push down stack for _automatic_ storage, etc. TeX has some nice
block structure features, something like in Algol or PL/I -- at the end of the
execution of the block, as in Vegas, what happened in the block stays in the
block or is just forgotten.

Well, I can believe that LaTeX has macros, commands, keywords, functions,
features, whatever for the LaTeX version of all of those TeX macros I wrote.
There is likely some variety and flexibility for the LaTeX versions.

So, with the analogy, with LaTeX, if I want cookies, then I get to type in
LaTeX commands obviously about cookies and not just code that doesn't mention
cookies.

AND I will have to read and understand the big books, at least two, on LaTeX,
each book much bigger than Knuth's _The TeXBook_. Read more big books on
software ....

But if I want very different cookies, or pizza, then I might have to write TeX
macros as I've always done: THEN I might have to understand how early on Knuth
tweaked LaTeX in ways done after _The TeXBook_ ; there I see big chances of
software mud wrestling. I didn't have to mud wrestle with TeX: Knuth was very
clear and precise; all I had to do was understand _The TeXBook_ , and I did.

Or LaTeX does me a lot of favors, likely its way, how it believes I should do
things. No doubt for nearly all new users of TeX, LaTeX is much easier than
the TeX macros I wrote.

But maybe in some cases how LaTeX does things is not how I want to do things.

Besides, just with TeX can write not just math papers but whole books, books
that look nice in all respects. And for big, technical books, will want cross
references, and my TeX macros have those, too.

So, for me, TeX is plenty good enough, solves the problem, has excellent
documentation, is essentially totally free of bugs, and, did I mention, I'm
happy as a clam.

So, I love TeX but don't use LaTeX, and don't want a GUI for TeX.

I can understand that for just some start on getting math into HTML pages, the
OP, MathJax, or whatever, is a big help.

I have a friend who for years tried to get me to write software to generate
HTML code from TeX input. I told him, TeX is a programming language, complete
with if-then-else, do-while, expressions, assignment statements, etc. and HTML
is not and is just a markup language. So in principle can't translate TeX
input to HTML. But there is something that can be done: Just generate the TeX
output as graphics, that is, BMP, GIF, PNG, or TIF files, put those in HTML as
just images, and call it done. Or better yet, put the TeX output in a PDF file
and call it done.

Broadly I'm sorry: As best I can tell, it looks like Tim Berners-Lee when he
designed HTML had seen and understood TeX and other _markup_ languages.
Soooooo, why didn't he design HTML to be as good for math as TeX? Heck, he was
at CERN with lots of physics and math. Well, a guess is that he wanted HTML to
be much simpler than TeX, that the extra difficulties of TeX would be of
interest to less than 1% of his target users of HTML. So HTML took over the
world, maybe trillions of HTML Web pages out there. Then, now so many years
later, that 1% wants some math in HTML. Uh, sorry 'bout that! Sounds like they
have been happy with a Toyota Camry for 20+ years and suddenly now want
something different, maybe either a Lamborghini or a Chevy 3500 Silverado.
Sorry 'bout that.

So, to summarize, for some math in a Web page, can:

(1) Can do what I do with screen captures of TeX output and get some displayed
equations but still are a long way from getting all of TeX.

(2) Get a GUI for much of what I do with (1) and have the work easier.

(3) Send a PDF file of the output of essentially anything from TeX or LaTeX.

But really doing math in HTML and as good as TeX, not so easy. And for a GUI
for TeX, people have tried that, but personally I don't want a GUI for TeX.

