Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mathjax to header so docs can use latex with the usual delimiters #9

Merged
merged 2 commits into from
Feb 3, 2020

Conversation

glangmead
Copy link
Contributor

This is one way to enhance the docs with LaTeX. The authors of mathlib can simply add math in the comments with the usual delimiters. These pass through into json_export.txt (escaped appropriately for json), and then directly into the HTML. I confirmed this end to end. The only change I had to make was to add MathJax to the header of each page, so it can find the delimited math and render it.

Copy link
Member

@robertylewis robertylewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I think the ideal for @PatrickMassot and others would be "full" TeX support in the docs, i.e. the ability to draw diagrams and such. But MathJax is a big and apparently easy step toward that.

print_docs.py Outdated
<script>
MathJax = {{
tex: {{
inlineMath: [['$', '$'], ['\\(', '\\)']]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC Zulip (and other LaTeX/makdown combos?) don't use single $ delimiters for inline MathJax. It results in a lot of false positives. I'm not sure how big a worry it is here. Is MathJax smart enough to avoid inserting itself in code blocks f $ g $ h a? If not, double $$ is safer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to see how the docs render with this patch in place? I.e., can we test what happens?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://robertylewis.com/mathlib_docs/ is a build with @glangmead 's changes enabled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something funny is going on here. I don't think the parens are escaped in the markdown. Does ['\(', '\)'] mean that everything in parens will become Mathjax??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should mean that \( x + \sqrt{y + z} \) is parsed as mathjax. So the \ has to be explicitly in the source before the parens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MathJax does exclude itself from <code> blocks and you can configure it to exclude more css classes. I'll test that out.

Copy link
Member

@robertylewis robertylewis Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Edit - oops, you beat me to it...) For what it's worth, MathJax is already smart enough to avoid running in <pre> or <code> blocks. So $ occurrences in Lean code shouldn't trigger it, unless someone has forgotten to put backticks around it. So maybe the simple solution is to keep single $ and remove \( as delimiters. I doubt there are many if any plain text lines in mathlib that have two $s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gosh is there an existing bug with parens? See https://leanprover-community.github.io/mathlib_docs/geometry/manifold/real_instances.html where the definition of euclidean_space in lean is def euclidean_space (n : ℕ) : Type := (fin n → ℝ) but on the web some of it is deleted: def euclidean_space : ℕ → Type.

This isn't a bug. The web docs are only displaying the type, which is ℕ → Type. The argument doesn't have to be bound to print Type, but in the source it's referenced in the body of the definition (fin n → ℝ).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is maybe a badly phrased doc string though. It should "informally bind" n, i.e. "euclidean_space n is the space ℝ^n."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned a few things and will update the PR shortly. First, I've swung back to wanting dollar signs and NOT the parens or brackets, since the latter do not pass cleanly through markdown. Stackoverflow has the same problem, see https://math.meta.stackexchange.com/a/18714. So: only single and double dollars should be processed, which will be what most people guess they should use anyway.

Further, I've added all the lean-related css classes Rob defined in the python script as exclusions for MathJax. Some of those do not also have pre or code around them, so this should help keep MathJax out of the Lean stuff entirely. It can be further tweaked later of course!

@@ -268,6 +268,18 @@ def html_head(title):
<link rel="shortcut icon" href="https://leanprover-community.github.io/assets/img/lean.ico">
<title>mathlib docs: {1}</title>
<meta charset="UTF-8">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this polyfill thing? Is there a reason to grab mathjax from there? Should we self-host a copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MathJax docs said to put both these inclusions which surprised me. Polyfill is apparently there as a backoff for "older browsers". I don't know how necessary it really is, I'll find out. As for self-hosting I don't know all the tradeoffs. The authors of mathlib don't need to care of course.

Copy link
Contributor Author

@glangmead glangmead Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs http://docs.mathjax.org/en/latest/output/browser.html say that polyfill is there to support IE11. The stats here https://www.w3counter.com/globalstats.php say that IE11 is ranked 8th with 2.3% of... whatever they're counting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for explaining. In that case, I'm fine with using polyfill.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the question. I only followed up with that research once you asked, having merrily copied and pasted it.

… will/won't process; add links to relevant documentation to explain these
@glangmead
Copy link
Contributor Author

Attaching a zipped version of real_instances.html, where I have a test comment

# Constructing examples of manifolds over dollar $\mathbb{R}$ or double dollar $$\mathbb{R}$$. Or paren \(\mathbb{R}\) or bracket \[\mathbb{R}\]

and the rendered html attached here shows that dollars are processed by mathjax, double-dollars are processed and create display mode, and the paren/brackets are not processed by mathiax.

real_instances.html.zip

Happy also for @robertylewis to regenerate the docs so we can browse for issues again if desired.

@robertylewis
Copy link
Member

I've regenerated the docs, with a few examples of good behavior: https://robertylewis.com/mathlib_docs/tactics.html

I don't know of any natural occurrences of $..$ in the library to check, but I'm pretty convinced that this will work! We'll keep an eye out for any breakage.

@robertylewis robertylewis merged commit 8cb620c into leanprover-community:master Feb 3, 2020
@PatrickMassot
Copy link
Member

For the record, let me write that I think we should do server-side rendering here. There is no dynamic content in those pages, the doc generation script should call mathjax instead of asking all readers web browsers to do so. This allows opens up the possibility of having commutative diagrams or other fancier LaTeX. But I don't have time right now :-(

@glangmead
Copy link
Contributor Author

When you say "render", what output format do you have in mind? To me LaTeX is a markup language like HTML and MathML that the browser's job is to render. Because it's left as markup the browser can change the font, colors, do screen reading for accessibility etc.

@urkud
Copy link
Member

urkud commented Feb 18, 2020

@PatrickMassot AFAIK, MathJax outputs different HTML in different browsers, so it can only run in browser. At least it worked that way a couple of years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants