Skip to main content

Use MathJax (Latex or AsciiMath) in your AsciiDoc projects!

Project description

// Header
# adoc-math
:toc: macro

// Links
:example: https://github.com/hacker-dom/adoc-math/raw/main/example/adoc-math-example.pdf[Example]
:adoc: https://docs.asciidoctor.org/asciidoc/latest[AsciiDoc]
:markdown: https://daringfireball.net/projects/markdown/[Markdown]
:latex: https://www.latex-project.org[LaTeX]
:adoctor: https://github.com/asciidoctor/asciidoctor[Asciidoctor]
:adoctor-pdf: https://github.com/asciidoctor/asciidoctor-pdf[Asciidoctor-Pdf]
:adoctorjs: https://github.com/asciidoctor/asciidoctor.js[Asciidoctor.js]
:adoc-stem: https://docs.asciidoctor.org/asciidoc/latest/stem/[AsciiDoc STEM]
:adoctor-pdf-stem: https://docs.asciidoctor.org/pdf-converter/latest/stem[Asciidoctor-Pdf STEM]
:mathjax: https://github.com/mathjax/MathJax-src[MathJax]
:katex: https://github.com/KaTeX/KaTeX[KaTeX]
:adoc-math: https://github.com/hacker-dom/adoc-math[adoc-math]
:adoctor-math: https://github.com/asciidoctor/asciidoctor-mathematical[asciidoctor-mathematical]
:amath: http://asciimath.org[AsciiMath]

Use MathJax (Latex or AsciiMath) in your AsciiDoc projects 🤟🚀

toc::[]

## 📝 {example}

## 📝 Installation

adoc-math has zero depependencies! So it's fine to install it globallyfootnote:[Theoretically, the only time this could cause issues is if you have another package which has the name adoc-math (it obviously has to have a different PyPI name, because adoc-math is already taken 😛. But this is not very likely.. )] 😛

[source,bash]
----
pip3 install --user --upgrade adoc-math
adoc-math-setup # will call `npm i -g mathjax@3` and `npm link`
----

## 📝 Overview

### 🔍 Background

I think of {adoc} as a markup syntax somewhere between {markdown} and {latex}. It originated with a https://github.com/asciidoc-py/asciidoc-py[Python implementation], but afaik that isn't actively developed, and the reference implementation is {adoctor} in Ruby.

{adoc} allows you to write a document and then output it in:

* html ({adoctor})
* pdf ({adoctor-pdf})

and many other formats! There is even an {adoctorjs} version (an automated translation of the Ruby code to JavaScript).

### 🔍 LaTeX
Putting LaTeX equations in other places than a TeX document is not so easy. There are two main libraries for this:

* {mathjax}
** It uses native browser fonts and a lot of Css to replicate {latex} in the browser.
* {katex}
** Similar to {mathjax}, built by Khan Academy.

### 🔍 STEM
STEM stands for Science, Technology, Engineering, Mathematics, basicaly {latex}. There are two sections in the {adoc} documentation on STEM:

* {adoc-stem}
* {adoctor-pdf-stem}

TLDR:

* In {adoctor} (i.e. Html output), you can include math with `stem:[x+y]`. In the browser, {mathjax} is used to render the math, and frankly, it looks beautiful.
* Since {mathjax} uses browser fonts and Css, it doesn't work in Pdfs. There is an official {adoctor-math} package that provides this support. However, it is extremely quirky, and the ouput doesn't look very good (see a comparison of {adoc-math} and {adoctor-math} in the {example})
** Some more references:
*** https://github.com/asciidoctor/asciidoctor-mathematical/issues/45

### 🔍 Architecture

That's where `adoc-math` comes in! I decided for:

* a Python package that searches for naturally-looking latex cells (e.g. `$a+b$`), calls {mathjax} to create an svg, and replaces the cells with an image of the svg

I couldn't use {katex} because only {mathjax} has an Svg output (see https://github.com/KaTeX/KaTeX/issues/375).

