Jump to content

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


Recommended Posts

Posted (edited)

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
Posted (edited)
On 11/7/2020 at 1:43 AM, lynxlinius said:

10.14?

Whoops! Thanks for pointing it out!
Updated the workflow. Should work for 10.13 and up now, although I have no means of testing...

Edited by Mr Pennyworth
  • 2 weeks later...
Posted

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

Posted

@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

Posted

@ 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.

Posted

@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"...

Posted

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

Posted

@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

Posted

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

Posted

Thanks @RCL for the log!
Because I have no way of testing as I'm not on BigSur, this is just a guess, but I think I have a fix

open -g "asciimath://render?x%5E2%2Bz%5E2"

Try the above command in terminal and see if it works. If it does, I'll accordingly update the workflow.

  • 10 months later...
Posted

I literally just made an account to say THANK YOU SO MUCH for this. It's so good!

 

When I save the image, the resolution isn't so good and the background isn't transparent. Any idea about how I can fix that?

  • 2 weeks later...
Posted

Hi @Mr Pennyworth. Absolutely love this workflow; thank you!

 

Unfortunately, I'm having an issue. The workflow functioned as intended for a day, however now the render/typesetting window no longer pops up. Also, without it, I can't copy the PNG or the LaTeX. Any help would be much appreciated!

AsciiMath.png

Posted

As a follow-up, it turns out AsciiMath Renderer has been crashing on me every time it tries to run. I have verified this in the macOS console logs. No idea what the issue is...

Posted
7 hours ago, JJGordo said:

As a follow-up, it turns out AsciiMath Renderer has been crashing on me every time it tries to run. I have verified this in the macOS console logs. No idea what the issue is...

 

And what did the logs say? Are you getting an error message in Alfred’s workflow debugger?

 

We certainly have no idea what's going on on your Mac, and we can't help you if you don't give us more information than "X is crashing".

Posted (edited)

Hi @deanishe. I'm not as adept as others here, sorry. Attached is a screenshot of the Alfred debugger (where I don't see it catching anything?) as well as the console log:

Screen Shot 2021-11-05 at 11.17.45 AM.png

Screen Shot 2021-11-05 at 11.20.03 AM.png

 

 

Text version of console log:

 

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               AsciiMath Renderer [21875]
Path:                  /Users/USER/Library/Application Support/Alfred/*/AsciiMath Renderer.app/Contents/MacOS/AsciiMath Renderer
Identifier:            mr.pennyworth.AsciiMathRenderer
Version:               0.0.8 (1)
Code Type:             X86-64 (Native)
Parent Process:        launchd [1]
User ID:               501

Date/Time:             2021-11-05 11:15:52.8523 -0400
OS Version:            macOS 12.0.1 (21A559)
Report Version:        12
Bridge OS Version:     6.0 (19P548)
Anonymous UUID:        4D86192D-FA14-0F5A-A234-6211E3EA13D5

Sleep/Wake UUID:       C2F701DB-62DF-4E4F-8D4E-D009D6F3632C

Time Awake Since Boot: 73000 seconds
Time Since Wake:       1424 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Reason:    Namespace SIGNAL, Code 4 Illegal instruction: 4
Terminating Process:   exc handler [21875]

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   AsciiMath Renderer                       0x109dc6d39 globalinit_33_9029708ADB81389A80B02D410BB30023_func11 + 329
1   libdispatch.dylib                     0x7ff80f4a1cc9 _dispatch_client_callout + 8
2   libdispatch.dylib                     0x7ff80f4a2ec1 _dispatch_once_callout + 20
3   AsciiMath Renderer                       0x109dc9744 globalinit_33_B5244C1962C1E149A3A2E7710EC309D5_func14 + 276
4   libdispatch.dylib                     0x7ff80f4a1cc9 _dispatch_client_callout + 8
5   libdispatch.dylib                     0x7ff80f4a2ec1 _dispatch_once_callout + 20
6   AsciiMath Renderer                       0x109dcbf08 specialized static PressSecretary.enable() + 232
7   AsciiMath Renderer                       0x109dca849 static Alfred.onHide(callback:) + 41
8   AsciiMath Renderer                       0x109dc1b16 AppDelegate.applicationDidFinishLaunching(_:) + 1270
9   AsciiMath Renderer                       0x109dc1dab @objc AppDelegate.applicationDidFinishLaunching(_:) + 107
10  CoreFoundation                        0x7ff80f714e73 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
11  CoreFoundation                        0x7ff80f7b2349 ___CFXRegistrationPost_block_invoke + 49
12  CoreFoundation                        0x7ff80f7b22c6 _CFXRegistrationPost + 496
13  CoreFoundation                        0x7ff80f6e6786 _CFXNotificationPost + 733
14  Foundation                            0x7ff81052e996 -[NSNotificationCenter postNotificationName:object:userInfo:] + 82
15  AppKit                                0x7ff81215118b -[NSApplication _postDidFinishNotification] + 305
16  AppKit                                0x7ff812150edd -[NSApplication _sendFinishLaunchingNotification] + 208
17  AppKit                                0x7ff81214eab0 -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] + 541
18  AppKit                                0x7ff81214e707 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 665
19  Foundation                            0x7ff81055a284 -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 308
20  Foundation                            0x7ff81055a0f6 _NSAppleEventManagerGenericHandler + 80
21  AE                                    0x7ff815d9bd28 0x7ff815d90000 + 48424
22  AE                                    0x7ff815d9b592 0x7ff815d90000 + 46482
23  AE                                    0x7ff815d949c7 aeProcessAppleEvent + 419
24  HIToolbox                             0x7ff81834cb42 AEProcessAppleEvent + 54
25  AppKit                                0x7ff812148d8a _DPSNextEvent + 2064
26  AppKit                                0x7ff812146f5c -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1411
27  AppKit                                0x7ff812139359 -[NSApplication run] + 586
28  AsciiMath Renderer                       0x109dc2a86 closure #1 in  + 294
29  AsciiMath Renderer                       0x109dc2abe specialized thunk for @callee_guaranteed () -> (@error @owned Error) + 14
30  libswiftObjectiveC.dylib              0x7ff8243e9e6c autoreleasepool<A>(invoking:) + 44
31  AsciiMath Renderer                       0x109dc1033 main + 35
32  dyld                                     0x10d8994fe start + 462

Edited by JJGordo
Posted

This looked like an interesting workflow and I tried installing and got a very similar error - no quickview lookup when typing a formula, nothing concrete coming out of Alfred debugger, but an actual crash log for AsciiMathRenderer every time its called. Very similar error log, pasting screencap of top section for context

image.thumb.png.f57752e8e75a13151b17d6ba8e7d01e9.png

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...