nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.41k stars 1.47k forks source link

Implement Latex math formulas in docgen #22657

Open a-mr opened 1 year ago

a-mr commented 1 year ago

Summary

We don't have a feature to type mathematical formulas in our markup language.

RST, Github Markdown, Pandoc Markdown all support the display of Latex formulas.

Description

For Markdown mode we can adopt the syntax shared by Github extension and Pandoc extension.

Inline $\sqrt{3x-1}+(1+x)^2$
Block:
$$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)$$

Inline $\sqrt{3x-1}+(1+x)^2$ Block: $$\left( \sum_{k=1}^n a_k bk \right)^2 \leq \left( \sum{k=1}^n ak^2 \right) \left( \sum{k=1}^n b_k^2 \right)$$

Need to implement syntax in parser (rst.nim) and output generation (rstgen.nim).

For HTML rendering I suggest to make implementation based on Katex as the easiest option (MathML is incomparably more difficult).

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

Araq commented 1 year ago

The problem with LaTeX's syntax is that even though math formulas are in a hidden tree structure (AST) LaTeX's representation is not an AST. It's unnecessarily losing information and is overly concerned with rendering aspects, quite ironically.

a-mr commented 10 months ago

The problem with LaTeX's syntax is that even though math formulas are in a hidden tree structure (AST) LaTeX's representation is not an AST. It's unnecessarily losing information is overly concerned with rendering aspects, quite ironically.

The explicit hierarchical representation is great only for specific purposes like efficient parsing or programmatical tree modification. For text editing of formulas it just should be concise and hence readable, and Latex is good at it. And hierarchical essence is well reconstructed by human brain when one looks on the rendered formula, no need to reproduce that in text. The math community has been using Tex/Latex for decades, and doesn't complain about its math syntax.

Araq commented 10 months ago

For text editing of formulas it just should be concise and hence readable, and Latex is good at it.

There is nothing particularly readable about Latex's syntax and I don't even want to know how many bugs it caused in published papers.

The math community is happier with Mathematica which keeps the tree structure.

WangWei90 commented 10 months ago

How to write Mathematica expression in some text file(e.g. sth.txt or sth.tex) and compile-render it in PDF.

a-mr commented 10 months ago

The math community is happier with Mathematica which keeps the tree structure.

There is no ready to use renderer for Mathematica formulas. And Latex is a de facto standard.

Your comments are valid but they don't propose a meaningful strategy.

The situation is similar to the choice of Markdown as default language. W.r.t. e.g. HTML both RST and Markdown do not manifest explicit tree structure in many parts, but Markdown is even worse:

But there are no well-established alternatives to Markdown, so it's OK to adopt some of these things that have some use or are familiar to use. Nim project is not required to solve all the problems of programming world.

Alternatives may become ready only in long time like 20 years, then only then there will be the time to switch to them.

Araq commented 10 months ago

Whatever syntax we implement should map easily to MathML. It can be a clean subset of LaTeX or it can be the full thing if there is a reasonably complete list of its quirks somewhere.