.tex image made with root, text does not scale in latex

Hi All,

I recently learned that with root, you can export tikz files from you canvasses, which is great! Now I’m including it in my latex file and I find that when I make the image smaller or bigger, the text size changes as well. I was actually not expecting this, since I was under the assumption that if you include a tikz image, you can resize it without affecting the text.

What is great, is that the font of all the text in the image is exactly the font that I use for my latex document, so that seems to work. I was wondering if anyone has the same experience, or maybe someone knows if I’m doing something wrong, or if my expectations are wrong :slight_smile:

This is how I include the image in latex:

			\begin{figure}
				\centering
				\scalebox{0.5}{\input{./figures/rawClusters_Ag.tex}}
				\caption{Image ({hpx.tex}) generated thanks to {TTeXDump}}
			\end{figure}

And I make the plots like this (I do not include all the code here because it is rather big…)

// This is where all the histos are made in the same way
TH1D *allMyHistos(unsigned int metal, unsigned int color, unsigned int nColors, bool clusters, bool strips){
  TH1D *myHist = (TH1D*)allMyFiles->Get("h1");
  myHist->SetStats(false);
  // Titles
  gStyle->SetOptTitle(0);
  //myHist->GetXaxis()->SetTitleFont(132);
  //myHist->GetYaxis()->SetTitleFont(132);
  //myHist->GetXaxis()->SetLabelFont(132);
  //myHist->GetYaxis()->SetLabelFont(132);
  //myHist->GetXaxis()->SetTitleSize(0.045);
  //myHist->GetYaxis()->SetTitleSize(0.045);
  //myHist->GetXaxis()->SetLabelSize(0.045);
  //myHist->GetYaxis()->SetLabelSize(0.045);
  myHist->GetXaxis()->SetTitle("Vcth [dac]");
  if(clusters) myHist->GetYaxis()->SetTitle("Clusters/Event");
  else myHist->GetYaxis()->SetTitle("Hits/Event");
  // Axes ranges
  myHist->GetYaxis()->SetRangeUser(1e-8,1e2);
  myHist->GetXaxis()->SetRangeUser(88,122);
  // Line width
  //myHist->SetLineWidth(2);
  // Colors for the markers and lines
  if(nColors==4) myHist->SetMarkerColor(startColorsFour[metal]+color), myHist->SetLineColor(startColorsFour[metal]+color);
  else if(nColors==10) myHist->SetMarkerColor(startColorsTen[metal]+color), myHist->SetLineColor(startColorsTen[metal]+color);
  else if(nColors==6) myHist->SetMarkerColor(allColors[metal]), myHist->SetLineColor(allColors[metal]);
  // Now return the histogram
  return myHist;
}//end void stripHisto()

// This is where all the cancasses are made in the same way
TCanvas *allMyCanvasses(bool logScale=true){
  TCanvas *myCanvas = new TCanvas();
  // Remove a little bit of white space on the sides
  myCanvas->SetRightMargin(0.02);
  myCanvas->SetTopMargin(0.02);
  // Plot in log scale, 
  if(logScale) myCanvas->SetLogy();
  myCanvas->SetTicks();
  myCanvas->SetGrid();
  // Return the canvas:
  return myCanvas;
}//end TCanvas allMyCanvasses()

// This is where all the histos are made in the same way
TLegend *allMyLegends(unsigned int size){
  TLegend *myLegend=NULL;
  if(size==6) myLegend = new TLegend(0.14,0.65,0.46,0.94);
  else if(size==4) myLegend = new TLegend(0.14,0.74,0.46,0.94);
  else if(size==10) myLegend = new TLegend(0.14,0.45,0.5,0.94);
  else if(size==41) myLegend = new TLegend(0.14,0.74,0.4,0.94);
  else if(size==61) myLegend = new TLegend(0.14,0.65,0.65,0.94);
  //gStyle->SetLegendFont(132);
  //gStyle->SetLegendTextSize(0.045);
  return myLegend;
}//end TLegend allMyLegends()

// This is were the different plot are made
void makeNClustersMetals(unsigned int metal){
  // TCanvas to draw one, and a TLegend to add entries too
  TCanvas *c0 = allMyCanvasses();
  TLegend *l0 = allMyLegends(4);
  // Draw options for in the loop
  unsigned int colorUpdater=0;
  for (unsigned int iSensor=0; iSensor<nSensors; iSensor++){
    for (unsigned int iCbc=0; iCbc<nCbcs; iCbc++) {
      if(fileExists(sensorFileName("clusters", "nClusters", allMeasurements[metal], iSensor, iCbc))){
        allMyFiles = new TFile(sensorFileName("clusters", "nClusters", allMeasurements[metal], iSensor, iCbc), "READ");
        TH1D *plot = allMyHistos(metal, colorUpdater, 4, true, false);
        if(iCbc==0 && iSensor==0) plot->Draw();
        else plot->Draw("same");
        //l0->AddEntry(plot, Form("%s, s%dc%d", allLegends[metal], iSensor, iCbc), "l");
        l0->AddEntry(plot, Form("Sensor %d, CBC %d", iSensor, iCbc), "l");
        ++colorUpdater;
      }else{
        cout << MAGENTA << sensorFileName("clusters", "nClusters", allMeasurements[metal], iSensor, iCbc) << " does not exists!" << RESET << endl;
      }//end if fileExists()
    }//end for iCbc
  }//end for iSensor
  l0->Draw();
  c0->SaveAs(totalFileName("rawClusters", allMeasurements[metal]));
  c0->SaveAs(totalFileNamePDF("rawClusters", allMeasurements[metal]));
  
  // Let's see if we can make a tikz file :)
  gStyle->SetPaperSize(25., 17.6);
  gPad->Print(totalFileNameTIKZ("rawClusters", allMeasurements[metal]));
  
  delete c0;
}//end void makeNClustersMetals()

