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?
[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();
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?