sup
sup

Reputation: 734

Can I get a string length in physical units (cm, pixels, dots) as printed by latex from python?

I am using python to automatically create a huge table that I then want to pass to LaTeX. I would like to know a length of certain strings as they will be printed by LaTeX. Something like:

get_latex_length(string,font,fontsize)

Does such a thing exists?

Upvotes: 2

Views: 866

Answers (1)

Delimitry
Delimitry

Reputation: 3037

You could try to get string size using font.getsize() from Python Imaging Library. If the same font is used in LaTeX this could help:

from PIL import ImageFont

def get_latex_length(string, font, fontsize):
    font = ImageFont.truetype(font, fontsize)        
    return font.getsize(string)

print(get_latex_length('Some text', 'Arial.ttf', 12))

The output is width and height of the given text string in pixels: (54, 10)

Upvotes: 3

Related Questions