Search code examples
pythongraphicslatexpyx

Is it possible to typeset fractions with pyx?


I have this python snippet:

from pyx import *
from pyx import text

text.set(mode="latex")
text.preamble(r"\usepackage{amssymb}")
text.preamble(r"\usepackage{amsmath}")
c.text(-0.5, 0.1, r"\huge{$\frac{8}{3}$}", [text.halign.boxcenter])

Which is failing with error:

The expression passed to TeX was:
  \ProcessPyXBox{\gdef\PyXBoxHAlign{0.50000}\huge{$\frac{8}{3}$}%
  }{1}%
  \PyXInput{5}%
After parsing the return message from TeX, the following was left:
  *
  *! Undefined control sequence.
  <recently read> \frac 
                        
  <argument> ...PyXBoxHAlign {0.50000}\huge {$\frac 
  (cut after 5 lines; use errordetail.full for all output)

This only happens with \frac, if instead of a fraction I print a symbol, say 5, it works fine.


Solution

  • The solution is that you should call all teh text operations prior to creating the canvas:

    text.set(text.LatexEngine, texenc='utf-8')
    text.preamble(r'\usepackage[utf8]{inputenc}')
    text.preamble(r"\usepackage{amssymb}")
    text.preamble(r"\usepackage{amsmath}")
    text.preamble(r"\usepackage{amsfonts}")
    c = canvas.canvas().layer("aa")