pyx-project / pyx

Repository of PyX, a Python package for the creation of PostScript, PDF, and SVG files.
https://pyx-project.org/
GNU General Public License v2.0
109 stars 18 forks source link

(Question) How to typeset a fraction? #24

Closed Makogan closed 3 years ago

Makogan commented 3 years ago

I am trying to render a fracion with pyx:

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

But I am getting an error:

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

The documentation says that the string is passed directly onto the LateX engine, and to my knowledge the engine should be able to handle the frac command, so I am a bit confused.

Makogan commented 3 years ago

The answer is to make all the text operations prior to creating the canvas.

wobsta commented 3 years ago

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.

This works:

from pyx import *

text.set(engine=text.LatexEngine)
c = canvas.canvas()

c.text(0, 0, r'$\frac{1}{2}$')

but doing it the other way around it fails:

from pyx import *

c = canvas.canvas()
text.set(engine=text.LatexEngine)

c.text(0, 0, r'$\frac{1}{2}$')

However, you can insert text instead of using the internal textengine of the canvas. Than it starts to work again:

from pyx import *

c = canvas.canvas()
text.set(engine=text.LatexEngine)

c.insert(text.text(0, 0, r'$\frac{1}{2}$'))