Unfortunately, {mathjax} 3 doesn't come with a Node CLI package like https://github.com/mathjax/mathjax-node-cli/[MathJax 2]. So I implemented xref:./adoc_math/d_mathjax_wrapper.js[a wrapper] over the library.

### 🔍 Usage

[cols="2*"]
|===
| Inline cells:
a|
----
$x + y$ [...options]
----

| Block cells:
a|
----
$$ [...options]
x + y
$$
----
|===

For more examples, see the {example}.


## 📝 FAQ

> Why isn't `adoc-math` written in Ruby?

I don't speak Ruby 😞 If you would like to translate this library to Ruby, or at least an AsciiDoc macro that can get replaced by an image, so we cant get rid of the extra metacompilation step, I'd be more than happy to help!

> What about Windows?

I tried to be conscious of non-Posix platforms, but haven't tested in on Windows. Any behavioral discrepancies would be considered valid issues.

> Can I reference a cell, or add a caption to a block cell?

Yes! Check out the {example}.

> It's annoying having to uncomment the source math to edit it.

You can use a `pre-post` pattern. `pre.adoc` will be your source code, and `post.adoc` will be the output of `adoc-math` / input to `asciidoctor(-pdf)?`. Run `cpy pre.adoc post.adoc` before every invocation to `adoc-math`.

> How come inline cells become part of the sentence when they are on a separate line?

In {adoc}, you need to separate two blocks with at least one _empty_ line. 🙂

> Does `adoc-math` work with an Html output?

This first version is geared towards Pdf output. Happy to add more powerful support for Html outputs in the future (e.g., just use the native `stem:[]` macro for Html, so we can use basic {mathjax} with browser fonts and Css (instead of svgs)).

> Can I use a different font?

{mathjax} currently http://docs.mathjax.org/en/v3.2-latest/output/fonts.html[doesn't provide support for multiple fonts].

> Can I make my math thinner/thicker?

The created svgs have a property called `stroke-width` that can adjust this. Unfortunately, it is currently set to 0, so it is not possible to make it thinner. In theory it should be possible to make it *thicker* by increasing that value. xref:./adoc_math/e_svg_transforming.py[svg_transforming.py] would be the place for that; or create an issue and I'll add it.

## 📝 Debugging

> I get a MODULE_NOT_FOUND error.

MathJax probably cannot be found. Try running `adoc-math-setup`.

> My AsciiMath fractions are too large!

It seems that {amath} interprets fractions in `displaystyle` rather than `textstyle` (`\dfrac{}{}` rather than `\tfrac{}{}` or even `\frac{}{}`, see https://tex.stackexchange.com/a/135395/31626[StackExchange]).

I haven't found a good solution to this yet. If you have any ideas, please let me know! Note that if you have a singleton fraction (`$a/b$ amath`) you can scale it down with `$a/b$ amath, scale = 60%` (or just use `tex`).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adoc-math-1.0.9.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

adoc_math-1.0.9-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file adoc-math-1.0.9.tar.gz.

File metadata

  • Download URL: adoc-math-1.0.9.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for adoc-math-1.0.9.tar.gz
Algorithm Hash digest
SHA256 6b9157f7095966b70db84f4b163c332ad99c7ee27d46cc8889a4867890a91f97
MD5 0197a5bde6887d43bd34f07ea784c68a
BLAKE2b-256 6c80c5e385b805438abc4941738e4a71aa915fd5f104802a364af53a95f80efd

See more details on using hashes here.

File details

Details for the file adoc_math-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: adoc_math-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for adoc_math-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 65f7d004df86cd67b58d69a2b50e8d233bc27666b4106604b099a75b3bffecce
MD5 af3750fbab5d490d27483dec8ca94ca9
BLAKE2b-256 bb2c89ffc79e4eb984f9ae5e467cd0526f3bfbdacebc008108f4f0d5d122436d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page