m-gnuplot.mkxl /size: 2677 b    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=m-gnuplot,
3
%D version=2020.02.10,
4
%D title=\CONTEXT\ Extra Modules,
5
%D subtitle=Gnuplot,
6
%D author=Hans Hagen,
7
%D date=\currentdate,
8
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
%C
10
%C This module is part of the \CONTEXT\ macro||package and is
11
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12
%C details.
13 14
%D This is a variant on the \GNUPLOT\ terminal code. At some point (when there is a
15
%D reason) I will make a proper environment that can be used for embedded code.
16 17
\unexpanded
\def
\includegnuplotsvgfile
[#
1
]
%
18
{
\hbox
\bgroup
19
\ctxlua{
metapost
.
startsvghashing
(
)
}
%
20
\includesvgfile
[#
1
]
%
21
\ctxlua{
metapost
.
stopsvghashing
(
)
}
%
22
\egroup
}
23 24
\startluacode
25 26
local
modificationtime
=
lfs
.
modification
27
local
longtostring
=
string
.
longtostring
28
local
formatters
=
string
.
formatters
29
local
expandfilename
=
dir
.
expandname
30
local
isfile
=
lfs
.
isfile
31 32
local
runner
=
sandbox
.
registerrunner
{
33
name
=
"
gnuplot to svg
"
,
34
program
=
"
gnuplot
"
,
35
template
=
longtostring
[
[
36
-
e
"
set output '%newname%'; set terminal svg
"
37
"
%oldname%
"
38
]
]
,
39
checkers
=
{
40
oldname
=
"
readable
"
,
41
newname
=
"
writable
"
,
42
}
,
43
}
44 45
figures
.
programs
.
gnuplot
=
{
46
runner
=
runner
,
47
}
48 49
local
function
remap
(
specification
)
50
local
oldname
=
specification
.
fullname
51
if
oldname
then
52
local
newname
=
file
.
replacesuffix
(
oldname
,
"
svg
"
)
53
local
oldtime
=
modificationtime
(
oldname
)
or
0
54
local
newtime
=
modificationtime
(
newname
)
or
0
55
if
newtime
=
=
0
or
oldtime
>
newtime
then
56
runner
{
57
newname
=
expandfilename
(
newname
)
,
58
oldname
=
expandfilename
(
oldname
)
,
59
}
60
end
61
if
isfile
(
newname
)
then
62
local
only
=
file
.
nameonly
(
newname
)
63
local
name
=
formatters
[
"
svg-%s-inclusion
"
]
(
only
)
64
local
code
=
formatters
[
"
\\includegnuplotsvgfile[%s]\\resetbuffer[%s]
"
]
(
newname
,
name
)
65
buffers
.
assign
(
name
,
code
)
66
specification
.
format
=
"
buffer
"
67
specification
.
fullname
=
name
68
end
69
end
70
return
specification
71
end
72 73
figures
.
remappers
.
gp
=
{
svg
=
remap
}
74 75
\stopluacode
76 77
\continueifinputfile
{
m
-
gnuplot
.
mkxl
}
78 79
\startluacode
80
io
.
savedata
(
"
m-gnuplot-demo.gp
"
,
[
[
81
set
format
xy
"
$%g$
"
82 83
set
title
'
This is a plot of $y=\\sin(x)$
'
84
set
xlabel
'
This is the $x$ axis
'
85
set
ylabel
'
This is the $y$ axis
'
86 87
plot
[
0
:
6
.
28
]
[
0
:
1
]
sin
(
x
)
88
]
]
)
89
\stopluacode
90 91
\starttext
92 93
\externalfigure
[
m
-
gnuplot
-
demo
.
gp
][
conversion
=
svg
,
width
=
4
cm
]
94 95
\externalfigure
[
m
-
gnuplot
-
demo
.
gp
][
conversion
=
svg
,
width
=
8
cm
]
96 97
\stoptext
98