0

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.

Makogan
  • 8,208
  • 7
  • 44
  • 112

1 Answers1

0

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")
Makogan
  • 8,208
  • 7
  • 44
  • 112
  • This is correct. When a canvas in created, it can get a textengine as a constructor argument. If nothing is set, it uses the defaulttextengine from the text module as set at this point in time. If you configure it later, this is not seen from within the canvas. – wobsta Jan 20 '21 at 17:29