Cheers,
Nikkie


_ROOT Version: 6.14/06
_Platform: CentOS 7 (@cern)
_Compiler: g++


I will check but that seems correct …

I tried with:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\scalebox{0.5}{\input{c1.tex}}
\end{figure}
\end{document}

When I change the value of the scale factor, the size of the picture changes accordingly. The size of text appearing on the picture also change. It is normal as it is part of the picture. I do not see what is wrong with that,

textdump-05.pdf (19.6 KB)
textdump-25.pdf (19.7 KB)

Hi there,
unfortunately I fell for the same trap, that latex-text scales with these tikz-images and have no ‘solution’ to this either. I’m not sure how easy it is to implement but I would really enjoy having a feature with non scaled text, too!
One motivation to have this feature, at least for me, is that you might not know how big the plot has to be and always rerun your script with changed style.

btw: Maybe you’ll find this interesting. This is my try to solve this for me, but I’m struggling there as well: Modify axis of existing TCanvas

What latex text is scaling? The caption ?
I will try to run your example

There’s no text scaling, or better it scales with the scale box in lates, as in your problem.
I thought the idea might be interesting for you.
What I’m trying to accomplish is to write a fuction that saves a canvas with a variety of different axis-sizes, like e.g. ‘small’, ‘medium’ and ‘large’. This would not be ideal, but at least you just have to choose one of the sizes and don’t have to bother with recreation of the plot. But by now my function won’t pick up a new axis-style.

Hi Couet and Hviemann,

Thanks for replying so fast!

So for the text that is scaling I mean the axes labels and the numbers next to the axes. So when I have A4 paper for instance, my image could be 15 cm wide, and I would like to text size to be something like 11pt, also in my graphs. However, let’s say I change the image size, like you did with the scale box, I still want the text of the image to be 11pt for readability, so I don’t want to scale the axes labels.

I thought I could solve this by exporting the TCanvas to a tikz file. Because for instance the font that the canvas has in my latex document is “Times New Roman-like”, while if I look at the .root file it is “Arial-like”. This is what I like, because then I don’t have to worry about the font of my TCanvas any more, if I make it into a tikz file, latex takes care of the font of the TCanvas for me. But this same sort of behaviour is not seem for the font size.

Cheers,
Nikkie

The axis labels are part of the image. If you scale down the picture the axis labels scale down also. If want bigger labels you make them bigger on your initial plot so they will have the right size when you scale down in latex

In bitmaps (png, jpeg), of course it is not possible to include scale-independent elements (text in this case). For tex or vector (ps/eps) figures, I don’t know if it is possible, but I can think of using variables that could be updated (manually, though) when changing the scale. This should be easier in tex files than in postscript :slight_smile:

Here is an example of how the histogram title (``My histo’’) is coded in an exported canvas file (in my case, using root 6.14.06, these lines are by the bottom of the canvas.tex file):
\draw (10,13.0156) node[scale=2.80008, color=c, rotate=0]{My histo};
And a couple of the labels (values of 0 and 200) in the x axis:
\draw [anchor= east] (1.9,2.94906) node[scale=1.01821, color=c, rotate=0]{0};
\draw [anchor= east] (1.9,4.54855) node[scale=1.01821, color=c, rotate=0]{200};

The text sizes are controlled by the scale values (e.g. scale=1.01821), apparently related in some (unknown to me) way to the canvas size. So you could modify the canvas.tex file and replace those numbers by a variable/s that you have to define at the beginning of your main tex file (probably the latex file that will include the figures, not the canvas.tex file/s, but I’m not 100% sure), see here:

For example:
– inside main tex file? –
\def\z{2.80} % for title
\def\x{1.018} % for axis labels
– in canvas.tex –
\draw (10,13.0156) node[scale=\z, color=c, rotate=0]{My histo};
\draw [anchor= east] (1.9,2.94906) node[scale=\x, color=c, rotate=0]{0};
\draw [anchor= east] (1.9,4.54855) node[scale=\x, color=c, rotate=0]{200};
and so on…
Then, to scale the figure inside latex, you only need to change the scale factors at the top of the file.
From one of the replies in the page I mentioned, you should alternatively be able to define the variables inside the tikz (exported canvas) file, using \pgfmathsetmacro{\x}{1.018}, etc.

1 Like

Hi Couet,

Thanks for the response, I think this is where my confusion is coming from, and I think I should make sure that I have the right “PaperSize” in root. Because at first I thought this was the paper size of my document, however I believe I should tread this more like the size of the image. Then I can choose a text size that works for that image size, which results in a nice image in latex.

Cheers,
Nikkie

Hi @dastudillo,

Thanks, I’ll look into this!

Cheers,
Nikkie

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