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: (tex.cc)

#include<iostream>
#include<string>
#include<fstream>
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;
}
while(!myfile.eof())
{
for(int i=0;i<5;i++)
{
getline(myfile,str[i],' ');

}}
cout<<"\\documentclass{article}"<<endl;
cout<<"\\title{Curve Tracing by Sage in Latex}"<<endl;
cout<<" \\author{Priyanka Kapoor}"<<endl;
cout<<"\\usepackage{sagetex}"<<endl<<endl;
cout<<" \\begin{document}"<<endl;
cout<<" \\maketitle"<<endl<<endl;
for(int i=0;i<5;i++)
{cout<<endl;
cout<<"\\begin{sageblock}"<<endl;
cout<<" f(x) = "<<str[i]<<endl;
cout<<" c = plot(f,(x,-2,2),thickness = 2.0,legend_label='"<<str[i]<<"')"<<endl;
cout<<"\\end{sageblock}"<<endl<<endl;
cout<<"\\begin{center}";
cout<<"\\sageplot{plot(f,(x,-2,2),figsize=(3,3),aspect_ratio=1,legend_label='"<<str[i]<<"')}\\end{center}"<<endl<<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 :

\documentclass{article}
\title{Curve Tracing by Sage in Latex}
 \author{Priyanka Kapoor}
\usepackage{sagetex}

 \begin{document}
 \maketitle

\begin{sageblock}
 f(x) = x^2-1

 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='x^2-1')
\end{sageblock}

\begin{center}\sageplot{plot(f,(x,-2,2),figsize=(3,3),aspect_ratio=1,legend_label='x^2-1
')}\end{center}

\begin{sageblock}
 f(x) = x^2
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='x^2')
\end{sageblock}

\begin{center}\sageplot{plot(f,(x,-2,2),figsize=(3,3),aspect_ratio=1,legend_label='x^2')}\end{center}

\begin{sageblock}
 f(x) = sin(x)
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='sin(x)')
\end{sageblock}

\begin{center}\sageplot{plot(f,(x,-2,2),figsize=(3,3),aspect_ratio=1,legend_label='sin(x)')}\end{center}

\begin{sageblock}
 f(x) = cos(x)
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='cos(x)')
\end{sageblock}

\begin{center}\sageplot{plot(f,(x,-2,2),figsize=(3,3),aspect_ratio=1,legend_label='cos(x)')}\end{center}

\begin{sageblock}
 f(x) = x+1
 c = plot(f,(x,-2,2),thickness = 2.0,legend_label='x+1')
\end{sageblock}

\begin{center}\sageplot{plot(f,(x,-2,2),figsize=(3,3),aspect_ratio=1,legend_label='x+1')}\end{center}

 \end{document}
Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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