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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
\ProvidesPackageRCS[v\pgfversion] $Header: /cvsroot/pgf/pgf/generic/pgf/basiclayer/pgfbaseplot.code.tex,v 1.4 2006/10/11 15:22:25 tantau Exp $
% PGF's plotting interface works as follows:
%
% In order to plot something, two things need to be done. First, you
% need to provide the coordinates (obviously) of the points that
% should be plotted. The coordinates are given via a long stream of
% commands. These commands are \pgfplotstreamstart, which is
% given exactly once at the beginning, \pgfplotstreampoint, of which there
% are numerous in the middle, \pgfplotstreamspecial, of which there may be
% numerous in the middle, and \pgfplotstreamend, which must be given
% at the end. Between these commands arbitrary other commands may be
% given. Here is an example:
%
% ...
% \pgfplotstreamstart
% \pgfplotstreampoint{\pgfpointxy{0}{0}}
% \pgfplotstreampoint{\pgfpointxy{1}{1}}
% \pgfplotstreampoint{\pgfpointxy{2}{4}}
% \relax
% \pgfplotstreampoint{\pgfpointxy{3}{9}}
% \pgfplotstreamspecial{some handler-dependent special stuff}
% \pgfplotstreamend
%
% By themselves, the \pgfplotstreamxxxx commands do not do anything by
% default. Rather, the ``use'' such a stream, you must first install a
% stream handler. For example, the ``lineto'' handler will simply
% translate every \pgfplotstreampoint into a \pgfpathlineto.
%
% Example:
%
% \pgfpathmoveto{\pgfpointorigin}
%
% \pgfplothandlerlineto
% \pgfplotstreamstart
% \pgfplotstreampoint{\pgfpointxy{0}{0}}
% \pgfplotstreampoint{\pgfpointxy{1}{1}}
% \pgfplotstreampoint{\pgfpointxy{2}{4}}
% \relax
% \pgfplotstreampoint{\pgfpointxy{3}{9}}
% \pgfplotstreamend
% The stream commands actually call their ``internal'' versions, which
% are set by the handlers:
\def\pgfplotstreamstart{\pgf@plotstreamstart}
\def\pgfplotstreampoint#1{\gdef\pgfplotlastpoint{#1}\pgf@plotstreampoint{#1}}
\def\pgfplotstreamspecial{\pgf@plotstreamspecial}
\def\pgfplotstreamend{\pgf@plotstreamend}
% Sets the action taken for the first point of a plot to a lineto.
%
% Description:
%
% For certain handlers it makes sense either the start a plot by
% moving to the first point of the plot or to do a lineto to that
% first point. Using this command this action can be set to a lineto.
%
% Example:
%
% \pgfsetlinetofirstplotpoint
\def\pgfsetlinetofirstplotpoint{\let\pgf@plot@first@action=\pgfpathlineto}
% Sets the action taken for the first point of a plot to a moveto.
%
% Example:
%
% \pgfsetmovetofirstplotpoint
\def\pgfsetmovetofirstplotpoint{\let\pgf@plot@first@action=\pgfpathmoveto}
\let\pgf@plot@first@action=\pgfpathmoveto
%
% Handlers
%
% This handler converts each plot stream command into a lineto
% command, except for the first, which is converted to the action that
% has previously been specified using \pgfsetlinetofirstplotpoint or
% \pgfsetmovetofirstplotpoint.
%
% Example:
%
% \pgfplothandlerlineto
% \pgfplotxyfile{mytable}
\def\pgfplothandlerlineto{%
\def\pgf@plotstreamstart{%
\global\let\pgf@plotstreampoint=\pgf@plot@line@handler%
\global\let\pgf@plotstreamspecial=\pgf@gobble%
\global\let\pgf@plotstreamend=\relax%
}%
}
\def\pgf@plot@line@handler#1{%
\pgf@plot@first@action{#1}%
\global\let\pgf@plotstreampoint=\pgfpathlineto%
}
% More handlers are defined in pgflibraryplothandlers
% This handler discards the plot.
%
% Example:
%
% \pgfplothandlerdiscard
% \pgfplotxyfile{mytable}
\def\pgfplothandlerdiscard{%
\def\pgf@plotstreamstart{%
\global\let\pgf@plotstreampoint=\pgf@gobble%
\global\let\pgf@plotstreamspecial=\pgf@gobble%
\global\let\pgf@plotstreamend=\relax%
}%
}
% This handler records each plot stream command to a macro. This is
% useful if plot commands are difficult to generate and need to be
% ``recylced'' later on.
%
% Example:
%
% \pgfplothandlerrecord{\myplot}
% \pgfplotxyfile{mytable} % stored in \myplot now
% \pgfplothandlerline
% \myplot
% \pgftransformxshift{1cm}
% \myplot
\def\pgfplothandlerrecord#1{%
\def\pgf@plot@recordto{#1}%
\gdef#1{\pgfplotstreamstart}%
\def\pgf@plotstreamstart{%
\gdef\pgf@plotstreampoint####1{\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreampoint{####1}}}%
\gdef\pgf@plotstreamspecial####1{\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreamspecial{####1}}}%
\gdef\pgf@plotstreamend{\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreamend}}%
}%
}
% Read a plot stream from a file and plot it.
%
% #1 = file from which to read things
%
% File format:
%
% Each line of the file should begin with two numbers separated by a
% space. Such a line with number #1 and #2 is converted to a
% \pgfplotstreampoint{\pgfpointxy{#1}{#2}}. Extra characters following
% on the line are ignored.
%
% Lines starting with ``%'' and ``#'' are ignored.
%
% Example:
%
% \pgfplotxyfile{tableformgnuplot.dat}
\def\pgfplotxyfile#1{%
\begingroup%
\pgfplotstreamstart%
\openin1=#1
\ifeof1
\PackageWarning{pgf}{Plot data file `#1' not found.}
\else
\catcode`\#=14
\pgf@readxyfile%
\fi
\pgfplotstreamend%
\endgroup%
}
\def\pgf@partext{\par}
\def\pgf@readxyfile{%
\read1 to \pgf@temp%
\let\par=\@@par%
\edef\pgf@temp{\pgf@temp}%
\ifx\pgf@temp\pgf@empty%
\else\ifx\pgf@temp\pgf@partext%
\else%
\expandafter\pgf@parsexyline\pgf@temp\pgf@stop%
\fi\fi%
\ifeof1\else\expandafter\pgf@readxyfile\fi%
}
\def\pgf@parsexyline#1 #2 #3\pgf@stop{%
\pgfplotstreampoint{\pgfpointxy{#1}{#2}}%
}
% Read a plot stream from a file and plot it.
%
% #1 = file from which to read things
%
% File format:
%
% Like xy, except that each line contains three numbers, which are
% converted to xyz coordiantes.
%
% Example:
%
% \pgfplotxyfile{tableformgnuplot.dat}
\def\pgfplotxyzfile#1{%
\begingroup%
\pgfplotstreamstart%
\openin1=#1
\ifeof1
\PackageWarning{pgf}{Plot data file `#1' not found.}
\else
\catcode`\#=14
\pgf@readxyzfile%
\fi
\pgfplotstreamend%
\endgroup%
}
\def\pgf@readxyzfile{%
\read1 to \pgf@temp%
\ifx\pgf@temp\pgf@empty%
\else\ifx\pgf@temp\pgf@partext%
\else%
\expandafter\pgf@parsexyzline\pgf@temp\pgf@stop%
\fi\fi%
\ifeof1\else\expandafter\pgf@readxyzfile\fi%
}
\def\pgf@parsexyzline#1 #2 #3 #4\pgf@stop{%
\pgfplotstreampoint{\pgfpointxyz{#1}{#2}{#3}}%
}
% Render a function using gnuplot.
%
% #1 = filename prefix for .gnuplot and .table files (optional,
% default is \jobname)
% #2 = gnuplot function text
%
% Description:
%
% This command will write a file called #1.gnuplot that sets up
% some gnuplot commands to write output to a file called
% #1.table. Then it calls gnuplot (using the \write18 mechanism)
% to execute the file. Then it reads #2.table using \pgfplotxyfile.
%
% Example:
%
% \pgfplothandlerlineto
% \pgfplotgnuplot[\jobname]{plot [x=0:5] x*sin(x)}
{
\catcode`\%=12
\catcode`\"=12
\xdef\pgf@gnuplot@head#1{set terminal table; set output "#1.table"; set format "%.5f"}
}
\newwrite\pgf@plotwrite
\newif\ifpgf@resample@plot
\def\pgfplotgnuplot{\pgf@ifnextchar[{\pgf@plotgnuplot}{\pgf@plotgnuplot[\jobname]}}%}
\def\pgf@plotgnuplot[#1]#2{%
\pgf@resample@plottrue%
% Check, whether it is up-to-date
\openin\@inputcheck=#1.gnuplot
\ifeof\@inputcheck%
\else%
\read\@inputcheck to\pgf@temp% ignored
\read\@inputcheck to\pgf@plot@line%
\edef\pgf@plot@code{#2\space}%
\ifx\pgf@plot@code\pgf@plot@line%
\openin\@inputcheck=#1.table
\ifeof\@inputcheck%
\else%
\pgf@resample@plotfalse%
\fi%
\fi%
\fi
\ifpgf@resample@plot%
\immediate\openout\pgf@plotwrite=#1.gnuplot
\immediate\write\pgf@plotwrite{\pgf@gnuplot@head{#1}}%
\immediate\write\pgf@plotwrite{#2}%
\immediate\closeout\pgf@plotwrite%
\immediate\write18{gnuplot #1.gnuplot}
\fi%
\pgfplotxyfile{#1.table}
}
\endinput
|