2 |
%object: create/increment/complete/compile a tex file from within matlab |
%object: create/increment/complete/compile a tex file from within matlab |
3 |
%input: myFile is the file name |
%input: myFile is the file name |
4 |
% myStep is the operation to perform on the tex file |
% myStep is the operation to perform on the tex file |
5 |
% 0 create file starting with write2tex.header |
% 0 create file starting with title page (see myText) |
6 |
%(not yet) 1 add title or section page (see myLev) |
% 1 add section or subsection (see myLev) |
7 |
% 2 add a figure plus caption (see myFig) |
% 2 add a figure plus caption (see myFig) |
8 |
% 3 add a paragraph |
% 3 add a paragraph |
9 |
% 4 finish file |
% 4 finish file |
10 |
% 5 compile and remove temporary files (incl. *fig*.ps) |
% 5 compile (latex x 2 then dvipdf) |
11 |
% -5 compile solely |
% 6 remove temporary files (incl. *fig*.ps) |
12 |
%optional myText is a cell array of text lines (for myStep=1 to 2) |
%optional myText is a cell array of text lines (for myStep=1 to 2) |
13 |
% myLev is the title level (for myStep=1) |
% myLev is the title level (for myStep=1) |
14 |
% 0=manuscript title, 1=section, 2=subsection |
% 1=section, 2=subsection (not yet implemented) |
15 |
% myFig is a figure handle (for myStep=2) |
% myFig is a figure handle (for myStep=2) |
16 |
|
|
17 |
myText=[]; myLev=[]; myFig=[]; |
if isempty(myFile); |
18 |
if myStep>0&myStep<4; myText=varargin{1}; end; |
%if no file name, then do nothing |
19 |
if myStep==1; myLev=varargin{2}; |
return; |
20 |
|
end; |
21 |
|
if iscell(myFile); |
22 |
|
%extecute alternative command (myFile{1}) passing rest as arguments |
23 |
|
eval([myFile{1} '(myFile{2:end});']); |
24 |
|
return; |
25 |
|
end; |
26 |
|
|
27 |
|
|
28 |
|
myText=[]; myLev=[]; myFig=[]; myRdm=[]; |
29 |
|
if myStep<4; myText=varargin{1}; end; |
30 |
|
if myStep==0; myRdm=varargin{2}; |
31 |
|
elseif myStep==1; myLev=varargin{2}; |
32 |
elseif myStep==2; myFig=varargin{2}; |
elseif myStep==2; myFig=varargin{2}; |
33 |
end; |
end; |
34 |
|
|
35 |
|
%format use for printing out plots : |
36 |
|
frmt='eps'; |
37 |
|
if ispc; frmt='jpg'; end; |
38 |
|
|
39 |
%create file starting with write2tex.header |
%create file starting with write2tex.header |
40 |
if myStep==0; |
if myStep==0; |
41 |
test0=dir(myFile); |
test0=dir(myFile); |
47 |
if ~test0; |
if ~test0; |
48 |
return; |
return; |
49 |
else; |
else; |
50 |
tmp1=which('write2tex'); |
fid=fopen(myFile,'w'); |
51 |
eval(['!\cp -f ' tmp1(1:end-2) '.header ' myFile ]); |
|
52 |
|
fprintf(fid,'\\documentclass[12pt]{beamer}\n'); |
53 |
|
fprintf(fid,'%%a nice series of examples for the beamer class:\n'); |
54 |
|
fprintf(fid,'%%http://www.informatik.uni-freiburg.de/~frank/ENG/beamer/example/beamer-class-example-en-5.html\n'); |
55 |
|
fprintf(fid,'\\usepackage{multicol}\n'); |
56 |
|
|
57 |
|
fprintf(fid,'\n'); |
58 |
|
fprintf(fid,'\\newcommand\\Fontvi{\\fontsize{6}{7.2}\\selectfont}\n'); |
59 |
|
fprintf(fid,'\n'); |
60 |
|
|
61 |
|
fprintf(fid,'\\begin{document} \n\n'); |
62 |
|
|
63 |
|
fprintf(fid,'\\title{\n'); |
64 |
|
for ii=1:length(myText); fprintf(fid,[myText{ii} '\\\\ \n']); end; |
65 |
|
fprintf(fid,'}\n\n'); |
66 |
|
fprintf(fid,'\\date{\\today}\n\n'); |
67 |
|
fprintf(fid,'\\frame{\\titlepage}\n\n'); |
68 |
|
|
69 |
|
fprintf(fid,'\\frame{\n'); |
70 |
|
fprintf(fid,'\\frametitle{Table of contents}\n'); |
71 |
|
fprintf(fid,'\\begin{multicols}{2}\n'); |
72 |
|
fprintf(fid,'\\Fontvi\n'); |
73 |
|
fprintf(fid,'\\tableofcontents\n'); |
74 |
|
fprintf(fid,'\\end{multicols}\n'); |
75 |
|
fprintf(fid,'} \n\n'); |
76 |
|
|
77 |
|
if ~isempty(myRdm); |
78 |
|
fprintf(fid,'\\frame{\n'); |
79 |
|
fprintf(fid,'\\section{README}\n'); |
80 |
|
fprintf(fid,'\\Fontvi\n'); |
81 |
|
for pp=1:length(myRdm); |
82 |
|
fprintf(fid,[myRdm{pp} '\n\n']); |
83 |
|
end; |
84 |
|
fprintf(fid,'} \n\n'); |
85 |
|
end; |
86 |
|
|
87 |
|
fclose(fid); |
88 |
end; |
end; |
89 |
myFigNumTex=0; |
myFigNumTex=0; |
90 |
eval(['save ' myFile(1:end-4) '.mat myFigNumTex;']); |
mySection=''; |
91 |
|
eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']); |
92 |
end; |
end; |
93 |
|
|
94 |
%open file and go to last line |
%open file and go to last line |
95 |
fid=fopen(myFile,'a+'); |
fid=fopen(myFile,'a+'); |
96 |
eval(['load ' myFile(1:end-4) '.mat myFigNumTex;']); |
eval(['load ' myFile(1:end-4) '.mat;']); |
97 |
|
|
98 |
%add title or section page (see myLev) |
%add title or section page (see myLev) |
99 |
|
if myStep==1; |
100 |
|
mySection=myText; |
101 |
|
if myLev==1; fprintf(fid,'\\section{\n'); |
102 |
|
else; fprintf(fid,'\\subsection{\n'); |
103 |
|
end; |
104 |
|
fprintf(fid,mySection); |
105 |
|
fprintf(fid,'} \n\n'); |
106 |
|
end; |
107 |
|
|
108 |
%add a figure plus caption (see myFig) |
%add a figure plus caption (see myFig) |
109 |
if myStep==2; |
if myStep==2; |
110 |
figure(myFig); |
figure(myFig); |
111 |
drawnow; |
drawnow; |
112 |
myFigNumTex=myFigNumTex+1; |
myFigNumTex=myFigNumTex+1; |
113 |
nn=strfind(myFile,'/'); |
[dirTex,fileTex,EXT] = fileparts(myFile); |
|
if ~isempty(nn); |
|
|
dirTex=myFile(1:nn(end)); fileTex=myFile(nn(end)+1:end-4); |
|
|
else; |
|
|
dirTex='./'; fileTex=myFile(1:end-4) |
|
|
end; |
|
114 |
%print the very figure |
%print the very figure |
115 |
print(myFig,'-depsc',[dirTex fileTex '.fig' num2str(myFigNumTex)]); |
if strcmp(frmt,'eps'); |
116 |
|
print(myFig,'-depsc',[dirTex fileTex '_fig' num2str(myFigNumTex) '.eps']); |
117 |
|
elseif strcmp(frmt,'jpg'); |
118 |
|
print(myFig,'-djpeg90',[dirTex fileTex '_fig' num2str(myFigNumTex) '.jpg']); |
119 |
|
elseif strcmp(frmt,'png'); |
120 |
|
print(myFig,'-dpng',[dirTex fileTex '_fig' num2str(myFigNumTex) '.png']); |
121 |
|
end |
122 |
close; |
close; |
123 |
%add figure to text file |
%add figure to text file |
124 |
fprintf(fid,'\\frame{ \n'); |
fprintf(fid,'\\frame{ \n'); |
125 |
|
fprintf(fid,['\\frametitle{' mySection '} \n']); |
126 |
fprintf(fid,'\\begin{figure}[tbh] \\centering \n'); |
fprintf(fid,'\\begin{figure}[tbh] \\centering \n'); |
127 |
% fprintf(fid,'\\includegraphics[width=\\textwidth,height=0.9\\textheight]'); |
% fprintf(fid,'\\includegraphics[width=\\textwidth,height=0.9\\textheight]'); |
128 |
fprintf(fid,'\\includegraphics[width=0.85\\textwidth]'); |
fprintf(fid,'\\includegraphics[width=0.75\\textwidth]'); |
129 |
fprintf(fid,['{' fileTex '.fig' num2str(myFigNumTex) '}\n']); |
fprintf(fid,['{' fileTex '_fig' num2str(myFigNumTex) '.' frmt '}\n']); |
130 |
fprintf(fid,'\\caption{'); |
fprintf(fid,'\\caption{'); |
131 |
for ii=1:length(myText); fprintf(fid,[myText{ii} '\n']); end; |
for ii=1:length(myText); fprintf(fid,[myText{ii} '\n']); end; |
132 |
fprintf(fid,'} \\end{figure} \n'); |
fprintf(fid,'} \\end{figure} \n'); |
146 |
%close file |
%close file |
147 |
fprintf(fid,'\n\n'); |
fprintf(fid,'\n\n'); |
148 |
fclose(fid); |
fclose(fid); |
149 |
eval(['save ' myFile(1:end-4) '.mat myFigNumTex;']); |
eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']); |
150 |
|
|
151 |
%compile |
%compile |
152 |
if myStep==5|myStep==-5; |
if myStep==5; |
153 |
dirOrig=pwd; |
dirOrig=pwd; |
154 |
nn=strfind(myFile,'/'); |
[PATHSTR,fileTex,EXT] = fileparts(myFile); |
155 |
if ~isempty(nn); |
cd(PATHSTR); |
156 |
cd(myFile(1:nn)); fileTex=myFile(nn+1:end-4); |
system(['latex ' fileTex]); |
157 |
else; |
system(['latex ' fileTex]); |
158 |
fileTex=myFile(1:end-4); |
system(['dvipdf ' fileTex]); |
|
end; |
|
|
eval(['!latex ' fileTex]); |
|
|
eval(['!latex ' fileTex]); |
|
|
eval(['!dvipdf ' fileTex]); |
|
159 |
cd(dirOrig); |
cd(dirOrig); |
160 |
end; |
end; |
161 |
|
|
162 |
|
|
163 |
%compile |
%compile |
164 |
if myStep==5; |
if myStep==6&ispc; |
165 |
|
fprintf('warning : compiling tex to pdf is bypassed on PCs\n'); |
166 |
|
end; |
167 |
|
|
168 |
|
if myStep==6&~ispc; |
169 |
dirOrig=pwd; |
dirOrig=pwd; |
170 |
nn=strfind(myFile,'/'); |
nn=strfind(myFile,filesep); |
171 |
if ~isempty(nn); |
if ~isempty(nn); |
172 |
cd(myFile(1:nn)); fileTex=myFile(nn+1:end-4); |
cd(myFile(1:nn(end))); fileTex=myFile(nn(end)+1:end-4); |
173 |
else; |
else; |
174 |
fileTex=myFile(1:end-4); |
fileTex=myFile(1:end-4); |
175 |
end; |
end; |
176 |
eval(['!\rm -f ' fileTex '.fig*']); |
delete([fileTex '_fig*']); |
177 |
eval(['!\rm -f ' fileTex '.aux']); |
delete([fileTex '.aux']); |
178 |
eval(['!\rm -f ' fileTex '.log']); |
delete([fileTex '.log']); |
179 |
eval(['!\rm -f ' fileTex '.out']); |
delete([fileTex '.out']); |
180 |
eval(['!\rm -f ' fileTex '.dvi']); |
delete([fileTex '.dvi']); |
181 |
cd(dirOrig); |
cd(dirOrig); |
182 |
end; |
end; |