1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17\permanent\protected\def\includegnuplotsvgfile[#1]
18 {\hbox\bgroup
19 \ctxlua{metapost.startsvghashing()}
20 \includesvgfile[#1]
21 \ctxlua{metapost.stopsvghashing()}
22 \egroup}
23
24\startluacode
25
26local modificationtime = lfs.modification
27local longtostring = string.longtostring
28local formatters = string.formatters
29local expandfilename = dir.expandname
30local isfile = lfs.isfile
31
32local 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
45figures.programs.gnuplot = {
46 runner = runner,
47}
48
49local 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
71end
72
73figures.remappers.gp = { svg = remap }
74
75\stopluacode
76
77\continueifinputfile{mgnuplot.mkxl}
78
79\startluacode
80io.savedata("m-gnuplot-demo.gp", [[
81set format xy "$%g$"
82
83set title 'This is a plot of $y=\\sin(x)$'
84set xlabel 'This is the $x$ axis'
85set ylabel 'This is the $y$ axis'
86
87plot [0:6.28] [0:1] sin(x)
88]])
89\stopluacode
90
91\starttext
92
93 \externalfigure[mgnuplotdemo.gp][conversion=svg,width=4cm]
94
95 \externalfigure[mgnuplotdemo.gp][conversion=svg,width=8cm]
96
97\stoptext
98 |