Jump to content

Render-as-you-type math formulas: AsciiMath to PNG and LaTeX


Recommended Posts

demo.gif

My fashcard software supports neither LaTeX nor MathML.
Given that image is the only way to go, I created this workflow where you,

  1. Open Alfred
  2. Type ` (backtick)
  3. Start typing a math expression in AsciiMath syntax.
  4. As soon as you start typing, another window opens and it displays the typesetted math in real time as you type.
  5. Pressing enter closes both alfred and the math window, and puts a PNG image into clipboard.
  6. Instead of pressing enter, if you pressed command + enter, it copies LaTeX instead of the image to clipboard.

 

Q: Why AsciiMath?

A: Because it is extremely concise and intuitive. Check out this handy list of asciimath-vs-latex comparison examples.

 

Q: What if I don't know how to write a particular thing in AsciiMath?

A: If you are stuck in middle of typing, just stop, hit shift, and it'll pop up a reference. When done consulting the reference, hit escape again, and start typing from where you left off! (see gif below)

demo-ref.gif

 

Meta: A thing that I find exciting is this gives an example of a "rich preview as you type" workflow.

 

Broad Technical details:

  • custom protocol URLs: gives a cheap one-way-server functionality, ie, a way for alfred scripts to communicate with the running app, instead of launching an app every script invocation, or, running a server manually in the app (or some other complicated IPC scheme)
  • monitor window events so that even if the user presses escape and quits the workflow without actioning, the math window can be hidden

 

Download: AsciiMath.alfredworkflow

Edited by Mr Pennyworth
Link to post
  • 2 weeks later...

Thank you for this great workflow. I have been trying it but found that powers are not shown properly. For example, X^2 only shows x without the power. Is there a way to fix it? Thank you.

Screenshot of Microsoft Edge (11-15-20, 11-30-01 AM).jpg

Link to post

@RCL I just tried x^2 and it did show correctly...

 

can you run this command in terminal and confirm it just shows "x+y"? open -g "asciimath://render?x^2+y^2"
Or, does it show x^2+y?
Or, does it show x^2+y^2 correctly?

 

Also make sure that "alfred filters results" is not checked in the workflow. (see screenshot)

image.thumb.png.d0288d821d4bb248bebc4639e5f9c850.png

Link to post

@ Mr Pennyworth

The "alfred filters results" box is unchecked. When I try the command in the terminal, I got the message below. I am running Big Sur 11.0.1. Could it be the cause of the problem? Thank you.

 

RCL@MBP ~ % open -g "asciimath://render?x^2+y^2"
The file /Users/rcl/asciimath:/render?x^2+y^2 does not exist.

Link to post

@RCL hmm... yeah, it could be BigSur. I haven't tested on it.

In the meanwhile, can you open the workflow folder in terminal (like in the screenshot) and then run the following command twice?

open -a 'AsciiMath Renderer.app' 'asciimath://render?x^2+y^2'

(on the first run, likely it won't show anything at all, hence twice)

image.png.b06284e68291196b68cfc457f2102fda.png

 

Basically, I want to figure out whether it has problem especially with powers or with anything that's longer that one letter...

For that, it'll be awesome if you could try out something simple in alfred like "x+y=z" and see if renders or whether even in that case it is just "x"...

Link to post

I ran the way suggested and got the message below. Attached are screenshots that show equations properly. Thank you.

 

RCL@MBP user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E % open -a 'AsciiMath Renderer.app' 'asciimath://render?x^2+y^2'
The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+y^2 does not exist.
RCL@MBP user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E % open -a 'AsciiMath Renderer.app' 'asciimath://render?x^2+y^2'
The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+y^2 does not exist.

 

Screenshot of Firefox (11-15-20, 10-25-53 PM).jpg

Screenshot of Firefox (11-15-20, 10-13-30 PM).jpg

Link to post

@RCL hmm... that's very strange!

both the parts about the terminal commands not working,

and about the workflow still showing "x+y=z"

 

I don't have access to a BigSur machine, but here's a last thing to try:

1. In the workflow, right-click on the script filter, and post a screenshot here so that I can double-check the exact code you're runningimage.thumb.png.b2865481ea9c81bfb6614948684a369d.png

 

2. Keeping the debugger open, try "x^2+y^2" inside alfred and let me know what it renders and also the full text of the debugging output.

image.png.5f4efd8d3f80610df6c57fdd6be51d9d.png

Link to post

Attached are screenshots. The debug message is as follows. Thank you.

 

[11:11:10.427] AsciiMath[Script Filter] Queuing argument 'x^2+y^2'
[11:11:10.459] AsciiMath[Script Filter] Script with argv 'x^2+y^2' finished
[11:11:10.463] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+y^2 does not exist.
[11:11:10.464] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:13.641] AsciiMath[Script Filter] Queuing argument 'X'
[11:11:13.728] AsciiMath[Script Filter] Script with argv 'X' finished
[11:11:13.728] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:15.739] AsciiMath[Script Filter] Queuing argument 'x'
[11:11:15.891] AsciiMath[Script Filter] Script with argv 'x' finished
[11:11:15.895] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:16.401] AsciiMath[Script Filter] Queuing argument 'x^'
[11:11:16.492] AsciiMath[Script Filter] Script with argv 'x^' finished
[11:11:16.496] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^ does not exist.
[11:11:16.497] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:16.625] AsciiMath[Script Filter] Queuing argument 'x^2'
[11:11:16.653] AsciiMath[Script Filter] Script with argv 'x^2' finished
[11:11:16.663] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2 does not exist.
[11:11:16.664] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:18.073] AsciiMath[Script Filter] Queuing argument 'x^2+'
[11:11:18.103] AsciiMath[Script Filter] Script with argv 'x^2+' finished
[11:11:18.113] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+ does not exist.
[11:11:18.115] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:18.819] AsciiMath[Script Filter] Queuing argument 'x^2+y'
[11:11:18.909] AsciiMath[Script Filter] Script with argv 'x^2+y' finished
[11:11:18.918] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+y does not exist.
[11:11:18.919] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:19.425] AsciiMath[Script Filter] Queuing argument 'x^2+y^'
[11:11:19.515] AsciiMath[Script Filter] Script with argv 'x^2+y^' finished
[11:11:19.524] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+y^ does not exist.
[11:11:19.525] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}
[11:11:19.705] AsciiMath[Script Filter] Queuing argument 'x^2+y^2'
[11:11:19.796] AsciiMath[Script Filter] Script with argv 'x^2+y^2' finished
[11:11:19.807] STDERR: AsciiMath[Script Filter] The file /Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/asciimath:/render?x^2+y^2 does not exist.
[11:11:19.808] AsciiMath[Script Filter] {"items": [
    {
        "title": "Copy Math",
        "subtitle": "as Image (⌘: as LaTeX)",
        "arg": "copyImage",
        "quicklookurl": "/Users/rcl/OneDrive/Alfred.alfredpreferences/workflows/user.workflow.F62AE381-F57E-4939-AA70-79FA9026916E/AsciiMath.html",
        "mods": {
            "cmd": {
                "subtitle": "as LaTeX",
                "arg": "copyLatex"
            }
        }
    }
]}

Screenshot of Alfred Preferences (11-16-20, 11-09-50 AM).jpg

Screenshot of Alfred Preferences (11-16-20, 11-11-30 AM).jpg

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...