我可以从 python 获得乳胶打印的物理单位(厘米、像素、点)的字符串长度吗?

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

我正在使用 python 自动创建一个巨大的 table 然后我想将其传递给 LaTeX。我想知道某些字符串的长度,因为它们将由 LaTeX 打印。类似于:

get_latex_length(string,font,fontsize)

有这种东西吗?

您可以尝试使用 Python 图像库中的 font.getsize() 获取字符串大小。如果在 LaTeX 中使用相同的字体,这可能会有所帮助:

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

输出是给定文本字符串的宽度和高度(以像素为单位):(54, 10)