How can I right align text on the canvas?

Hi all

Please consider the following reproducible:

import ROOT

str1 = "Mean: 1.23"
str2 = "Std: 1.23"
max_len = max(len(str1), len(str2))
str1 = f"{str1: >{max_len}}"
str2 = f"{str2: >{max_len}}"
print(str1)
print(str2)

c = ROOT.TCanvas()
latex = ROOT.TLatex()
latex.DrawLatexNDC(0.25, 0.6, str1)
latex.DrawLatexNDC(0.25, 0.5, str2)
c.Update()
input("wait")

How can I make two strings aligned on the TCanvas, similar to as they are aligned shown in the terminal?

Output in the terminal:

Mean: 1.23
 Std: 1.23

Output in the canvas:

Hello! @couet will for sure know how to solve this case.

Hi:

Try:

latex.SetTextAlign(32)
latex.DrawLatexNDC(0.95, 0.6, str1)
latex.DrawLatexNDC(0.95, 0.5, str2)

See documentation about text alignment here:
https://root.cern/doc/master/classTAttText.html#ATTTEXT1

2 Likes