/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_misc/write2tex.m
ViewVC logotype

Contents of /MITgcm_contrib/gael/matlab_class/gcmfaces_misc/write2tex.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download)
Thu Jul 11 22:08:27 2013 UTC (12 years ago) by gforget
Branch: MAIN
Changes since 1.3: +11 -0 lines
added options :
- if no file name, then do nothing
- if cell type file name, then alternative command

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 % 0 create file starting with title page (see myText)
6 % 1 add section or subsection (see myLev)
7 % 2 add a figure plus caption (see myFig)
8 % 3 add a paragraph
9 % 4 finish file
10 % 5 compile and remove temporary files (incl. *fig*.ps)
11 % -5 compile solely
12 %optional myText is a cell array of text lines (for myStep=1 to 2)
13 % myLev is the title level (for myStep=1)
14 % 1=section, 2=subsection (not yet implemented)
15 % myFig is a figure handle (for myStep=2)
16
17 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 myText=[]; myLev=[]; myFig=[];
29 if myStep<4; myText=varargin{1}; end;
30 if myStep==1; myLev=varargin{2};
31 elseif myStep==2; myFig=varargin{2};
32 end;
33
34 %create file starting with write2tex.header
35 if myStep==0;
36 test0=dir(myFile);
37 if ~isempty(test0);
38 test0=input(['you are about to overwrite ' myFile ' !!! \n type 1 to proceed, 0 to stop \n']);
39 else;
40 test0=1;
41 end;
42 if ~test0;
43 return;
44 else;
45 fid=fopen(myFile,'w');
46
47 fprintf(fid,'\\documentclass[12pt]{beamer}\n');
48 fprintf(fid,'%%a nice series of examples for the beamer class:\n');
49 fprintf(fid,'%%http://www.informatik.uni-freiburg.de/~frank/ENG/beamer/example/beamer-class-example-en-5.html\n');
50 fprintf(fid,'\\begin{document} \n\n');
51
52 fprintf(fid,'\\title{\n');
53 for ii=1:length(myText); fprintf(fid,[myText{ii} '\\\\ \n']); end;
54 fprintf(fid,'}\n\n');
55 fprintf(fid,'\\date{\\today}\n\n');
56 fprintf(fid,'\\frame{\\titlepage}\n\n');
57
58 fprintf(fid,'\\frame{');
59 fprintf(fid,'\\frametitle{Table of contents}');
60 fprintf(fid,'\\tableofcontents');
61 fprintf(fid,'} \n\n');
62
63 fclose(fid);
64 end;
65 myFigNumTex=0;
66 mySection='';
67 eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']);
68 end;
69
70 %open file and go to last line
71 fid=fopen(myFile,'a+');
72 eval(['load ' myFile(1:end-4) '.mat;']);
73
74 %add title or section page (see myLev)
75 if myStep==1;
76 mySection=myText;
77 fprintf(fid,'\\section{\n');
78 fprintf(fid,mySection);
79 fprintf(fid,'} \n\n');
80 end;
81
82 %add a figure plus caption (see myFig)
83 if myStep==2;
84 figure(myFig);
85 drawnow;
86 myFigNumTex=myFigNumTex+1;
87 nn=strfind(myFile,'/');
88 if ~isempty(nn);
89 dirTex=myFile(1:nn(end)); fileTex=myFile(nn(end)+1:end-4);
90 else;
91 dirTex='./'; fileTex=myFile(1:end-4)
92 end;
93 %print the very figure
94 print(myFig,'-depsc',[dirTex fileTex '.fig' num2str(myFigNumTex)]);
95 close;
96 %add figure to text file
97 fprintf(fid,'\\frame{ \n');
98 fprintf(fid,['\\frametitle{' mySection '} \n']);
99 fprintf(fid,'\\begin{figure}[tbh] \\centering \n');
100 % fprintf(fid,'\\includegraphics[width=\\textwidth,height=0.9\\textheight]');
101 fprintf(fid,'\\includegraphics[width=0.75\\textwidth]');
102 fprintf(fid,['{' fileTex '.fig' num2str(myFigNumTex) '}\n']);
103 fprintf(fid,'\\caption{');
104 for ii=1:length(myText); fprintf(fid,[myText{ii} '\n']); end;
105 fprintf(fid,'} \\end{figure} \n');
106 fprintf(fid,'} \n\n');
107 end;
108
109 %add a paragraph
110 if myStep==3;
111 for ii=1:length(myText);
112 fprintf(fid,[myText{ii} '\n']);
113 end;
114 end;
115
116 %finish file
117 if myStep==4; fprintf(fid,'\n\n \\end{document} \n\n'); end;
118
119 %close file
120 fprintf(fid,'\n\n');
121 fclose(fid);
122 eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']);
123
124 %compile
125 if myStep==5|myStep==-5;
126 dirOrig=pwd;
127 nn=strfind(myFile,'/');
128 if ~isempty(nn);
129 cd(myFile(1:nn)); fileTex=myFile(nn+1:end-4);
130 else;
131 fileTex=myFile(1:end-4);
132 end;
133 eval(['!latex ' fileTex]);
134 eval(['!latex ' fileTex]);
135 eval(['!dvipdf ' fileTex]);
136 cd(dirOrig);
137 end;
138
139
140 %compile
141 if myStep==5;
142 dirOrig=pwd;
143 nn=strfind(myFile,'/');
144 if ~isempty(nn);
145 cd(myFile(1:nn)); fileTex=myFile(nn+1:end-4);
146 else;
147 fileTex=myFile(1:end-4);
148 end;
149 eval(['!\rm -f ' fileTex '.fig*']);
150 eval(['!\rm -f ' fileTex '.aux']);
151 eval(['!\rm -f ' fileTex '.log']);
152 eval(['!\rm -f ' fileTex '.out']);
153 eval(['!\rm -f ' fileTex '.dvi']);
154 cd(dirOrig);
155 end;

  ViewVC Help
Powered by ViewVC 1.1.22