# TLatex text overflowing histogram border

**URL:** <https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548>\
**Category:** Newbie\
**Tags:** root\
**Created:** [March 15, 2024, 7:29am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548 "2024-03-15T07:29:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 15, 2024, 7:29am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/1 "2024-03-15T07:29:54Z")

</div>

Hi all,

I am trying to add some text with x coordinate centred at the pad and the Y coordinate just above a histogram (with some offset)  
However Setting X = 0.5 and Y = 0.9 draws the text but it overflows the histogram border(not sure of exact term here) as seen below

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/7/a75f0a3ddfdbc652633388203c669730985e7db2.png)

Is it possible to parametrically re-adjust the top border so that the test is inside the border and does not overlap with the histogram?

```auto
void hist() {
  
    TCanvas *canvas = new TCanvas("canvas", "", 800, 600);
   
    TH1F *histogram = new TH1F("histogram", "", 100, -5, 5);
   
    TRandom3 randomGenerator;
    for (int i = 0; i < 10000; ++i) {
        double value = randomGenerator.Gaus(0, 1); // Generate random numbers following a Gaussian distribution with mean 0 and standard deviation 1
        histogram->Fill(value);
    }

    histogram->Draw();

    TLatex *textTop = new TLatex(0.5, 0.9, "Top Text");
    textTop->SetNDC();
    textTop->SetTextSize(0.03);
    textTop->SetTextAlign(22); // Center align
    textTop->Draw();
    canvas->Update();
}

```

Thanks a lot

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 15, 2024, 8:27am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/2 "2024-03-15T08:27:08Z")

</div>

> [@utkarsh-2000](#):
>
> ```auto
> textTop->SetTextAlign(22); // Center align
> 
> ```

see [ROOT: TAttText Class Reference](https://root.cern/doc/master/classTAttText.html#ATTTEXT1)

you need: 21

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 15, 2024, 11:02am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/3 "2024-03-15T11:02:33Z")

</div>

Thanks for sharing the correct code, 21.

However, the problem persists; I am trying to generate the TTest at the position shown below with some offset from the histogram maximum.  
I can move the TText through GUI; however not able to create some extra space between the top border and the histogram Y axis minima.  
Sorry if I missed something in the solution.

Like the way in the text is added to the image below  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/7/4/746aac6501156e3c6093cb21360ebdd03309a0c2.png)

> [@Applying cut on histogram](https://root-forum.cern.ch/t/applying-cut-on-histogram/28404):
>
> Dear Friends, I want to apply cut on the attached histogram around 400 GeV. Could you please guide me how can I do that? Cheers [1]

Thanks a lot !

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 15, 2024, 11:08am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/4 "2024-03-15T11:08:16Z")

</div>

Can you provide an example ?

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 15, 2024, 11:20am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/5 "2024-03-15T11:20:20Z")

</div>

Do you mean a macro or just an image of what I am trying to achieve ?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 15, 2024, 11:28am UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/6 "2024-03-15T11:28:26Z")

</div>

A macro reproducing the problem

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 15, 2024, 1:41pm UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/7 "2024-03-15T13:41:15Z")

</div>

Actually, I just started the task; the original macro is the same as the above, expect it draws a histogram from a root file, which I changed t o make it independent.  
The main issue is that when I draw the TText, shouldn’t the axis auto-adjust to include it inside the graph?

---

<div class="post-metadata">

**Author:** ![Shiva\_Prasad\_Nayak](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shiva_prasad_nayak/32/36391_2.png) [@Shiva\_Prasad\_Nayak](https://root-forum.cern.ch/u/Shiva_Prasad_Nayak)\
**Post date:** [March 15, 2024, 2:08pm UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/8 "2024-03-15T14:08:38Z")

</div>

If I understand you correctly (and correct me if I’m mistaken) you want some space between y axis maxima and peak of histogram. You can do so by adding this to your macro.

> ```
> histogram->GetYaxis()->SetRangeUser(0,700);
> 
> ```

For better understanding read [this](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254).

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 15, 2024, 2:34pm UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/9 "2024-03-15T14:34:03Z")

</div>

Maybe you want

```auto
canvas->SetTopMargin(somevalue);

```

and similar for the Left, Right, and Bottom margins, which define the space from the edges of the canvas to the frame of the histogram. Try e.g. with 0.02 and play around with other values from there.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 18, 2024, 12:07pm UTC](https://root-forum.cern.ch/t/tlatex-text-overflowing-histogram-border/58548/10 "2024-03-18T12:07:42Z")

</div>

Thanks @dastudillo @Shiva_Prasad_Nayak for your help.

I think I managed to do that by merging both of your suggestions.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/f/1/f1be18c4e84a4f310502cda33250bf5cd4e6204f.png)

```auto
void hist() {
  
    TCanvas *canvas = new TCanvas("canvas", "", 800, 600);
  
  TH1F *histogram = new TH1F("histogram", "", 100, -5, 5);

    TRandom3 randomGenerator;
    for (int i = 0; i < 10000; ++i) {
        double value = randomGenerator.Gaus(0, 1); // Generate random numbers following a Gaussian distribution with mean 0 and standard deviation 1
        histogram->Fill(value);
    }
    histogram->Draw();
    TLatex *textTop = new TLatex(0.5, 0.85, "Top Text");
    textTop->SetNDC();
    textTop->SetTextSize(0.03);
    textTop->SetTextAlign(21); // Center align
    textTop->Draw();

    histogram->GetYaxis()->SetRangeUser(0, 520);
    canvas->SetTopMargin(0.1);
}

```

Probably a hard-coded way of doing this, but guess it works.
