RegEx Fractions to Open Type Font Fraction Symbols

I'm looking to add a step in my cleanup routine that converts fractions written as 1/3 and substitute with the OTF fonts fraction symbol β…“.

I can find the fraction using /(?:[1-9][0-9]|0)/[1-9][0-9]/g, however I can't seem to get the OTF font version substituted.

I've been looking at regex [\u2150-\u215E\u00BC-\u00BE] that will find the Vulgar fractions.

What do I need to do to replace the original fraction using the super and subscript characters to build a more compact fraction.

To me, the easiest way to replace fractions like 1/3 with the prebuilt fractions like β…“ is to start with a dictionary that maps "1/2" to "Β½", "1/3" to "β…“", "1/4" to "ΒΌ", etc. Then use the Search and Replace action. Here's a small sample:

Find and Replace Fractions.kmmacros (3.3 KB)

Expand to see image of macro

In this example, I've included only some of the vulgar fractions available; you can fill out the rest if you think this will work for you.

(I should mention that I took the easy way out in specifying the fraction-finding regex. Since the Unicode fractions are restricted in how many digits can be in the numerator and denominator, the regex should account for those restrictions. Mine doesn't.)

I don't recommend building fractions from the superscript and subscript characters. It involves a lot more programming and tends to produce ugly fractions.

3 Likes

By the way, if you want to enter the Unicode fractions as you type, you can set up Keyboard Maestro macros that do that for you. For example, I type β€œ;1/2” and get β€œΒ½β€. This sort of text substitution can also be done using tools like TextExpander, Typinator, or Apple’s own text substitution system.

This is not an answer to @Potts_Jeff's question, but I thought it worth mentioning in case people find this thread when searching for this kind of substitution.

3 Likes