summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/psfragx/psfragx.m
blob: 5c051b953b5818700acf53755618457da8c47bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
%% psfragx.m [2012/05/02 v0.4 Pascal Kockaert]
%%
%% function psfragx(TeXname,EPSname,Outname)
%% nargin=1 -> EPSname=TeXname
%% nargin=2 -> Outname=EPSname
%%
%% Interleaves lines of TeXname.tex and EPSname.eps
%% so that all the lines of EPSname.eps are copied in Outname.tex and
%% lines from TeXname.tex starting with
%% \psfrag
%% and
%% %<pfx>
%% are written in Outname.eps, as a comment following the
%% %%BoundigBox
%% line.

%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This file is currently maintained by Pascal Kockaert

function psfragx(TeXname,EPSname,Outname)
TMPname='psfragx_tmp';
if nargin<2, EPSname=TeXname; end
if nargin<3, Outname=EPSname; end
if Outname==EPSname,
   eval(['!rm ',TMPname,'.eps'])
   eval(['!mv ',EPSname,'.eps ',TMPname,'.eps'])
   EPSname=TMPname;
end
TeXName=([TeXname,'.tex']);
EPSName=([EPSname,'.eps']);
OutName=([Outname,'.eps']);

%% Uncomment for debugging purposes
%% eval(['ls ',TeXName])
%% eval(['ls ',EPSName])
%% eval(['ls ',OutName])

BeginInput ='%%BoundingBox:';
BeginPSFRAG='%<pfx>\pfxbegin[1.0]{laprint}%';
EndPSFRAG  ='%<pfx>\pfxend';
StartPFX   ='%<*pfx> Inserted where \begin{psfrags}% occured';
StopPFX    ='%</pfx> Inserted where \end{psfrags}% occured';
EndInput  ='%\endinput';
EndOfFile ='%%EOF';
ResizeBox ='%<pfx>\def\naturalwidth';
StopOn    ={'\psfrag{','<pfx>','\begin{psfrags}','\end{psfrags}','\resizebox'};

TeXFile=fopen(TeXName,'r');
if (TeXFile==-1)
        error(['I was not able to open ',TeXName,'!']);
end
EPSFile=fopen(EPSName,'r');
if (EPSFile==-1)
        error(['I was not able to open ',EPSName,'!']);
end
OutFile=fopen(OutName,'w');
if (OutFile==-1)
        error(['I was not able to open ',OutName,'!']);
end

[sEPS,llEPS,iEPS]=CopyUntil(EPSFile,OutFile,{BeginInput});
       if sEPS~=1, error(['No line contains ',BeginInput]);
       else
                 fprintf(OutFile,'%s\n',llEPS);
       end

%%%
%%% Write preamble
%%%
fprintf(OutFile,'%%<*pfx> Begin Preamble\n');
fprintf(OutFile,'%%\\providecommand*{\\pfxbegin}[2][]{}%%\n');
fprintf(OutFile,'%%\\providecommand{\\pfxend}{}%%\n');
fprintf(OutFile,'%%</pfx> End Preamble\n');
%%%
%%% Copy interesting lines
%%%
while 1
       [sTeX,llTeX,iTeX]=ReadUntil(TeXFile,StopOn);
              if sTeX~=1, break; end
       switch iTeX
          case 1,    %   \psfrag
                 fprintf(OutFile,'%%%s\n',llTeX);
          case 2,    %   %<pfx>
                 fprintf(OutFile,'%s\n',llTeX);
          case 3,    %   \begin{psfrags}
                 fprintf(OutFile,'%s\n',BeginPSFRAG);
                 fprintf(OutFile,'%s\n',StartPFX);
          case 4,    %   \end{psfrags}
                 fprintf(OutFile,'%s\n',StopPFX);
                 fprintf(OutFile,'%s\n',EndPSFRAG);
          case 5,    %   \resizebox
                 tmpbeg=findstr(llTeX,'{');
                 tmpend=findstr(llTeX,'}');
                 if (length(tmpbeg)>0)&(length(tmpend)>0)
                     if (tmpbeg(1)<tmpend(1))
                        fprintf(OutFile,'%s%s%%\n',ResizeBox,llTeX(tmpbeg(1):tmpend(1)));
                     end
                 end
       otherwise
              error('Otherwise should never happen !')
       end
end
%%%
%%% Write postamble
%%%
fprintf(OutFile,'%s\n',EndInput);
%%%
%%% Copy to the end of file
%%%
[sEPS,llEPS,iEPS]=CopyUntil(EPSFile,OutFile,{''});
%%%
%%% Close files
%%%
fclose(OutFile);
fclose(TeXFile);
fclose(EPSFile);
return

function [OK,lastline,elt]=CopyUntil(fidIn,fidOut,linebeg);
sl=length(linebeg);
if sl==0, OK=-2; return, end
llb=zeros(sl);
for ii=1:sl
       llb(ii)=length(linebeg{ii});
end
lastline='';
OK=0;
elt=0;
while 1
       Line=fgetl(fidIn);
       if ~isstr(Line),
              OK=-1;
              return,
       end  %EndOfFile
       for ii=1:sl
%%%              fprintf('Seeking for line starting with %s.\n',linebeg{ii});
              if llb==0,    %%% Copying to the end of file
              else
                     if length(Line)>=llb(ii)
%%%                            fprintf('This line counts more than %i chars.\n',llb(ii));
                            if Line(1:llb(ii))==linebeg{ii},
                                   OK=1;
                                   elt=ii;
                                   lastline=Line;
                                   break
                            end
                     end
              end
       end    %%% No matching string
       if OK==1, break, end
       if ~isempty(fidOut)
              fprintf(fidOut,'%s\n',Line);
       end
end
return

function [OK,lastline,elt]=ReadUntil(fidIn,linebeg);
         [OK,lastline,elt]=CopyUntil(fidIn,[],linebeg);
return