PyX — Example: text/textengine.py

0.8 KB
53.1 KB
41.7 KB
22.3 KB
42.1 KB

Running and controlling TeX

textengine.png
from pyx import *

# Set properties of the defaulttextengine, e.g. switch to LaTeX.
text.set(text.LatexEngine)

c = canvas.canvas()
# The canvas, by default, uses the defaulttextengine from the text module.
# This can be changed by the canvas method settextengine.
c.text(0, 0, r"This is \LaTeX.")

# If you want to use another textengine temporarily, you can just insert
# a text box manually
plaintex = text.TexEngine() # plain TeX engine
c.insert(plaintex.text(0, -1, r"This is plain \TeX."))

# There also is the UnicodeEngine to output text directly without TeX/LaTeX.
unicodetext = text.UnicodeEngine() # unicode engine
c.insert(unicodetext.text(0, -2, "Simple unicode output without TeX/LaTeX."))

c.writeEPSfile("textengine")
c.writePDFfile("textengine")
c.writeSVGfile("textengine")

Description

Usually, all you will ever need is a single textengine instance which controls a single running TeX or LaTeX instance at a time or uses direct Unicode text typesetting. To make life easy to our users, such an instance is created automatically upon startup and its called the defaulttextengine. Whenever you use the function shortcuts like text.text() and in particular text.set(), you are accessing this defaulttextengine instance. Also, the shortcut method text of any canvas instance uses this defaulttextengine by default.

However, it is possible to create several textengine instances. In this example, we are using three of them. To make it a little more interesting, we instruct the defaulttextengine to use LaTeX, while keeping TeX for the plaintex instance. It is you turn to use expressions valid in TeX or LaTeX only at the different textengine instances. Additionally we show the use of a unicodetext instance to typeset Unicode text.

Note that you do not have to worry about mixing the results of different textengines into a single output. Even the embedded fonts in the output are shared. You can, by the way, also restart a plain TeX or LaTeX engines including the defaulttextengine, which is a plain TeX engine.