Debian Codegraph @

texmf-dist/doc/latex/psfragx/psfragx.m

raw
1 %% psfragx.m [2012/05/02 v0.4 Pascal Kockaert]
2 %%
3 %% function psfragx(TeXname,EPSname,Outname)
4 %% nargin=1 -> EPSname=TeXname
5 %% nargin=2 -> Outname=EPSname
6 %%
7 %% Interleaves lines of TeXname.tex and EPSname.eps
8 %% so that all the lines of EPSname.eps are copied in Outname.tex and
9 %% lines from TeXname.tex starting with
10 %% \psfrag
11 %% and
12 %% %<pfx>
13 %% are written in Outname.eps, as a comment following the
14 %% %%BoundigBox
15 %% line.
16
17 %% This work may be distributed and/or modified under the
18 %% conditions of the LaTeX Project Public License, either version 1.3
19 %% of this license or (at your option) any later version.
20 %% The latest version of this license is in
21 %% http://www.latex-project.org/lppl.txt
22 %% and version 1.3 or later is part of all distributions of LaTeX
23 %% version 2005/12/01 or later.
24 %%
25 %% This file is currently maintained by Pascal Kockaert
26
27 function psfragx(TeXname,EPSname,Outname)
28 TMPname='psfragx_tmp';
29 if nargin<2, EPSname=TeXname; end
30 if nargin<3, Outname=EPSname; end
31 if Outname==EPSname,
32 eval(['!rm ',TMPname,'.eps'])
33 eval(['!mv ',EPSname,'.eps ',TMPname,'.eps'])
34 EPSname=TMPname;
35 end
36 TeXName=([TeXname,'.tex']);
37 EPSName=([EPSname,'.eps']);
38 OutName=([Outname,'.eps']);
39
40 %% Uncomment for debugging purposes
41 %% eval(['ls ',TeXName])
42 %% eval(['ls ',EPSName])
43 %% eval(['ls ',OutName])
44
45 BeginInput ='%%BoundingBox:';
46 BeginPSFRAG='%<pfx>\pfxbegin[1.0]{laprint}%';
47 EndPSFRAG ='%<pfx>\pfxend';
48 StartPFX ='%<*pfx> Inserted where \begin{psfrags}% occured';
49 StopPFX ='%</pfx> Inserted where \end{psfrags}% occured';
50 EndInput ='%\endinput';
51 EndOfFile ='%%EOF';
52 ResizeBox ='%<pfx>\def\naturalwidth';
53 StopOn ={'\psfrag{','<pfx>','\begin{psfrags}','\end{psfrags}','\resizebox'};
54
55 TeXFile=fopen(TeXName,'r');
56 if (TeXFile==-1)
57 error(['I was not able to open ',TeXName,'!']);
58 end
59 EPSFile=fopen(EPSName,'r');
60 if (EPSFile==-1)
61 error(['I was not able to open ',EPSName,'!']);
62 end
63 OutFile=fopen(OutName,'w');
64 if (OutFile==-1)
65 error(['I was not able to open ',OutName,'!']);
66 end
67
68 [sEPS,llEPS,iEPS]=CopyUntil(EPSFile,OutFile,{BeginInput});
69 if sEPS~=1, error(['No line contains ',BeginInput]);
70 else
71 fprintf(OutFile,'%s\n',llEPS);
72 end
73
74 %%%
75 %%% Write preamble
76 %%%
77 fprintf(OutFile,'%%<*pfx> Begin Preamble\n');
78 fprintf(OutFile,'%%\\providecommand*{\\pfxbegin}[2][]{}%%\n');
79 fprintf(OutFile,'%%\\providecommand{\\pfxend}{}%%\n');
80 fprintf(OutFile,'%%</pfx> End Preamble\n');
81 %%%
82 %%% Copy interesting lines
83 %%%
84 while 1
85 [sTeX,llTeX,iTeX]=ReadUntil(TeXFile,StopOn);
86 if sTeX~=1, break; end
87 switch iTeX
88 case 1, % \psfrag
89 fprintf(OutFile,'%%%s\n',llTeX);
90 case 2, % %<pfx>
91 fprintf(OutFile,'%s\n',llTeX);
92 case 3, % \begin{psfrags}
93 fprintf(OutFile,'%s\n',BeginPSFRAG);
94 fprintf(OutFile,'%s\n',StartPFX);
95 case 4, % \end{psfrags}
96 fprintf(OutFile,'%s\n',StopPFX);
97 fprintf(OutFile,'%s\n',EndPSFRAG);
98 case 5, % \resizebox
99 tmpbeg=findstr(llTeX,'{');
100 tmpend=findstr(llTeX,'}');
101 if (length(tmpbeg)>0)&(length(tmpend)>0)
102 if (tmpbeg(1)<tmpend(1))
103 fprintf(OutFile,'%s%s%%\n',ResizeBox,llTeX(tmpbeg(1):tmpend(1)));
104 end
105 end
106 otherwise
107 error('Otherwise should never happen !')
108 end
109 end
110 %%%
111 %%% Write postamble
112 %%%
113 fprintf(OutFile,'%s\n',EndInput);
114 %%%
115 %%% Copy to the end of file
116 %%%
117 [sEPS,llEPS,iEPS]=CopyUntil(EPSFile,OutFile,{''});
118 %%%
119 %%% Close files
120 %%%
121 fclose(OutFile);
122 fclose(TeXFile);
123 fclose(EPSFile);
124 return
125
126 function [OK,lastline,elt]=CopyUntil(fidIn,fidOut,linebeg);
127 sl=length(linebeg);
128 if sl==0, OK=-2; return, end
129 llb=zeros(sl);
130 for ii=1:sl
131 llb(ii)=length(linebeg{ii});
132 end
133 lastline='';
134 OK=0;
135 elt=0;
136 while 1
137 Line=fgetl(fidIn);
138 if ~isstr(Line),
139 OK=-1;
140 return,
141 end %EndOfFile
142 for ii=1:sl
143 %%% fprintf('Seeking for line starting with %s.\n',linebeg{ii});
144 if llb==0, %%% Copying to the end of file
145 else
146 if length(Line)>=llb(ii)
147 %%% fprintf('This line counts more than %i chars.\n',llb(ii));
148 if Line(1:llb(ii))==linebeg{ii},
149 OK=1;
150 elt=ii;
151 lastline=Line;
152 break
153 end
154 end
155 end
156 end %%% No matching string
157 if OK==1, break, end
158 if ~isempty(fidOut)
159 fprintf(fidOut,'%s\n',Line);
160 end
161 end
162 return
163
164 function [OK,lastline,elt]=ReadUntil(fidIn,linebeg);
165 [OK,lastline,elt]=CopyUntil(fidIn,[],linebeg);
166 return