Search code examples
python-3.xlatextext-alignmentpyx

Center alignment of text in PyX


I am using PyX to create rectangles and putting some (LaTex) text within the rectangle.

# python 3
from pyx import *
text.set(text.LatexRunner)
c = canvas.canvas()
c.stroke(path.rect(0, 0, 1, 1), [style.linewidth.Thick,
                                 color.rgb.red,
                                 deco.filled([color.rgb.green])])

c.text(0.25, 0.25, r"\LaTeX{}")

c.writePDFfile("text-centered") # creates a PDF file     

The above code snippet creates a PDF:

As you can see, the text is not aligned. It is possible to try manually until it is (visibly) centered, but this is problematic for obvious reasons.

My question: Is it possible to align the text centered (both horizontally and vertically) automatically?


Solution

  • just use

    c.text(0.5, 0.5, r"\LaTeX{}", [text.halign.center, text.vshift.mathaxis])
    

    see

    for some documentation and examples