1 |
gforget |
1.1 |
function []=write2tex(myFile,myStep,varargin); |
2 |
|
|
%object: create/increment/complete/compile a tex file from within matlab |
3 |
|
|
%input: myFile is the file name |
4 |
|
|
% myStep is the operation to perform on the tex file |
5 |
gforget |
1.3 |
% 0 create file starting with title page (see myText) |
6 |
|
|
% 1 add section or subsection (see myLev) |
7 |
gforget |
1.1 |
% 2 add a figure plus caption (see myFig) |
8 |
|
|
% 3 add a paragraph |
9 |
|
|
% 4 finish file |
10 |
gforget |
1.6 |
% 5 compile (latex x 2 then dvipdf) |
11 |
|
|
% 6 remove temporary files (incl. *fig*.ps) |
12 |
gforget |
1.1 |
%optional myText is a cell array of text lines (for myStep=1 to 2) |
13 |
|
|
% myLev is the title level (for myStep=1) |
14 |
gforget |
1.3 |
% 1=section, 2=subsection (not yet implemented) |
15 |
gforget |
1.1 |
% myFig is a figure handle (for myStep=2) |
16 |
|
|
|
17 |
gforget |
1.4 |
if isempty(myFile); |
18 |
|
|
%if no file name, then do nothing |
19 |
|
|
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 |
gforget |
1.8 |
myText=[]; myLev=[]; myFig=[]; myRdm=[]; |
29 |
gforget |
1.3 |
if myStep<4; myText=varargin{1}; end; |
30 |
gforget |
1.8 |
if myStep==0; myRdm=varargin{2}; |
31 |
|
|
elseif myStep==1; myLev=varargin{2}; |
32 |
gforget |
1.1 |
elseif myStep==2; myFig=varargin{2}; |
33 |
|
|
end; |
34 |
|
|
|
35 |
gforget |
1.10 |
%format use for printing out plots : |
36 |
|
|
frmt='eps'; |
37 |
|
|
if ispc; frmt='jpg'; end; |
38 |
|
|
|
39 |
gforget |
1.1 |
%create file starting with write2tex.header |
40 |
|
|
if myStep==0; |
41 |
|
|
test0=dir(myFile); |
42 |
|
|
if ~isempty(test0); |
43 |
|
|
test0=input(['you are about to overwrite ' myFile ' !!! \n type 1 to proceed, 0 to stop \n']); |
44 |
|
|
else; |
45 |
|
|
test0=1; |
46 |
|
|
end; |
47 |
|
|
if ~test0; |
48 |
|
|
return; |
49 |
|
|
else; |
50 |
gforget |
1.3 |
fid=fopen(myFile,'w'); |
51 |
|
|
|
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 |
gforget |
1.8 |
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 |
gforget |
1.3 |
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 |
gforget |
1.8 |
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 |
gforget |
1.3 |
fprintf(fid,'} \n\n'); |
76 |
|
|
|
77 |
gforget |
1.8 |
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 |
gforget |
1.3 |
fclose(fid); |
88 |
gforget |
1.1 |
end; |
89 |
gforget |
1.2 |
myFigNumTex=0; |
90 |
gforget |
1.3 |
mySection=''; |
91 |
|
|
eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']); |
92 |
gforget |
1.1 |
end; |
93 |
|
|
|
94 |
|
|
%open file and go to last line |
95 |
|
|
fid=fopen(myFile,'a+'); |
96 |
gforget |
1.3 |
eval(['load ' myFile(1:end-4) '.mat;']); |
97 |
gforget |
1.1 |
|
98 |
|
|
%add title or section page (see myLev) |
99 |
gforget |
1.3 |
if myStep==1; |
100 |
|
|
mySection=myText; |
101 |
gforget |
1.5 |
if myLev==1; fprintf(fid,'\\section{\n'); |
102 |
|
|
else; fprintf(fid,'\\subsection{\n'); |
103 |
|
|
end; |
104 |
gforget |
1.3 |
fprintf(fid,mySection); |
105 |
|
|
fprintf(fid,'} \n\n'); |
106 |
|
|
end; |
107 |
gforget |
1.1 |
|
108 |
|
|
%add a figure plus caption (see myFig) |
109 |
|
|
if myStep==2; |
110 |
|
|
figure(myFig); |
111 |
gforget |
1.2 |
drawnow; |
112 |
|
|
myFigNumTex=myFigNumTex+1; |
113 |
gforget |
1.12 |
[dirTex,fileTex,EXT] = fileparts(myFile); |
114 |
gforget |
1.1 |
%print the very figure |
115 |
gforget |
1.10 |
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 |
gforget |
1.11 |
elseif strcmp(frmt,'png'); |
120 |
gforget |
1.10 |
print(myFig,'-dpng',[dirTex fileTex '_fig' num2str(myFigNumTex) '.png']); |
121 |
|
|
end |
122 |
gforget |
1.2 |
close; |
123 |
gforget |
1.1 |
%add figure to text file |
124 |
gforget |
1.2 |
fprintf(fid,'\\frame{ \n'); |
125 |
gforget |
1.3 |
fprintf(fid,['\\frametitle{' mySection '} \n']); |
126 |
gforget |
1.1 |
fprintf(fid,'\\begin{figure}[tbh] \\centering \n'); |
127 |
|
|
% fprintf(fid,'\\includegraphics[width=\\textwidth,height=0.9\\textheight]'); |
128 |
gforget |
1.3 |
fprintf(fid,'\\includegraphics[width=0.75\\textwidth]'); |
129 |
gforget |
1.11 |
fprintf(fid,['{' fileTex '_fig' num2str(myFigNumTex) '.' frmt '}\n']); |
130 |
gforget |
1.1 |
fprintf(fid,'\\caption{'); |
131 |
|
|
for ii=1:length(myText); fprintf(fid,[myText{ii} '\n']); end; |
132 |
|
|
fprintf(fid,'} \\end{figure} \n'); |
133 |
gforget |
1.2 |
fprintf(fid,'} \n\n'); |
134 |
gforget |
1.1 |
end; |
135 |
|
|
|
136 |
|
|
%add a paragraph |
137 |
|
|
if myStep==3; |
138 |
|
|
for ii=1:length(myText); |
139 |
|
|
fprintf(fid,[myText{ii} '\n']); |
140 |
|
|
end; |
141 |
|
|
end; |
142 |
|
|
|
143 |
|
|
%finish file |
144 |
|
|
if myStep==4; fprintf(fid,'\n\n \\end{document} \n\n'); end; |
145 |
|
|
|
146 |
|
|
%close file |
147 |
|
|
fprintf(fid,'\n\n'); |
148 |
|
|
fclose(fid); |
149 |
gforget |
1.3 |
eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']); |
150 |
gforget |
1.1 |
|
151 |
|
|
%compile |
152 |
gforget |
1.6 |
if myStep==5; |
153 |
gforget |
1.1 |
dirOrig=pwd; |
154 |
gforget |
1.12 |
[PATHSTR,fileTex,EXT] = fileparts(myFile); |
155 |
gforget |
1.10 |
cd(PATHSTR); |
156 |
gforget |
1.9 |
system(['latex ' fileTex]); |
157 |
|
|
system(['latex ' fileTex]); |
158 |
|
|
system(['dvipdf ' fileTex]); |
159 |
gforget |
1.1 |
cd(dirOrig); |
160 |
|
|
end; |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
%compile |
164 |
gforget |
1.9 |
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 |
gforget |
1.1 |
dirOrig=pwd; |
170 |
gforget |
1.9 |
nn=strfind(myFile,filesep); |
171 |
gforget |
1.1 |
if ~isempty(nn); |
172 |
gforget |
1.6 |
cd(myFile(1:nn(end))); fileTex=myFile(nn(end)+1:end-4); |
173 |
gforget |
1.1 |
else; |
174 |
|
|
fileTex=myFile(1:end-4); |
175 |
|
|
end; |
176 |
gforget |
1.11 |
delete([fileTex '_fig*']); |
177 |
gforget |
1.9 |
delete([fileTex '.aux']); |
178 |
|
|
delete([fileTex '.log']); |
179 |
|
|
delete([fileTex '.out']); |
180 |
|
|
delete([fileTex '.dvi']); |
181 |
gforget |
1.1 |
cd(dirOrig); |
182 |
|
|
end; |