0

I have some code that uses non-ASCII unicode chars. Here's a simple example that happens to be JS but the problem exists more generally:

function replaceFractions(text) {
  // We're ignoring ⅙⅚⅑⅐⅑⅒
  return text
    .replace(/([0-9])½/g, '$1.5')
    .replace(/([0-9])⅓/g, '$1.333')
    .replace(/([0-9])⅔/g, '$1.666')
    .replace(/([0-9])¼/g, '$1.25')
    .replace(/([0-9])¾/g, '$1.75')
    .replace(/([0-9])⅕/g, '$1.2')
    .replace(/([0-9])⅖/g, '$1.4')
    .replace(/([0-9])⅗/g, '$1.6')
    .replace(/([0-9])⅘/g, '$1.8')
    .replace(/([0-9])⅛/g, '$1.125')
    .replace(/([0-9])⅜/g, '$1.375')
    .replace(/([0-9])⅝/g, '$1.625')
    .replace(/([0-9])⅞/g, '$1.875')
}

As I see this in Stackoverflow, it's perfectly aligned as I'd expect it to be.

I'm using Monospace-12 in emacs via the following in .emacs:

(set-frame-font "Monospace-12")

If I look at it I see this:

enter image description here

There's bigger gaps than there should be between the 3/4 and 1/5 lines, the 1/5, 2/5, 3/5 and 4/5 are all taking more horizontal space and it's basically ugly. I get similar issues with superscripts (e.g. ⁰¹²³⁴⁵⁶⁷⁸⁹⁽⁾⁻⁺⁼ⁿⁱ)

Is there any way to get this so it's equal width for all chars not just a subset? Ideally I'd like it for all unicode chars but I can live without the weirder ones, emojis and so forth. What I do want is subscripts, superscripts, fractions, currency symbols to all be consistently spaced.

1
  • 3
    It is probably the case that Monospace-12 does not provide those glyphs, so Emacs substitutes a font that does. Do C-u C-x = on each of the characters and see what font is reported for each one. There might be a smarter way to go about it, but all I can suggest is trial-and-error: try to find a monospace font that has all you need. I use Liberation Regular Mono and it does NOT have all of them: I don't know if there is a font that does.
    – NickD
    Mar 2, 2023 at 17:57

1 Answer 1

3

Your font doesn't include those characters, so it's using some other fallback font, and the fallback font size doesn't match.

The complex solution will be to set up fallback fonts that do include those characters, and also match the width of your primary font. This will involve "fontsets" and set-fontset-font (see this question and related).

I've had trouble figuring out fontsets, so I've used a simpler solution: use a font that includes more characters. For example, here's Iosevka:

Screenshot of Iosevka supporting the characters in question

and here's Menlo:

Screenshot of Menlo supporting the characters in question

3
  • Thanks. Those sound good. Next obvious is question is how to set them as being used. Mar 2, 2023 at 20:48
  • Answering my own question as to the usage (1) download from cufonfonts.com/font/menlo (2) Unzip and oopy the ttf to /usr/share/fonts/TTF (3) Add (set-frame-font "menlo-12") and (set-face-attribute 'default nil :font "Menlo-12") to .emacs (4) Replace any (copy-face ...) reference to anything other than default with default Mar 2, 2023 at 21:56
  • 1
    Glad you found Menlo. I think it's an Apple font included on Macs. Iosevka is an open source font downloadable at typeof.net/Iosevka
    – amitp
    Mar 4, 2023 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.