function latex_project(pn)

fd = ['~/latex/' pn ];
pd = [fd '/figs'];
figlist = get(0,'children');
fp = create_structure(fd,pn);

for i = 1:length(figlist)

name = ['figure' num2str(figlist(i)) '.eps'];

print(figlist(i),'-depsc2',[pd '/' name]);

fprintf(fp,'\\begin{center}\n');
fprintf(fp,['\\includegraphics[width=0.5\\textwidth]{figs/' name '}\n']);
fprintf(fp,'\\end{center}\n\n');

end;

close_file(fp);
return;

function fp = create_structure(fd,pn)
unix(['rm -Rf ' fd]);
mkdir(fd);
mkdir([fd '/figs']);
fp = fopen([fd '/' pn '.tex'],'w');


fprintf(fp,'\\documentclass[11pt,a4paper]{article}\n');
fprintf(fp,'\\usepackage{amsmath}\n');
fprintf(fp,'\\usepackage{amssymb}\n');
fprintf(fp,'\\usepackage{calc}\n');
fprintf(fp,'\\usepackage[dvips]{graphicx}\n');
fprintf(fp,'\n');
fprintf(fp,'\\setlength{\\textwidth}{6.3in}\n');
fprintf(fp,'\\setlength{\\topmargin}{-0.8in}\n');
fprintf(fp,'\\setlength{\\textheight}{9.3in}\n');
fprintf(fp,'\\setlength{\\oddsidemargin}{(\\paperwidth-\\textwidth)/2-1in}\n');
fprintf(fp,'\\setlength{\\evensidemargin}{(\\paperwidth-\\textwidth)/2-1in}\n');
fprintf(fp,'\\setlength{\\parindent}{0in}\n');
fprintf(fp,'\\setlength{\\parskip}{\\baselineskip}\n');
fprintf(fp,'\\DeclareGraphicsRule{.emf}{bmp}{}{}\n');
fprintf(fp,'\\DeclareGraphicsRule{.wmf}{bmp}{}{}\n');
fprintf(fp,'\\newcommand{\\ud}{\\mathrm{d}}\\usepackage{amsmath}\n');
fprintf(fp,'\\newcommand{\\ft}{\\mathbb{F}}\n');
fprintf(fp,'\n');
fprintf(fp,'\\begin{document}\n');
return;

function close_file(fp)
fprintf(fp,'\\end{document}');
fclose(fp);
return;
