Creating .tex file using C++ program

I first time realise the importance of programming. I need to make a tex file to plot graphs in latex file using sage. For this I had to write code separately for each of equation. So i used C++ program( suggested by Rai sir)  to do this for me. In short what i have done is:

I placed equations in a file separated by spaces. Then i wrote c++ program that reads the equation from the file and put in array. Using ‘for loop’ and ‘cout’ i wrote that code of tex file that was to be repeated by just changing the equation.

Thats all. 🙂

C++ program: (

using namespace std;
int main()

string str[5];
ifstream myfile("eqn.txt");
if(!myfile) //Always test the file open.
                cout<<"Error opening output file"<<endl;
                return -1;
for(int i=0;i<5;i++)
getline(myfile,str[i],' ');

cout<<"\\title{Curve Tracing by Sage in Latex}"<<endl;
cout<<" \\author{Priyanka Kapoor}"<<endl;
cout<<" \\begin{document}"<<endl;
cout<<" \\maketitle"<<endl<<endl;
for(int i=0;i<5;i++)
cout<<" f(x) = "<<str[i]<<endl;
cout<<" c = plot(f,(x,-2,2),thickness = 2.0,legend_label='"<<str[i]<<"')"<<endl;
}cout<<" \\end{document}";
return 0;

File for input (eqn.text)

x^2 sin(x) cos(x) x+1 x^2-1

And the Tex file produced is :

\title{Curve Tracing by Sage in Latex}
 \author{Priyanka Kapoor}


 f(x) = x^2-1

 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='x^2-1')


 f(x) = x^2
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='x^2')


 f(x) = sin(x)
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='sin(x)')


 f(x) = cos(x)
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='cos(x)')


 f(x) = x+1
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='x+1')



About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s