TLatex symbol height and width

Dear Experts,

I meet a problem to get height and width of a TLatex formula / symbol. The function GetTextExtent works well for normal stings without latex symbols. When the text has latex symbols like “#delta”, this function estimates the width (height) of the six characters instead of the symbol δ.
Is there any way to get the real height and width of a TLatex?

Best Regrads,
Yicheng

GetTextExtent is a method inherited from TText.
It does not have a TLatex version whereas TLatex::GetBoundingBox does.

Just do be sure ca you provide a few lines of code showing your problem ?

[quote=“couet”]GetTextExtent is a method inherited from TText.
It does not have a TLatex version whereas TLatex::GetBoundingBox does.

Just do be sure ca you provide a few lines of code showing your problem ?[/quote]

Thank you for the comment.
I have a python script which want to get the text width for text alignment.
Since I can not find the type UInt_t & (required by GetTextExtent) in python (array.array(‘I’,[0]) does not work), I use a C++ approach.
You can find the problem I mentioned in the output log.

Python part:

# Entries will be add to a Legend
# entry[1] is the name of the entry
for entry in entries:
     w = get_text_extent(pad, entry[1], text_size)
     logger.debug('legend entry width - {0}: {1}'.format(entry[1], w))

C++ part:

[code]
int get_text_extent(TPad* pad, const char* text, double text_size=0.04, int text_font=62)
{
// if (is_tlatex(text)) {return 0;}
pad->cd();
UInt_t w = 0;
UInt_t h = 0;
TText* t_text = new TText(0, 0, text);
t_text->SetTextSize(text_size);
t_text->SetTextFont(text_font);
t_text->Draw();
t_text->GetTextExtent(w, h, text);
t_text->Delete();

return  to_int(w);

}[/code]

Output log:

DEBUG - plot_template - legend entry width - Data: 41
DEBUG - plot_template - legend entry width - Z: 12
DEBUG - plot_template - legend entry width - W: 18
DEBUG - plot_template - legend entry width - Diboson: 75
DEBUG - plot_template - legend entry width - Top: 35
DEBUG - plot_template - legend entry width - ggH2000NWZZvvqq: 178
DEBUG - plot_template - legend entry width - #sigma_{stat}: 124    #  Here is the problem

The function returns 124 pixel for the text “#sigma_{stat}”. In fact when “#sigma_{stat}” is drawn in TLegend, it is as wide as the text “Top”, i.e. around 35 pixel.
Apparently the function regards “#sigma_{stat}” as a plain string. Now the question is how can I get the real width of it?

Best Regards,
Yicheng

If it is only for text alignment you can have a look at:
root.cern.ch/doc/master/classTAttText.html#T1

Otherwise, as I said, for Latex you should use GetBoundingBox
all your examples use GetTextExtent

[quote=“couet”][quote]
I have a python script which want to get the text width for text alignment.
[/quote]

If it is only for text alignment you can have a look at:
root.cern.ch/doc/master/classTAttText.html#T1

Otherwise, as I said, for Latex you should use GetBoundingBox
all your examples use GetTextExtent[/quote]

Sorry I missed it in your first comment. GetBoundingBox works for me.

Thank you very much,
Yicheng