Latex in Histogram Titles


I would like to use latex symbols in my histogram titles and axis labels, but I have run into a few issues.

First, how do I simply display a # sign without it being interpreted as the escape character for TLatex? Using two ## gives me back two ## signs.

Secondly, I would like to export this to a tex output and often have something written in latex as:

This would render only the $\Delta E$ in math mode and the rest in normal mode. I do not know how to accomplish this in ROOT. I can write the label in ROOT as

but this causes in error in latex as the outputed latex label is

$Energy Difference (\DeltaE) and \DeltaE is an unknown control sequence. If I then change the ROOT label to

Energy Difference (#Delta E) the error about \DeltaE is avoided, but the label is rendered entirely in math mode removing all the spacing even that which was anticipated between Energy and Difference.

Of course this can easily be corrected by adjusting the resultant tex output, but the idea is to minimize this type of editing. Would this be considered a bug or ignorance of TLatex on my part?


if ## gives you ## then # gives you #

"\\mbox{Energy Difference } (\\Delta E)"

The single # sign does not always work. For example the following will not display a # sign:

I realize if I wrap the #4 in an mbox then it will appear again, but this solution is not immediately apparent.

The issue I seem to have trouble understanding is why ROOT renders latex in what appears to be opposite to what one would expect. Usually you would need to initiate math mode to make symbols with the $, but it appears ROOT defaults to math mode and you have to escape it with mbox. Does TLatex recognize the math mode escape character, $ ? Is there a way to make TLatex follow the latex standard more closely?

It would be simpler and clearer if one code write the Latex titles as

instead of

As soon as you use a \ in a text then the TMathText tool is used.
TMathText, as its name indicates, enter the Math mode of TeX
Which means it is like having virtual $ $ around your text.
So you are in math mode always. therefore if you want text with space you enclose with mbox.
So I guess there is nothing to fix. I can mark this issue as solved.

Why is it treated this way? I would have expected TLatex to handle math mode as well with the $ characters.

The only bug I could see then would be when exort the following label to latex:

It does not invoke TMathText as there is no , but causes error in latex since the control sequence \DeltaE is written in the tex file. Also the text “Energy Difference” is rendered in math mode in the tex file.

So the end result is that whenever using latex titles and exporting to tex one has to wrap normal text in mbox?

The Class TMathText is meant to render LaTeX Math mod … See the help of this class. That’s not LAtex Text, That’s Latex Text in MAth mod

not latex, but TMathTex

Energy Difference (#DeltaE)

I guess put a space: #Delta E

This then leads to the entire label in math mode in the tex file as I stated in the original post.

"Energy Difference (#Delta#mbox{E})"

[quote=“couet”] "Energy Difference (#Delta#mbox{E})" [/quote]
The tex output of this still renders the entire label in math mode:

This removes the spacing between Energy and Difference and does not render the E in math mode as is appropriate.

I am also still have issue with the rendering of a pound sign as in the following:

Title #beta #4 renders in tex as

$Title \beta \4$ where the \4 is an unknown sequence.

Why is it treated this way? I would have expected TLatex to handle math mode as well with the $ characters.

The Class TMathText is meant to render LaTeX Math mod … See the help of this class. That’s not LAtex Text, That’s Latex Text in MAth mod[/quote]

My point here is that I feel ROOT is treating the Latex rendering in a sort of strange way. I would expect TLatex to be a class that can handle any latex even math mode. Instead of automatically switching to use TMathText when \ is in the label and rendering the entire label why can’t only a portion be rendered in that way? I would like to use the $ to evoke math mode as one does in Latex and then render everything outside in a normal text mode.

To summarize I would expect TLatex to see the $$ and render that portion with TMathText and render the rest as normal Latex.

I’ve also noticed that labels rendered with TMathText do not export to pdf.

That’s not how it is … TLatex is the old TLatex class which is in ROOT since the beginning. It did not have a very good quality to render math formulae. A new Class written by a ROOT user: TMathtex has been introduced recently. It is math mod !!! it assumes $ $…To facilitate the migration TLatex call TMathtex if there is \ in the string. Then I have implemented the Tex output … That is really TeX now … Also it encloses the output with $$ as soon as there is Math character like Greek …

I think we can stop the discussion here, as there is nothing to fix.

[quote=“couet”] "Energy Difference (#Delta#mbox{E})" [/quote]

I’m posting this example because your proposed solution here does not produce the correct tex output.

void latexFigure ()
  TCanvas  *canvas = new TCanvas("canvas","Canvas");
  TH1F *hist = new TH1F("hist","Title #4;Energy Difference (#Delta#mbox{E});Counts",100,-5,5);

The resulting ROOT histogram:

The resulting labels in tex are

{$Title \4 \beta$}


{$Energy Difference (\Delta\mbox{E})$}

The resulting error form pdflatex:

! Undefined control sequence. l.210 ...) node[scale=1.56234, rotate=0]{$Title \4 \beta $}; ?

After correcting the tex file for the “#4” error the pdf output:

There are two problems! First, the “#4” in the ROOT title converts to “\4” in the tex output. Second, the entire label is rendered in math mode.

I am working on something else I would like to finish. The best solution I can propose for now is:

  TCanvas  *canvas = new TCanvas("canvas","Canvas");
  TH1F *hist = new TH1F("hist","Title nb 4;#mbox{Energy Difference }(#Delta#mbox{E});Counts",100,-5,5);

I understand there are many projects that need attention and I appreciate your discussion so far.

Of course I can do as you suggest, but I still believe that this is fundamentally just a hack around a bug. I will continue to just correct the labels in the tex output, but I believe this issue should be tracked and taken care of at some point in the future. The implementation of latex labels in ROOT should follow the latex syntax more closely and only render in math mode when it is indicated by the user with the $ symbol.

Thanks again.

I will look tomorrow how to display the # (the control character for TLatex) in the tex output.

Looking closer it seems not realistic to pretend that TLatex will match exactly Latex in TTexDump. The control character # is one thing, but there is many other TLatex extension which do not have equivalent in the normal Latex (#kern, #splitline, etc …). So it is better to leave it as it is right now.