TLegend issue when saving to pdf

Hello,

I found an issue with the margins of a TLegend.
The plot produced by the macro below looks perfectly fine in the root window. However, once the canvas is exported to pdf the superscript -3 in one of the legend entries gets out of the border, as you can see in the attached file.

test.pdf (15.9 KB)

The issue appears to change with the distance from the right margin and the change of the default text size of the entries, since it shows up only in the bottom pad. I can probably find a workaround playing with gPad->SetRightMargin, anyhow I would like to find a better solution.
Any suggestion?

Greetings,
A.


_ROOT Version: 6.14/06
_Platform: macosx64 OS version 10.12.6
Compiler: Not Provided


{

  gStyle->SetOptTitle( kFALSE );
  gStyle->SetOptStat( kFALSE );
  
  gStyle->SetPadTickX( kTRUE );
  gStyle->SetPadTickY( kTRUE );

  gStyle->SetPadLeftMargin(0.11);
  gStyle->SetPadRightMargin(0.037);
  gStyle->SetPadTopMargin(0.055);
  gStyle->SetPadBottomMargin(0.12);

  gStyle->SetTitleSize(.05, "XY");
  gStyle->SetTitleXOffset(1.2);
  gStyle->SetTitleYOffset(.8);
  gStyle->SetLabelSize(.05, "XY");
  gStyle->SetLabelOffset(.00, "Y");
  gStyle->SetStripDecimals( kFALSE );

  Double_t width = 840.;
  Double_t aFrac = 674./width;

  Double_t xValues[5] = {1., 2., 3., 4., 5.};
  Double_t yValues[5] = {.2, .4, .1, .5, .6};
  TGraph *aGraph = new TGraph(5, xValues, yValues);

  TCanvas *aC = new TCanvas("aC", "aC", width, 1000);
  aC->Divide(1,2);
  aC->cd(1);
  gPad->SetLeftMargin(.075);
  gPad->SetRightMargin( 1 - aFrac );

  aGraph->Draw("AP");
  aGraph->SetTitle("aGraph; some X; some Y;");
  
  TLegend aLegend(aFrac+.02, gPad->GetBottomMargin(), .99, 1.-gPad->GetTopMargin());
  aLegend.AddEntry( (TObject*)NULL, "some label", "");
  aLegend.AddEntry( aGraph, "aGraph", "P");
  aLegend.AddEntry( (TObject*)NULL, "something#times10^{-3}", "");
  //  aLegend.AddEntry( (TObject*)NULL, "^{7}Li(p,n)^{7}Be#times10^{-3}", "");
  for(Int_t i=0;i<3;i++)
    aLegend.AddEntry( (TObject*)NULL, Form("a label %d", i), "");
  TLegend *anotherLegend = (TLegend*)aLegend.Clone();
  anotherLegend->Draw();
  
  gPad->Modified();
  gPad->Update();

  aC->cd(2);
  gPad->SetLeftMargin(.075);
  gPad->SetRightMargin( 1 - aFrac );

  aGraph->Draw("AP");

  for(Int_t i=3;i<8;i++)
    aLegend.AddEntry( (TObject*)NULL, Form("a label %d", i), "");

  aLegend.Draw();
  
  gPad->Modified();
  gPad->Update();

  //  aC->SaveAs("test.pdf");

}

The pdf output is not a direct screen copy. It is produced by a dedicated class (TPDF). The correspondence between the text rendered on screen and the PDF output is done as best as possible but in some cases like yours some small differences can be seen. You can do:

  TLegend aLegend(aFrac+.02, gPad->GetBottomMargin(), .99, 1.-gPad->GetTopMargin());
  aLegend.SetTextSize(0.033);

Thank you for your answer.

There is some other format I may choose, to be afterwards converted to pdf, that would avoid this issue?

Greetings,
A.

You can you png but you will loose the vector graphics side of pdf.

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