8 ms·
Implementing a “mini-LaTeX” in ~2000 lines of code
- PaulHoule 4y agonroff is probably a better comparison than LaTeX for this https://en.wikipedia.org/wiki/Nroff https://en.wikipedia.org/wiki/Nroff
- AlanYx 4y agoHe's doing something inspired by Knuth-Plass linebreaking, so either Heirloom Troff or Neatroff would be the better comparison.
- chongli 4y agoThis is fast becoming a pet peeve of mine: people conflating LaTeX with TeX. TeX is the macro language and the compiler, LaTeX is the library. Replacing TeX is pretty easy since it is a fairly small language. Replacing LaTeX, on the other hand, is very difficult because it contains a rather huge number of packages that have been developed over the decades.
- leephillips 4y agoNot quite. TeX is the compiler that produces DVI or PDF files and the typesetting language. It is also a set of macros that together are plain TeX. It is also all of this together, a typesetting system. The name is somewhat overloaded. LaTeX is not a library. It’s what is called a format in the TeX world; essentially another set of macros that create an alternative to plain TeX. Yes, there are a huge number of packages that are designed to work with LaTeX, but they are not part of LaTeX. Most distributions, such as TeXLive, include TeX, LaTeX, a large number of these packages, fonts, and much else.
- choeger 4y agoImportant distinction: TeX is an interpreter, not a compiler.
- code_biologist 4y agoWhy is that an important distinction? It's not clear to me that it is.
- kragen 4y agoSee https://news.ycombinator.com/item?id=32350841 https://news.ycombinator.com/item?id=32350841.
- Shorel 4y agoIt is not. And it is irrelevant, because the objective here is to produce good-looking documents, not to create or run programs.
- choeger 4y agoIt is. People get confused all the time when their "compilation" of a TeX "document" doesn't terminate or yields other typical runtime problems. LaTeX is a very leaky abstraction in that regard. For more than basic competence in using TeX, it is important to understand how it works internally and for that it is helpful to have an understanding of what an interpreter is.
- Shorel 4y agoI disagree. I don't need to know the compiled opcodes of a Java program to know that I wrote code that tries to perform a division by zero. What I need is a clear explanation of the issue using the actual source code I wrote. That's it. Whatever does this job, a compiler, an interpreter, random fairies, quantum AI overlords, it is not relevant. Clear error messages that show my source code, and explain the issue, that's the answer to fix these typical runtime problems.
- Koshkin 4y agoDoesn’t TeX emit a file like a compiler? You could say that a PDF reader, say, is an “interpreter,” but TeX itself? I don’t think so.
- thangalin 4y ago> Replacing TeX is pretty easy since it is a fairly small language. Conceptually, easy. In practice, it can go sideways. Take JMathTeX, for instance: http://jmathtex.sourceforge.net/ http://jmathtex.sourceforge.net/ The code had (has?) a particularly nasty bottleneck. An exception is thrown while parsing for every single character in a macro name until the macro name resolves to a known TeX command. In Java, throwing an exception requires filling out a complete stack trace. I forked the project to fix that issue and then optimized the font glyph to SVG path conversion algorithm. The TeX library went from non-real-time rendering of a few formulas to real-time rendering of 1,000 formulas. https://github.com/DaveJarvis/JMathTeX https://github.com/DaveJarvis/JMathTeX Here's a screenshot showing the JMathTeX fork integrated into my text editor, KeenWrite: https://github.com/DaveJarvis/keenwrite/blob/master/docs/screenshots.md#equations https://github.com/DaveJarvis/keenwrite/blob/master/docs/scr... See also the build instructions for Knuth's original TeX: https://tex.stackexchange.com/a/576314/2148 https://tex.stackexchange.com/a/576314/2148
- Shorel 4y agoBut... persistent memory TeX? Where you only change one letter and the entire document doesn't need to be rendered again? Just that letter? A way to make TeX work more like WordPerfect 5.1. That's what I want.
- ngcc_hk 4y agoAny real life editor like this will be great… any?
- Shorel 4y agoThe closest I know is TexMacs. The name is a bit misleading, as this is not TeX. http://www.texmacs.org/tmweb/home/welcome.en.html http://www.texmacs.org/tmweb/home/welcome.en.html
- thesz 4y agohttps://news.ycombinator.com/item?id=19473910 https://news.ycombinator.com/item?id=19473910 It talks about CSS layout algorithms, but TeX also does layout. The result: what you ask is very hard. The single letter change can result in changes throughout entire document. Figuring out and keeping dependencies in not easy.
- mhd 4y agoIt's interesting to look at the early issues of the official TeX publication Tugboat[1]. The first version of TeX, TeX78, written in the SAIL language was just out and there was a concerted effort to produce a more portable version in Pascal, TeX82 (IIRC already known under that name during production). The first issues of Tugboat then contained several reports about early TeX being ported to various architectures. That often meant rewriting the whole program, as basically no other system besides Knuth's "native" time-sharing OS had SAIL. So there was a port to a (Z80-based) Unix, someone rewriting parts in Fortran, etc. If they were tied to a specific backend/printer, there wasn't even a need to port Metafont. TeX was relatively small. That often gets lost in contemporary huge LaTeX distributions. [1]: https://www.tug.org/tugboat/ https://www.tug.org/tugboat/
- _gabe_ 4y agoI thought one of the reasons Knuth made TeX was because there were no good math typesetters available. I have no idea if this assumption is correct, but one of the main reasons I've resorted to using LaTeX in the past is for the math typesetting. I haven't found any good alternatives, and it seems incredibly complex compared to basic kerning + line breaks and/or justification for regular English-like text layout. So if this implementation lacks math typesetting, and that was one of the motivating factors behind the original TeX implementations, then I think this code is missing a pretty complex core feature.
- cpach 4y agoThat was indeed one of Knuth’s primary goals. But perhaps it wasn’t one of Pakkanen’s goals though.
- _gabe_ 4y agoFrom the authors original post, it looks like you're right in saying that was not one of his goals. This article says: > Thus we can reasonably say that the code does contain an implementation of a very limited and basic form of LaTeX. Which is what I'm primarily debating. I feel like a basic LaTeX implementation would at least attempt mathematical typesetting, but that could be debated :)
- mhd 4y agoI think TeX is just used eponymously for typesetters, as runoff, scribe, lout etc. aren't as popular anymore, to put it politely.
- db48x 4y agoMath typesetting was a goal, but it wasn’t really the main goal. Knuth started TeX because the proofs for TAOCP Volume II were done with early phototypesetting equipment, and the results were terrible. Not just terrible for math, but very poor even for just regular prose. The reduction in quality from the Linotype machines used for Volume I was unacceptable to him, but it would have been far too expensive to revive that obsolete typesetting process for one book, so he decided to write TeX instead. His goal here was to knock the whole typesetting process so far out of the park that he wouldn’t have to deal with this same problem again for Volume III, Volume IVa, Volume IVb, Volume Va, etc, etc.
- marcodiego 4y agoPosted 3 days ago: https://news.ycombinator.com/item?id=32307185 https://news.ycombinator.com/item?id=32307185
- seanwilson 4y ago> As you can easily tell, line breaks made at the beginning of the chapter affect the potential line breaks you can do later. Sometimes it is worth it to make a locally non-optimal choice at the beginning to get a better line break possibility much later. Evaluating a global metric like this can be potentially slow, which is why interactive programs like LibreOffice do not use this method. Is there not a fast "good enough" algorithm for justifying text vs a slow optimal one? Is it really infeasible for HTML to have justified text that looks something like LaTeX? It's sad that justified text still looks bad on the web with no signs of it getting better even with all the computing power we have now.
- tannhaeuser 4y agoAccording to previous discussions [1], none that'd fit the constraints of text rendering in browsers AIU (eg dynamic programming approaches considered infeasible on the web, hyphenation not really a thing). But who knows, CSS is incredibly bloated already ... [1]: https://news.ycombinator.com/item?id=28537923 https://news.ycombinator.com/item?id=28537923
- chucky 4y agoI once discussed this with a coworker who had worked a lot on various things related to typesetting, and he claimed the main issue with doing good text justification for the web is the lack of good, free hyphenation dictionaries (or rules) for most languages. In order to do full paragraph justification in a way that makes sense and always looks good you need to be able to hyphenate words dynamically (and fairly aggressively). LaTeX does come with hyphenation libraries for some languages, but web browsers would need wider support. There's also the question of how to standardize this, because you would have to ship hyphenation dictionaries for every language, and you would need to standardize this across browsers so they render pages the same. LaTeX does ship with hyphenation libraries for a bunch of languages, but if you try to use it for any of the minor languages, you'll find the results are so-so and you need to manually hyphenate words to get decent results (this was my experience a decade ago at least). All of the above could be solved, but sadly I suspect it wouldn't be worth the cost for the players that would have to be involved in solving it.
- groby_b 4y ago"The output contains some typographical niceties, so we can reasonably say that the code does contain an implementation of a very limited and basic form of LaTeX" has to be on the more optimistic side of overstatements. It's roughly akin to "cat can actually display content quite similar to a <pre> block, so it contains a very limited and basic form of a web browser". It's not entirely wrong. But mostly because it left "wrong" behind long ago and is in a different country now.
- ajross 4y agoLots of nitpickery about what exactly it means to be TeX here, that IMHO are sort of forest-for-the-trees argumentation. What's interesting here is that it's a blog post on how to do page-level typesetting, which was a subject of great interest and serious research in the 1970's but in the modern world has been mostly forgotten. And that's somewhat unique. Modern developers still occasionally worry about low level details like instruction architectures or I/O latency or packet dumps. The lessons of our ancestors about algorithm choice are still alive in our culture. But virtually no one gives any thought to how their text is laid out at a level of sophistication higher than the CSS box model. And that's kinda nice to see.
- svat 4y agoIn case there is interest in other implementations of TeX: I'm collecting the ones I know here: https://github.com/tex-other https://github.com/tex-other and have an answer here: https://tex.stackexchange.com/questions/507846/are-there-any-non-web-re-implementation-of-tex-core-in-recent-years/507930#507930 https://tex.stackexchange.com/questions/507846/are-there-any...
- planetis 4y agoI can't express how frustrating is editing TeX files. This archeology needs to die.
- nequo 4y agoI’m also frustrated by it. Do you know alternatives that can handle mathematical typesetting? I looked at Scribble[1] and Rmarkdown[2] but both seem to offer less flexibility for math, or just invoke LaTeX as is.[3] [1] https://docs.racket-lang.org/scribble/ https://docs.racket-lang.org/scribble/ [2] https://bookdown.org/yihui/rmarkdown/ https://bookdown.org/yihui/rmarkdown/ [3] https://bookdown.org/yihui/rmarkdown/markdown-syntax.html#math-expressions https://bookdown.org/yihui/rmarkdown/markdown-syntax.html#ma... Edit: If you're downvoting this, I'm genuinely curious to hear your thoughts.