Width of a TLatex text

Dear experts,

I cannot find a way how to calculate a width of a text to be printed in a TPad as a TLatex. I would like to know the width in NDC. Does a method with this functionality exist? If not, could you add it to TLatex, please? Meanwhile, is there some workaround?
Thank you for your help in advance.
Best regards

Vojtech

Of course this functionality exists because we need it to define the text alignment. It is GetTextExtent:
root.cern.ch/doc/master/classTT … fcde818590

Dear couet,

thank you for your answer. Unfortunately, I don't understand the output of the method. I am getting some very large numbers (see below). Moreover, I am trying to get the width of the string "another" in two different pads (cmain and cratio defined below) and I am getting two different numbers although the width of the two pads is the same! What is going on?
Best regards

Vojtech

Definition of the two pads:

gStyle.SetPadLeftMargin(0.16)
gStyle.SetPadRightMargin(0.05)
gStyle.SetPadBottomMargin(0.16)
gStyle.SetPadTopMargin(0.05)
cmother = TCanvas(“cmother”, “cmother”, 600, 600)
cmother.cd()
cmain = TPad(“cmain”, gPad.GetTitle(), 0, 0.358, 1, 1)
cmain.SetTopMargin(0.057)
cmain.SetBottomMargin(0.03)
cmain.Draw()
cmother.cd()
cratio = TPad(“cratio”, gPad.GetTitle(), 0, 0, 1, 0.358)
cratio.SetBottomMargin(0.33)
cratio.Draw()

Method to get the text width

def getTextWidth(self, text):
    self.pad.Modified()
    self.pad.Update()
    t = TLatex(0.2, 0.9, text)
    t.SetNDC()
    gROOT.ProcessLine("UInt_t w = 0; UInt_t h = 0;")
    gROOT.ProcessLine("TLatex* tmp = new TLatex(0,0,\"{0}\");".format(text))
    gROOT.ProcessLine("tmp->GetTextExtent(w, h, \"{0}\");".format(text))
    from ROOT import w, h
    return w

Output of getTextWidth(“another”) in cmain: 27728
Output of getTextWidth(“another”) in cratio: 15413

w and h are in pixels.

Dear couet,

 I am sorry for my ignorance but the width of all the canvas is 600 pixels; how can a text width be larger than 600?
 Why is the text width different in two pads of the same width?
 Best regards

Vojtech

here is an working example:

{
   TCanvas *c1 = new TCanvas("c1","test",500,500);
   TLatex *l = new TLatex(0.1,0.5,"max = 300"); 
   l->Draw();
   UInt_t w,h;
   l->GetTextExtent(w,h,"max = 300");
   printf("%d %d\n",w,h);
}

Dear couet,

 I set the font size in pixels (I am using the font 43). Modifying your example:

{
gStyle->SetTextFont(43);
gStyle->SetTextSize(24);
TCanvas *c1 = new TCanvas(“c1”,“test”,500,500);
TLatex *l = new TLatex(0.1,0.5,“max = 300”);
l->Draw();
UInt_t w,h;
l->GetTextExtent(w,h,“max = 300”);
printf("%d %d\n",w,h);
}

yields

49167 7743

Best regards

Vojtech

Yes … weird … no answer right now.

Yes, this method is for internal use only and works only with normal text size (not pixel). Internally root does a conversion to normal text size before calling it. That’s why it works.

Dear couet,

thank you for checking. Let me then reask the original question I posted, please.
Best regards

Vojtech

  • GetTextExtend is the way to get the w and h of the text string
  • it works only with normal text size (precision <3)
  • TAttText does the conversion for pixel to normal text size with SetTextSizePixels root.cern.ch/doc/master/classTA … 72e8f24469
    you can try to mimic it.

This is not very user friendly because it was never meant to be used by end users… May be if you explain what is your goal I may think of an other way.

Dear couet,

thank you for your suggestion. My goal is to write a script that places a text to a TPad in such a way that that it does not overlap with other objects in the TPad. For this, I need a method that returns maximum (and minimum) of objects like TH1, THStack, TGraph in the x-range over which the text spans. Obviously, I need to know this x-range and for this I need to know the text width. 
If you invent a method that would return the text width regardless of which font size and type one uses, this would be wonderful. I think that many people would appreciate it.
Best regards

Vojtech

An other approach could be to use TPaveText. You specify a box containing the text … the text size is adjusted automatically

Dear couet,

thank you but this is definitely not the way to go. I need to have control of the text size. Moreover, all the text sizes in the TPad (and in all TCanvas) must be the same (in a paper, one does not change the font size from paragraph to paragraph). Btw. this is the reason why I use fonts of the "3" family.
Best regards

Vojtech

I will provide a macro.

Here it is
TextExtentTest.C (924 Bytes)

[quote=“couet”]Of course this functionality exists because we need it to define the text alignment. It is GetTextExtent:
root.cern.ch/doc/master/classTT … fcde818590[/quote]

Dear Coute,

I also want to get the text width in my program. The function GetTextExtent works well for normal stings without TLatex symbols. When the text havs latex symbols like #delta, this function estimate the raw text width instead of the “real” width of the symbol δ.
As you said there must be method to estimate the “real” width of TLatex for alignment, but I couldn’t find it. Can you tell me the method to do that?

Thanks in advance,
Yicheng

try: root.cern.ch/doc/master/classTL … eb69090805