Ideally one can just type fractions as normally e.g. 2/3, and either the view simply displays it as ⅔, without changing the text itself OR the text itself is changed. I need this for org mode in case that's relevant.
3 Answers
You could use prettify-symbols-mode
with something, evaluable in your sratch buffer, like this:
(progn
(push '("2/3" . ?⅔) prettify-symbols-alist)
(prettify-symbols-mode -1)
(prettify-symbols-mode +1))
It only adds that particular fraction and it's unicode counterpart to the list of symbols to prettify, then resets the mode
If it works as expected, just add the prettification symbols you need to the mode hook you want.

- 2,576
- 1
- 14
- 22
-
I thought this works, but it doesn't when I put a character after pattern e.g. `2/3|` doesn't contract. Is there a fix for that? – avv May 20 '20 at 02:17
-
1Short answer wiill be it doesn't because the string doesn't match. `prettify` needs strings, not regexps and cannot cannot tell apart `2/3|` from `2/32` or other characters unless the mode you're working on somewhat recognize them as separators. What I mean is that under `org-mode` `2/3{` or `2/3:` will be prettified while `2/3|` won't. Also behavior won't be the same in the scratch buffer. The fine details of each mode prettify support are beyond my knowledge. – Muihlinn May 20 '20 at 07:14
-
If I'm understanding correctly, what you are saying is that matching requires a separator. (Perhaps for performance reasons?) What I would have expected originally, is that `2/32` would become `⅔2`. But I do see that this could lead to more confusion in terms of what substitutions apply in what order, etc (by default, I would assume they just apply in alist order?). – avv May 20 '20 at 18:55
-
1I'm thinking more in how the mode parses its contents, if it can tell what part is syntax, an operator, or whatever else it will be prettified. If you evaluate the example above in the scratch buffer, and then insert this `2/3 (2/3) '2/3 ,2/3 @,2/3 #2/3 !2/3 2/3|` you'll see what I mean. Oddly enough, but kind of understandable, it won't recognize `"2/3"` in such mode. – Muihlinn May 20 '20 at 19:03
If you're willing to have the text change, you can use one of Emacs' many input methods. With e.g. rfc3145
, you could type that as
&23
or with TeX
it would be
\\frac23
Use C-\ to choose your input method.

- 4,605
- 8
- 22
EDIT: Doesn't work completely, see comment on Muihlinn's answer.
Based off Muihlinn's answer, I looked into prettify-symbols-mode
and ended up finding this macro which solved my problem cleanly: https://github.com/Ilazki/prettify-utils.el/blob/master/prettify-utils.el#L222-L243
(prettify-utils-add-hook org-mode
("1/4" "¼")
("1/2" "½")
("3/4" "¾")
("1/3" "⅓")
("2/3" "⅔")
("1/5" "⅕")
("2/5" "⅖")
("3/5" "⅗")
("4/5" "⅘")
("5/6" "⅚")
("1/8" "⅛")
)

- 1,563
- 10
- 24