asciidoctorasciimath

Not able to use asciimath using AsciidoctorJ


I am trying to convert a asciidoc file containing math expression to html using AsciidoctorJ, but have been unsuccessful so far.

This is the math.asciidoc that i am trying to convert.

= My Diabolical Mathmatical Opus
Jamie Moriarty

sample1

asciimath:[sqrt(4) = 2] 
stem:[sqrt(4) = 2]

I am using the below configuration in Asciidoc

Attributes attributes = AttributesBuilder.attributes()
            .math("asciimath")
            .get();

Options options = OptionsBuilder.options()
            .attributes(attributes)
            .docType("article")
            .safe(SafeMode.SERVER)
            .backend("html5")
            .get();

asciidoctor.convert(asciiDoc, options);

The output always shows something like this:

sample1

\$sqrt(4) = 2\$
\$sqrt(4) = 2\$

In the above generated HTML output, how do we render the mathematical equations?


Solution

  • Asciidoctor support asciimath and latexmath syntax and the output produced by asciimath can be rendered on browser using http://asciimath.org js library (other asciimath libraries can also be used).

    Asciidoctorj uses \$ as the delimiter for asciimath markup, so we need to configure MathJax using the following configuration:

    <html>
    <head>
    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({
           asciimath2jax: {
               delimiters: [['\\$','\\$'], ['`','`']]
           }
        });
    </script>
    <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
    </script>
    ...
    </head>
    //rest of html
    </html>
    

    After we include the above code snippet in <head> section of html, asciimath rendering shall work fine.

    We can refer to this section of Asciidoctor documents for activating support of asciimath inside asciidocs: https://asciidoctor.org/docs/user-manual/#activating-stem-support