{ TCanvas *textc = new TCanvas("textc","Example of text",1);char cid[8]; // 使用固定大小的数组来代替动态分配内存 TLatex *lid; TLatex *l;for (int i=1; i<15; i++) {snprintf(cid,8,"ID %d :", i); // 使用snprintf代替sprintf lid = new TLatex(0.1,1-(double)i/15, cid);lid->SetTextFont(62);lid->Draw(); l = new TLatex(.2,1-(double)i/15,"The quick brown fox is not here anymore");l->SetTextFont(i*10+2);l->Draw(); }}
#include<iostream>#include<TCanvas.h>#include<TLatex.h>voiddrawLatex() { TCanvas *c1 =newTCanvas("c1","Latex Example",800,600);c1->cd(); // Create a TLatex object and set the position and text TLatex *latex =newTLatex(0.5,0.5,"This is a LaTeX expression: #frac{1}{2} #int_{0}^{#infty} e^{-x} dx");latex->SetTextAlign(22);latex->SetTextSize(0.04); // Draw the latex expressionlatex->Draw();c1->Update();}
在上面的代码中,TLatex对象被创建并设置了位置和文本内容。
使用SetTextAlign()函数设置文本的对齐方式;
使用SetTextSize()函数设置文本的大小;
使用SetTextColor()函数来设置文本的颜色。
然后,使用Draw()函数将LaTeX表达式绘制出来。
E.g. 只含公式的文字绘制
voidlatex() { TCanvas *c1 = new TCanvas("c1","test",600,700);// write formulas TLatex l;l.SetTextAlign(12);l.SetTextSize(0.04);l.DrawLatex(0.1,0.9,"1) C(x) = d #sqrt{#frac{2}{#lambdaD}}#int^{x}_{0}cos(#frac{#pi}{2}t^{2})dt");l.DrawLatex(0.1,0.7,"2) C(x) = d #sqrt{#frac{2}{#lambdaD}}#int^{x}cos(#frac{#pi}{2}t^{2})dt"); l.DrawLatex(0.1,0.5,"3) R = |A|^{2} = #frac{1}{2}#left(#[]{#frac{1}{2}+C(V)}^{2}+#[]{#frac{1}{2}+S(V)}^{2}#right)");
l.DrawLatex(0.1,0.3,"4) F(t) = #sum_{i=-#infty}^{#infty}A(i)cos#[]{#frac{i}{t+i}}");l.DrawLatex(0.1,0.1,"5) {}_{3}^{7}Li");}