是否可以用 pyx 排版分数?

Is it possible to typeset fractions with pyx?

我有这个 python 片段:

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])

失败并出现错误:

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)

这只发生在 \frac 上,如果我打印一个符号而不是分数,比如 5,它工作正常。

解决方案是您应该在创建 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")