AllenDowney / plastex-oreilly

Branch of plastex that generates DocBook 4.5 that meets O'Reilly style guidelines.
7 stars 7 forks source link

how do I render Greek letters? #12

Closed AllenDowney closed 12 years ago

AllenDowney commented 12 years ago

Greek letters in math mode get parsed into MathSymbols. Each contains the corresponding unicode character in a class variable named unicode. For example (from Math.py):

class MathSymbol(Command):
    pass

# Lowercase
class alpha(MathSymbol): unicode = unichr(945)

After parsing, $\alpha$ appears as an alpha tag. Which is not great for me because I want to be able to check whether something is a MathSymbol without enumerating all possible MathSymbols.

Then, I want to write a template to generate the entity.

α

Something like

name: MathSymbol
type: python
'&#x%4x' % self.unicode

But again, MathSymbol doesn't appear at this point, only the specific tag (alpha, beta).

So, do I have to go back and change the way MathSymbols get parsed, or can I recover the information I need during TreeCleaning?

tiarno commented 12 years ago

not an answer, but maybe a helpful link for the issue later on: http://milde.users.sourceforge.net/LUCR/Math/

AllenDowney commented 12 years ago

Ok, I made some progress on this. In Math.py I've got class MathSymbol with this new invoke method:

def invoke(self, tex):
    a = Command.invoke(self, tex)
    print 'MathSymbol.invoke'
    print self, self.tagName, type(self)

    if self.unicode is None:
        return a
    if self.unicode == -1:
        code = self.tagName
    else:
        code = '&#x%04x;' % ord(self.unicode)

    o = self.ownerDocument.createElement('mathsymbol')
    child = self.ownerDocument.createTextNode(code)
    o.append(child)
    self.ownerDocument.context.push(o)
    return [o]

Seems to be working, although the ampersand is getting expanded. I might need to escape it.

Or maybe just put in the unicode character and let it get expanded?

AllenDowney commented 12 years ago

Groan. Well, this turns out to be easier than I made it out to be. It turns out that if you hoist any of the MathSymbols out of the math tag (which gets imaged) and into a mathphrase tag (which gets rendered) everything just works.