DEV Community

Ishaan Verma for Elsewhere

Posted on

Why maths goes wrong when a textbook is read aloud

Hi, I'm Ishaan and I built Elsewhere, an ebook reader that runs in the browser and reads books aloud. I worked on how it reads formulas because I thought students in school or college would like the option, and trading books often have formulas too.

Here is how my reader used to say a copy of the quadratic formula that had plain hyphens for minus signs:

x equals, b plus or minus b two, four uh c two a

The fraction, the square root, the square and both minus signs are gone.

Symbols in order lose the structure

An EPUB can write a formula in MathML, which records its structure, here a fraction with a root inside it. This is the markup I tested:

<math>
  <mi>x</mi><mo>=</mo>
  <mfrac>
    <mrow>
      <mo>-</mo><mi>b</mi><mo>±</mo>
      <msqrt><mrow>
        <msup><mi>b</mi><mn>2</mn></msup>
        <mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi>
      </mrow></msqrt>
    </mrow>
    <mrow><mn>2</mn><mi>a</mi></mrow>
  </mfrac>
</math>
Enter fullscreen mode Exit fullscreen mode

For a formula with no alt text, I was collecting the symbols in reading order:

const words = Array.from(el.querySelectorAll('mi, mn, mo, mtext, ms'))
  .map((t) => (t.textContent ?? '').trim())
  .filter(Boolean)
  .join(' ')
Enter fullscreen mode Exit fullscreen mode

That throws away the tree, which is where the meaning was. The hyphen made it worse. My narration rules treat a spaced hyphen as a dash and turn it into a pause, so the minus was never said. With the Unicode minus sign (U+2212) both minuses were spoken, but the fraction, the root and the square were still missing.

Reading the tree

The new formula reader walks the MathML and gives each kind of element its own wording. Here are two of the cases, trimmed:

case 'mfrac':
  if (isBrief(a) && isBrief(b)) return `${slot(a)} over ${slot(b)}`
  return bounded(
    `the fraction with numerator ${slot(a)}, and denominator ${slot(b)}`,
    false, last, 'end fraction',
  )
case 'msqrt': {
  const inner = itemsOf(kids(el))
  return bounded(
    `the square root of ${speakRow(inner)}`,
    inner.length <= 1, last, 'end root',
  )
}
Enter fullscreen mode Exit fullscreen mode

The same formula now produces this:

x equals the fraction with numerator negative b plus or minus the square root of b squared minus 4 a c, and denominator 2 a

A minus with nothing before it to subtract from becomes "negative". A short fraction stays short, as in "1 over x". Matrices, sums with limits and function arguments have their own wording.

Saying where a root ends

On paper the bar over a root shows where it stops. Read aloud, the end has to be said. When a root holds more than one thing and something follows it in the same row of the MathML, bounded marks the spot:

the square root of a plus b, end root, plus c

The formula reader punctuates with commas, because a full stop would end the sentence the formula sits in.

When the letter "a" came out as "uh"

The phonemizer behind the Piper voices reads a lone "a" before another word as the article, so "a squared" came out as "uh squared". At the end of a phrase it says the letter's name, so the closing "2 a" was already right. The voice cannot tell the letter from the article, but the formula reader knows which one it wrote. It marks the letter with a word joiner (U+2060), which shows as nothing, and the narration rules respell a marked "a" as "eigh". These are the phonemes before and after:

a squared    -> ɐ skwˈɛɹd
eigh squared -> ˈeɪ skwˈɛɹd
Enter fullscreen mode Exit fullscreen mode

An unmarked "a" in ordinary prose is left alone.

Keeping the underline in step

The app underlines each word as it is spoken, so the text made at upload and the text rebuilt on the reading page must match character for character. The formula's words are worked out once, at upload, and stored on the <math> element as alttext. The reading page takes the words from that attribute and never needs to know how they were chosen. A book's own alt text still wins, unless it looks like leftover TeX or says it is not available.

Storing the words once has a cost. A book added before the change keeps its old reading until you delete it and add it again, because any audio it already has was made from those words.

What still goes wrong

  • √a + b and √(a + b) can come out as the same words, "the square root of a plus b". e^x + 1 and e^(x + 1) collide the same way. The code skips the closing phrase when a root or exponent is short or has nothing after it, so each pair lands on the same words.
  • In a PDF, an equation the app picks out on its own line is shown as a picture and not read aloud. Other PDF maths is still read symbol by symbol.
  • A formula that an EPUB ships as an image is not read at all.
  • The words the formula reader writes are English, even in a book written in another language.

Elsewhere is my project. It opens DRM-free books, so a book bought from the Kindle store will not open, and the voice is an AI voice rather than a human narrator. You can try the reader without an account at https://listenelsewhere.com/try (the sample there is Dracula, so it has no formulas).

What more would you add in a feature like this? Let me know in the comments.

Top comments (0)