Any chance of having an option to make asciiMath work?

m's Avatar

m

12 Jul, 2014 04:51 PM

I have a work around for this which is great but not pretty.

  1. I have downloaded ASCIIMathML.js from here and saved it on my machine at /my/local/path/to/.
  2. Any note that I have where I want to add some math I simply add the following lines at the bottom of my note before typing:

    <script type="text/javascript" src="/your/local/path/to/ASCIIMathML.js"></script>

    <pre><script>mathcolor="Black"</script></pre>

    The second line is only because the default colour for the math is blue.

  3. Now I write any math that I want between the amath and endamath tags.

Like so ...

amath

y = pi / x

endamath

And that's it.

But it would be nice if I could set this up in preferences, and didn't have to have the script lines at the end of my note.

  1. Support Staff 1 Posted by Brett on 19 Jul, 2014 12:43 PM

    Brett's Avatar

    I'd consider adding this to the preferences, but I'll need more than just one vote. If anyone else wants to see this, please comment here.

  2. 2 Posted by Mitchell on 03 Aug, 2014 12:19 AM

    Mitchell's Avatar

    I would really like this. TeX math input takes a lot of keystrokes and it's hard to read and make changes to it. I would like a checkbox that allows me to enter math just as before (same delimiters, etc.) but parses it as AsciiMath rather than TeX.

    Before:

    $$V_\text{frustum}=\pi\int_0^h\left[R_1+(R_2-R_1)\frac{z}{h}\right]^2 dz$$

    After:

    $$V_"frustum" = pi int_0^h [R_1 + (R_2 - R_1)z/h]^2 dz$$

  3. 3 Posted by Stephen Thomas on 22 Aug, 2014 01:25 AM

    Stephen Thomas's Avatar

    +1 (more if it were permitted).

    The lack of ASCIIMath support prevents me from using Marked, since most of the documents I'm currently writing include equations, and LaTeX is far too cumbersome to input by hand. I've had to resort to MathML (which isn't nearly as nice as ASCIIMath) but at least most Markdown editors (e.g Markdown Pro or MultiMarkdown Composer) pass it through unchanged to their previewers and Webkit renders it fine.

    We'll have to see what Typed has up it's sleeve.

  4. 4 Posted by Stephen Thomas on 22 Aug, 2014 01:31 AM

    Stephen Thomas's Avatar

    As a followup, I note that the latest version MathJax.js supports ASCIIMath if configured appropriately. Since Marked already has an option to include MathJax, enabling this really should be pretty simple. Probably just configuring the appropriate MathJax options and defining the delimiters (since MathJax uses backticks by default). Send me a note if you want more explicit suggestions or a beta tester.

  5. Support Staff 5 Posted by Brett on 22 Aug, 2014 02:54 AM

    Brett's Avatar

    Do you have a pointer to the docs for ASCIIMath support in MathJax?

  6. 6 Posted by Stephen Thomas on 22 Aug, 2014 11:33 AM

    Stephen Thomas's Avatar
  7. 7 Posted by Stephen Thomas on 22 Aug, 2014 12:28 PM

    Stephen Thomas's Avatar

    You'll probably want to add a bit of code to disable markdown processing within the ASCIIMath delimiters. For example (using the backtick as a delimiter):

    \`sum_(i=1)^n i^3=((n(n+1))/2)^2\`
    

    Gets turned into

    <p>sum_(i=1)^n i&lt;sup&gt;3=((n(n+1))/2)&lt;/sup&gt;2</p>
    

    And the added <sup> tags throw off the ASCIIMath processor.

  8. 8 Posted by Stephen Thomas on 24 Aug, 2014 12:45 PM

    Stephen Thomas's Avatar

    As a bit of followup, and in hopes of helping folks out until there's full support for asciiiMath, it is possible to get Marked 2 to work with asciiMath with only a small bit of work. These steps are based on the excellent idea originally described by m above.

    First, you'll want to disable the built-in MathJax support. That's in two places in the app. In the Style preferences, in the Advanced section at the bottom of the window, make sure Enable MathJax is not checked.

    Then, when exporting a document to HTML, make sure that Include MathJax CDN link is not checked.

    Second, in the document that you want to use asciiMath, add the following line at the end.

    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML"></script>
    

    Yes, this is adding back MathJax support (which we just removed in step 1), but it's doing so with a different configuration. The built-in configuration that Marked uses is TeX-AMS-MML_HTMLorMML and that configuration doesn't support asciiMath.

    Third, embed any asciiMath expressions within delimiters consisting of the backslash (\) followed by the back-tick (`). The backslash before the back-tick is important; without it, Marked will treat the back-tick as a way to indicate inline code, as per the regular markdown syntax. Also, you have to escape any characters that are part of regular markdown within your asciiMath. That's mostly the caret (^) and the underscore (_). To escape those characters, just add a backslash in front of them. For example, the following standard asciiMath expression:

    sum_(i=1)^n i^3=((n(n+1))/2)^2
    

    can be included as

    \`sum\_(i=1)\^n i\^3=((n(n+1))/2)\^2\`
    

    One caveat: You'll need an internet connection when viewing your document.

  9. Support Staff 9 Posted by Brett on 24 Aug, 2014 12:47 PM

    Brett's Avatar

    If that's all it takes, I can make that an option easily with the
    built-in MathJax. I'm just ignorant about all of the configuration
    options.

  10. 10 Posted by Mitchell on 24 Aug, 2014 12:58 PM

    Mitchell's Avatar

    All you need to do is use ?config=AM_HTMLorMML.

    Also, I would prefer to keep using dollar signs for consistency, because the back ticks are already used for code, and because using \` means the math markup will get parsed as Markdown. You can configured MathJax to use dollar signs with this:

    <script type="text/javascript">
    if (typeof MathJax !== 'undefined') {
        MathJax.Hub.Config({
            asciimath2jax: {
                delimiters: [['$','$']]
            }
        });
    }
    </script>
    

    You can have multiple pairs of delimiters in that array if you wanted. The only problem is that the ASCIIMath configuration doesn't recognize display math. In my own setup, I use a regex to change $$...$$ to <p class="display-math">$...$</p>, and it works pretty well just with .display-math { text-align: center; }.

  11. 11 Posted by Stephen Thomas on 24 Aug, 2014 02:23 PM

    Stephen Thomas's Avatar

    As a bit of followup to Mitchell's suggestions:

    1. If Marked provided the option to select the MathJax configuration (the value of the config= query parameter in the script URL), that would indeed be enough to get asciiMath working. The currently supported values for this query parameter can be found in the MathJax documentation

    2. This change alone, however, will not be sufficient to allow delimiters other than back-ticks, as all of the pre-defined configurations use back-ticks. As Mitchel notes, to change the delimiter, Marked would have to forgo the simple query string configuration and add actual JavaScript code to set up MathJax. It might be tricky to expose appropriate settings of that configuration to Marked users.

    3. Even if you change the delimiter to $, the math markup would still get parsed as markdown, so changing the delimiter doesn't really help much.

    4. Whatever the asciiMath delimiter is, it would be best if Marked could recognize it and disable markdown parsing of math markup. Without that support, you have to escape all the markdown characters in your math markup as noted in my comment above.

    5. Personally, I don't have any particular preference for the specific delimiter, escaped back-ticks, dollar signs, whatever.

    6. Without knowing anything about the Marked codebase, it's dangerous for me to do so, but it seems like the easiest approach would be:

      a. Add an option to select the MathJax configuration from among the predefined values, and use the selected value as the query string parameter.

      b. Automatically, or via a user option, disable markdown processing of text within a set of delimiters. The set of delimiters could initially be the escaped back-tick.

      c. Enhance MathJax support to allow arbitrary configuration instead of the predefined query string values.

    With the above approach, (a) would provide minimal asciiMath support without requiring users to add explicit scripts, (b) would allow use of math markup without requiring explicit escaping of markdown characters, and (c) would give users the option to customize MathJax support (including, but not limited to, delimiters).

  12. Support Staff 12 Posted by Brett on 24 Aug, 2014 02:34 PM

    Brett's Avatar

    It's a start...

    I'm open to the idea of custom configs, probably via exposing the local.js file in the App Support folder. If you choose local in the dropdown and that file exists, it would use it. I already have it set up to take the selection in prefs and use it in the query string for both local preview and embedded export with the CDN.

  13. 13 Posted by Stephen Thomas on 24 Aug, 2014 02:59 PM

    Stephen Thomas's Avatar

    Now that is worth a bunch of five-star reviews!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac