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

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

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

revision 1.1 by gforget, Tue Jun 28 20:58:03 2011 UTC revision 1.14 by gforget, Mon Mar 14 15:56:49 2016 UTC
# Line 2  function []=write2tex(myFile,myStep,vara Line 2  function []=write2tex(myFile,myStep,vara
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)&myStep==2;
18  if myStep>0&myStep<4; myText=varargin{1}; end;      myText=varargin{1};
19  if myStep==1; myLev=varargin{2};      myText=[myText{:}];
20        gcmfaces_caption(myText);
21        return;
22    elseif isempty(myFile);
23        %if no file name, then do nothing
24        return;
25    end;
26    if iscell(myFile);
27        %extecute alternative command (myFile{1}) passing rest as arguments  
28        eval([myFile{1} '(myFile{2:end});']);
29        return;
30    end;
31    
32    
33    myText=[]; myLev=[]; myFig=[]; myRdm=[];
34    if myStep<4; myText=varargin{1}; end;
35    if myStep==0; myRdm=varargin{2};
36    elseif myStep==1; myLev=varargin{2};
37  elseif myStep==2; myFig=varargin{2};  elseif myStep==2; myFig=varargin{2};
38  end;  end;
39    
40    %format use for printing out plots :
41    frmt='eps';
42    if ispc; frmt='jpg'; end;
43    
44  %create file starting with write2tex.header  %create file starting with write2tex.header
45  if myStep==0;  if myStep==0;
46      test0=dir(myFile);      test0=dir(myFile);
# Line 31  if myStep==0; Line 52  if myStep==0;
52      if ~test0;      if ~test0;
53          return;          return;
54      else;      else;
55          tmp1=which('write2tex');          fid=fopen(myFile,'w');
56          eval(['!\cp -f ' tmp1(1:end-2) '.header ' myFile ]);  
57      end;          fprintf(fid,'\\documentclass[12pt]{beamer}\n');
58            fprintf(fid,'%%a nice series of examples for the beamer class:\n');
59            fprintf(fid,'%%http://www.informatik.uni-freiburg.de/~frank/ENG/beamer/example/beamer-class-example-en-5.html\n');
60            fprintf(fid,'\\usepackage{multicol}\n');
61    
62            fprintf(fid,'\n');
63            fprintf(fid,'\\newcommand\\Fontvi{\\fontsize{6}{7.2}\\selectfont}\n');
64            fprintf(fid,'\n');
65    
66            fprintf(fid,'\\begin{document} \n\n');
67    
68            fprintf(fid,'\\title{\n');
69            for ii=1:length(myText); fprintf(fid,[myText{ii} '\\\\ \n']); end;
70            fprintf(fid,'}\n\n');
71            fprintf(fid,'\\date{\\today}\n\n');
72            fprintf(fid,'\\frame{\\titlepage}\n\n');
73    
74            fprintf(fid,'\\frame{\n');
75            fprintf(fid,'\\frametitle{Table of contents}\n');
76            fprintf(fid,'\\begin{multicols}{2}\n');
77            fprintf(fid,'\\Fontvi\n');
78            fprintf(fid,'\\tableofcontents\n');
79            fprintf(fid,'\\end{multicols}\n');
80            fprintf(fid,'} \n\n');
81    
82            if ~isempty(myRdm);
83              fprintf(fid,'\\frame{\n');
84              fprintf(fid,'\\section{README}\n');
85              fprintf(fid,'\\Fontvi\n');
86              for pp=1:length(myRdm);
87                fprintf(fid,[myRdm{pp} '\n\n']);
88              end;
89              fprintf(fid,'} \n\n');
90            end;
91    
92            fclose(fid);
93        end;
94        myFigNumTex=0;
95        mySection='';
96        eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']);
97  end;  end;
98    
99  %open file and go to last line  %open file and go to last line
100  fid=fopen(myFile,'a+');  fid=fopen(myFile,'a+');
101    eval(['load ' myFile(1:end-4) '.mat;']);
102    
103  %add title or section page (see myLev)  %add title or section page (see myLev)
104    if myStep==1;
105        mySection=myText;
106        if myLev==1; fprintf(fid,'\\section{\n');
107        else; fprintf(fid,'\\subsection{\n');
108        end;
109        fprintf(fid,mySection);
110        fprintf(fid,'} \n\n');
111    end;
112    
113  %add a figure plus caption (see myFig)  %add a figure plus caption (see myFig)
114  if myStep==2;  if myStep==2;
115      figure(myFig);      figure(myFig);
116      nn=strfind(myFile,'/');      drawnow;
117      if ~isempty(nn);      %add (but hide) caption directly in figure
118          dirTex=myFile(1:nn); fileTex=myFile(nn+1:end-4);      captionHandle=gcmfaces_caption([myText{:}]);
119      else;      set(captionHandle,'Visible','off');
120          dirTex='./'; fileTex=myFile(1:end-4)      set(get(captionHandle,'Children'),'Visible','off');
121      end;      %set file names
122      %print the very figure      myFigNumTex=myFigNumTex+1;
123      print(myFig,'-depsc',[dirTex fileTex '.fig' num2str(myFig)]);      [dirTex,fileTex,EXT] = fileparts(myFile);
124        %print figure
125        if strcmp(frmt,'eps');
126          print(myFig,'-depsc',[dirTex fileTex '_fig' num2str(myFigNumTex) '.eps']);
127        elseif strcmp(frmt,'jpg');
128          print(myFig,'-djpeg90',[dirTex fileTex '_fig' num2str(myFigNumTex) '.jpg']);
129        elseif strcmp(frmt,'png');
130          print(myFig,'-dpng',[dirTex fileTex '_fig' num2str(myFigNumTex) '.png']);
131        end
132        %save figure (with visible caption)
133        set(captionHandle,'Visible','on');
134        set(get(captionHandle,'Children'),'Visible','on');
135        saveas(myFig,[dirTex fileTex '_fig' num2str(myFigNumTex)],'fig');
136        %close figure
137        close;
138      %add figure to text file      %add figure to text file
139      fprintf(fid,'\\clearpage \n');      fprintf(fid,'\\frame{ \n');
140        fprintf(fid,['\\frametitle{' mySection '} \n']);
141      fprintf(fid,'\\begin{figure}[tbh] \\centering \n');      fprintf(fid,'\\begin{figure}[tbh] \\centering \n');
142  %     fprintf(fid,'\\includegraphics[width=\\textwidth,height=0.9\\textheight]');  %     fprintf(fid,'\\includegraphics[width=\\textwidth,height=0.9\\textheight]');
143      fprintf(fid,'\\includegraphics[width=\\textwidth]');      fprintf(fid,'\\includegraphics[width=0.75\\textwidth]');
144      fprintf(fid,['{' fileTex '.fig' num2str(myFig) '}\n']);      fprintf(fid,['{' fileTex '_fig' num2str(myFigNumTex) '.' frmt '}\n']);
145      fprintf(fid,'\\caption{');      fprintf(fid,'\\caption{');
146      for ii=1:length(myText); fprintf(fid,[myText{ii} '\n']); end;      for ii=1:length(myText); fprintf(fid,[myText{ii} '\n']); end;
147      fprintf(fid,'} \\end{figure} \n');      fprintf(fid,'} \\end{figure} \n');
148      fprintf(fid,'\\clearpage \n\n');      fprintf(fid,'} \n\n');
149  end;  end;
150    
151  %add a paragraph  %add a paragraph
# Line 77  if myStep==4; fprintf(fid,'\n\n \\end{do Line 161  if myStep==4; fprintf(fid,'\n\n \\end{do
161  %close file  %close file
162  fprintf(fid,'\n\n');  fprintf(fid,'\n\n');
163  fclose(fid);  fclose(fid);
164    eval(['save ' myFile(1:end-4) '.mat myFigNumTex mySection;']);
165    
166  %compile  %compile
167  if myStep==5|myStep==-5;  if myStep==5;
168      dirOrig=pwd;      fprintf('\nNow we can attempt to compile the tex file from within Matlab. \n');
169      nn=strfind(myFile,'/');      fprintf('If the latex implementation is incomplete then this may fail, and \n');
170      if ~isempty(nn);      fprintf('user should then abort and compile the tex file from outside Matlab. \n');
171          cd(myFile(1:nn)); fileTex=myFile(nn+1:end-4);      fprintf('The beamer class is required by in particular. In Matlab, if prompted \n');
172      else;      fprintf(' with a question mark then typing ''quit'' will abort compilation. \n');
173          fileTex=myFile(1:end-4);      test0=input('\n type 1 to proceed or 0 to skip this attempt\n');
174        if test0;
175          dirOrig=pwd;
176          [PATHSTR,fileTex,EXT] = fileparts(myFile);
177          cd(PATHSTR);
178          system(['latex ' fileTex]);
179          system(['latex ' fileTex]);
180          system(['dvipdf ' fileTex]);
181          cd(dirOrig);
182      end;      end;
     eval(['!latex ' fileTex]);  
     eval(['!latex ' fileTex]);  
     eval(['!dvipdf ' fileTex]);  
     cd(dirOrig);  
183  end;  end;
184    
185    
186  %compile  %compile
187  if myStep==5;  if myStep==6&ispc;
188        fprintf('warning : compiling tex to pdf is bypassed on PCs\n');
189    end;
190    
191    if myStep==6&~ispc;
192      dirOrig=pwd;      dirOrig=pwd;
193      nn=strfind(myFile,'/');      nn=strfind(myFile,filesep);
194      if ~isempty(nn);      if ~isempty(nn);
195          cd(myFile(1:nn)); fileTex=myFile(nn+1:end-4);          cd(myFile(1:nn(end))); fileTex=myFile(nn(end)+1:end-4);
196      else;      else;
197          fileTex=myFile(1:end-4);          fileTex=myFile(1:end-4);
198      end;      end;
199      eval(['!\rm -f ' fileTex '.fig*']);      delete([fileTex '_fig*']);
200      eval(['!\rm -f ' fileTex '.aux']);      delete([fileTex '.aux']);
201      eval(['!\rm -f ' fileTex '.log']);      delete([fileTex '.log']);
202      eval(['!\rm -f ' fileTex '.out']);      delete([fileTex '.out']);
203      eval(['!\rm -f ' fileTex '.dvi']);      delete([fileTex '.dvi']);
204      cd(dirOrig);      cd(dirOrig);
205  end;  end;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.22