m-gnuplot.mkxl /size: 2686 b    last modification: 2021-10-28 13:51
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\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{m-gnuplot.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[m-gnuplot-demo.gp][conversion=svg,width=4cm]
94
95    \externalfigure[m-gnuplot-demo.gp][conversion=svg,width=8cm]
96
97\stoptext
98