是否可以使用 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,它就可以正常工作。

解决方法

解决方案是您应该在创建画布之前调用所有文本操作:

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