# TLatex - Combine a string and char\*

**URL:** <https://root-forum.cern.ch/t/tlatex-combine-a-string-and-char/35477>\
**Category:** Newbie\
**Created:** [August 2, 2019, 7:28pm UTC](https://root-forum.cern.ch/t/tlatex-combine-a-string-and-char/35477 "2019-08-02T19:28:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [August 2, 2019, 7:28pm UTC](https://root-forum.cern.ch/t/tlatex-combine-a-string-and-char/35477/1 "2019-08-02T19:28:16Z")

</div>

Hi all,

I’m using the TLatex class to make a label, but I’d like it to read “Average” [string] with only one TLatex. The label is in the bottom right.

Thanks!

Here’s what I have so far.

 ![25%20PM](https://root-forum.cern.ch/uploads/default/original/3X/8/3/837b2b1ce2435135acb3365109bdc99fa2ab37a6.jpeg)

Relevant code:

```auto
 char average[64];
  int ret = snprintf(average, sizeof average, "%f", avg);

if (ret < 0) {
    return EXIT_FAILURE;
}
if (ret >= sizeof average) {
    //Result was truncated - resize the buffer and retry.
}

TLatex *avg2 = new TLatex(0.94,0.01, average);
  avg2->SetNDC();
  avg2->SetTextSize(.015);
  avg2->SetLineWidth(2);
  avg2->Draw();
  title->SetFillColor(32);
  title->Draw();

  TLatex *avg3 = new TLatex(0.87,0.01,"Average:");
  avg3->SetNDC();
  avg3->SetTextSize(.015);
  avg3->SetLineWidth(2);
  avg3->Draw();
  title->SetFillColor(32);
  title->Draw();

```

All code:

```auto
namespace {

Double_t my_transfer_function(const Double_t *x, const Double_t * /*param*/)
{
   // Bin values in our example range from -2 to 1.
   // Let's make values from -2. to -1.5 more transparent:
  if (*x>-.0001 && *x<.0001)
      return 0.001;
   return 1.;
}
}

void glgraph(){

int data = 0;
   
ifstream file { "5603_bubbles.txt" };
if (!file.is_open()) return -1;

double transArray [200][4]{};
 
for (int i{}; i != 200; ++i) {
    for (int j{}; j != 4; ++j) {
        file >> transArray[i][j];
        
    }
}

  float avg = 0.0; 
  float sum = 0.0;
  int size;

  for (int i{}; i != 200; ++i) {
    for (int j{}; j != 4; ++j) {
    size = sizeof(transArray) / sizeof(transArray[i][j]     
 );
  sum += transArray[i][j];
    }
  }
  avg = ((float)sum)/size;

  double dev[size][4];

  cout << "Average Optical Transmittance " << avg << "\n";

  char average[64];
  int ret = snprintf(average, sizeof average, "%f", avg);

if (ret < 0) {
    return EXIT_FAILURE;
}
if (ret >= sizeof average) {
    //Result was truncated - resize the buffer and retry.
}
  
 for (int i{}; i != 195; ++i) {
    for (int j{}; j != 4; ++j) {
      dev[i][j]= transArray[i][j] - avg;
    }}

 
 TH3F *hist = new TH3F("glvoxel", "OT", (size/4), 0., (size/4), 20, 0., 20., 20, 0., 20.);

   for (UInt_t i = 0; i < 200; ++i) {
     for (Int_t j = 1; j< 19;j++){
       hist->Fill(i, j, 0., dev[i][0]); //Side 1
       hist->Fill(i, 19.9, j, dev[i][1]); //Side 2
       hist->Fill(i, j, 19.9, dev[i][2]); // Side 3
       hist->Fill(i, 0.1,j , dev[i][3]); // Side 4
       }
    }
     
   
   gStyle->SetCanvasPreferGL(1);
   hist->Draw("glcolz");

   hist->GetXaxis()->SetTitle("Length (mm)");
   hist->GetXaxis()->SetTitleOffset(1.5);
   hist->GetYaxis()->SetTitle("Width (mm)");
   hist->GetYaxis()->SetTitleOffset(1.5);
   hist->GetZaxis()->SetTitle("Height (mm)");
   hist->GetZaxis()->SetTitleOffset(1.5);
   hist->GetZaxis()->SetRangeUser(-10,0);
   
   hist->SetTitle("OT");
   gStyle->SetCanvasPreferGL(1);
   gStyle->SetPalette(kRainBow);
   hist->SetFillColor(9);
   hist->Draw("glcolz");

   TPaveLabel *title = new TPaveLabel(-1., 0.86, 1., 0.98, "Sample 5603 (Bubbles) (360 nm) - Optical Transmittance Deviance");
   
   TLatex *tex = new TLatex(0.75,0.05,"Optical Transmittance Deviance (%)");
  tex->SetNDC();
  tex->SetTextSize(.015);
  tex->SetLineWidth(2);
  tex->Draw();
  title->SetFillColor(32);
  title->Draw();

  TLatex *avg2 = new TLatex(0.94,0.01, average);
  avg2->SetNDC();
  avg2->SetTextSize(.015);
  avg2->SetLineWidth(2);
  avg2->Draw();
  title->SetFillColor(32);
  title->Draw();

  TLatex *avg3 = new TLatex(0.87,0.01,"Average:");
  avg3->SetNDC();
  avg3->SetTextSize(.015);
  avg3->SetLineWidth(2);
  avg3->Draw();
  title->SetFillColor(32);
  title->Draw();

   //Now, specify the transfer function.
   TList * lf = hist->GetListOfFunctions();
   if (lf) {
      TF1 * tf = new TF1("TransferFunction", my_transfer_function);
      lf->Add(tf);
   }
}

  

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 2, 2019, 7:35pm UTC](https://root-forum.cern.ch/t/tlatex-combine-a-string-and-char/35477/2 "2019-08-02T19:35:10Z")

</div>

Try:

```auto
TLatex *avg3 = new TLatex(0.87, 0.01, TString::Format("Average: %s", average);

```

or simply:

```auto
TLatex *avg3 = new TLatex(0.87, 0.01, TString::Format("Average: %g", avg);

```
