%%% generate latex document and inerts the table
%%%%% []=tabtex2texpdf(name,doamble,titlep,table_,portrait)
function []=tabtex2texpdf(name,doamble,titlep,table_,portrait,scriptsize)
if nargin<4
    disp('not enoght input arguments in tabtex2texpdf');
end
if nargin==4;
    portrait=1;
    
end
if nargin<6
    scriptsize='';
end
if isempty(titlep)|titlep==0
    titlestuff=[];
else
    titlestuff=strvcat('\begin{titlepage}',...
['\title{',titlep,'}'],...
'\author{Your Name\\Affiliation \\ Address\\ E-mail: {}}',...
'\date{\today}',...
' \maketitle',...
' \end{titlepage}',...
'%\tableofcontents',...
'\newpage');
end
amble=[];
endamble=[];
if doamble==1
    amble=strvcat('\documentclass[12pt,a4paper]{article}%',...
'\usepackage{graphicx,lscape}',...
'\usepackage{amsmath}',...
'\usepackage{epsfig}',...
'\usepackage{amsfonts}%',...
'\usepackage{amssymb,longtable,hyperref}',...
' ',...
'\begin{document}',...
['{',scriptsize]);
endamble=[];
if portrait==0
   amble=strvcat(amble,'\begin{landscape}');
    endamble='\end{landscape}';
end
endamble=strvcat(endamble,'}','\end{document}');
end


pream=strvcat(amble,...
titlestuff,...
table_,...
endamble);

pream2=table_;
ccdd=cd;
if ~exist('tex_stuff','dir')
    mkdir('tex_stuff')
end
tex_name=name;%input(upper('Give the name of the tex file to save (no extension)'));
tex_name2=[pwd,'/tex_stuff/',tex_name,'tabonly.tex'];
tex_name=[pwd,'/tex_stuff/',tex_name,'.tex'];

fid=fopen([tex_name],'w+');
fid2=fopen([tex_name2],'w+');
if fid<0
    disp('problems with the file: it might be open')
    return
end
fprintf(fid,'%s\n',pream(1,:));
fclose(fid);
fprintf(fid2,'%s\n',pream2(1,:));
fclose(fid2);
for jj=2:size(pream,1)
  fid=fopen([tex_name],'a+');
fprintf(fid,'%s\n',pream(jj,:));
fclose(fid); 
end
for jj=2:size(pream2,1)
  fid2=fopen([tex_name2],'a+');
fprintf(fid2,'%s\n',pream2(jj,:));
fclose(fid2); 
end

% compile
if doamble==1
%cd tex_stuff -no-file-line-error
% system(['latex  -output-directory=tex_stuff ', [ccdd,'/tex_stuff/',name,'.tex  ']]);
% system(['latex  -output-directory=tex_stuff ', [ccdd,'/tex_stuff/',name,'.tex  ']]);

system(['latex  -output-directory=tex_stuff ', ['tex_stuff/',name,'.tex  ']]);
system(['latex  -output-directory=tex_stuff ', ['tex_stuff/',name,'.tex  ']]);
%dos(['latex --quiet -output-directory=tex_stuff ', [ccdd,'\tex_stuff\',name,'.tex  ']]);

% system(['dvipdfm  -p a4  -r 800  -o ',[ccdd,'/tex_stuff/',name,'.pdf '],[ccdd,'/tex_stuff/',name,'.dvi']]);
system(['dvipdfm  -p a4  -r 800  -o ',['tex_stuff/',name,'.pdf '],['tex_stuff/',name,'.dvi']]);
% -l for landscape
end
try
fclose(fid);
fclose(fid2);
catch
end