Right or left alignment of legend header

Hi ROOTers,
I would like to set right alignment for the header of my legend. As far as I read from root page, only center alignment is available like this

leg->SetHeader("my legend title", "C");

Is there anyway to align it to the left or right with an option like “R” for right and “L” for left?

Kind regards,
Haidar.

From the documentation, you can set the alignment first for the whole legend (which includes the header), and then set alignment entry by entry; thus, you can first set the alignment you want for the header (don’t change it when creating the header) and then change the alignment of the other entries; eg:

void test() {
   gStyle->SetOptStat(0);
   TCanvas *c4 = new TCanvas("c", "c", 600,500);
   TH1D *hpx = new TH1D("hpx","This is the hpx distribution",100,-4.,4.);
   hpx->FillRandom("gaus", 50000);
   hpx->Draw("E");
   hpx->GetYaxis()->SetTitle("Y Axis title");
   hpx->GetYaxis()->SetTitleOffset(1.3); hpx->GetYaxis()->CenterTitle(true);
   hpx->GetXaxis()->SetTitle("X Axis title");
   hpx->GetXaxis()->CenterTitle(true);
 
   TH1D *h1 = new TH1D("h1","A green histogram",100,-2.,2.);
   h1->FillRandom("gaus", 10000);
   h1->SetLineColor(kGreen);
   h1->Draw("same");
 
   TGraph *g = new TGraph();
   g->SetPoint(0, -3.5, 100 );
   g->SetPoint(1, -3.0, 300 );
   g->SetPoint(2, -2.0, 1000 );
   g->SetPoint(3,  1.0, 800 );
   g->SetPoint(4,  0.0, 200 );
   g->SetPoint(5,  3.0, 200 );
   g->Draw("L");
   g->SetTitle("This is a TGraph");
   g->SetLineColor(kRed);
   g->SetFillColor(0);

   TLegend *leg = new TLegend(0.5, 0.5, .95, .7);
   leg->SetTextAlign(32);
   leg->SetHeader("my legend title");
   TLegendEntry *le1 = leg->AddEntry(hpx, "hpx", "l");
   TLegendEntry *le2 = leg->AddEntry(h1, "h1", "l");
   TLegendEntry *le3 = leg->AddEntry(g, "graph", "l");
   le1->SetTextAlign(12);
   le2->SetTextAlign(22);
   // if we don't change le3, it keeps the alignment of leg (32 in this case)
   leg->Draw();
}

c

Thank you so much!! It is exactly what I am looking for :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.