summaryrefslogtreecommitdiff
path: root/macros/context/contrib/context-gnuplot
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/context/contrib/context-gnuplot
Initial commit
Diffstat (limited to 'macros/context/contrib/context-gnuplot')
-rw-r--r--macros/context/contrib/context-gnuplot/VERSION1
-rw-r--r--macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/example.plt4
-rw-r--r--macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.pdfbin0 -> 11414 bytes
-rw-r--r--macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.tex286
-rw-r--r--macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.pdfbin0 -> 441366 bytes
-rw-r--r--macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.tex419
-rw-r--r--macros/context/contrib/context-gnuplot/metapost/context/third/gnuplot/mp-gnuplot.mp969
-rw-r--r--macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkii1032
-rw-r--r--macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkiv977
9 files changed, 3688 insertions, 0 deletions
diff --git a/macros/context/contrib/context-gnuplot/VERSION b/macros/context/contrib/context-gnuplot/VERSION
new file mode 100644
index 0000000000..12a38e971b
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/VERSION
@@ -0,0 +1 @@
+2013.05.09
diff --git a/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/example.plt b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/example.plt
new file mode 100644
index 0000000000..b81e8dce2f
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/example.plt
@@ -0,0 +1,4 @@
+set term context size 5in,3in standalone
+set output "fullpage-example.tex"
+plot sin(x)
+plot cos(atan(x))*sin(x)
diff --git a/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.pdf b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.pdf
new file mode 100644
index 0000000000..216a21e6f0
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.pdf
Binary files differ
diff --git a/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.tex b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.tex
new file mode 100644
index 0000000000..9e99294055
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/fullpage-example.tex
@@ -0,0 +1,286 @@
+% Written by ConTeXt terminal for GNUPLOT on: 2011-09-18 15:32 CEST
+% GNUPLOT version: 4.5.0, terminal version: 1.0.0 (2011-09-16)
+% See also http://wiki.contextgarden.net/Gnuplot
+%
+\usemodule
+ [gnuplot]
+\setupcolors
+ [state=start]
+\setupbodyfont
+ [12pt]
+\setupGNUPLOTterminal
+ [context]
+ [linejoin=mitered, % *mitered* | rounded | beveled
+ linecap=butt, % *butt* | rounded | squared
+ dashed=yes, % *yes* | no
+ dashlength=1, % scaling factor for dash lengths
+ linewidth=1, % scaling factor for line widths (1.0 means 0.5bp)
+ %fontscale=1, % scaling factor for text labels
+ points=metapost, % *metapost* | tex (Should points be drawn with MetaPost or TeX?)
+ images=inline] % *inline* | external (inline only works in MKIV, external requires png support in gnuplot)
+
+\starttext
+
+\startGNUPLOTpage % Graphic Nr. 1
+string gnuplotversion; gnuplotversion := "4.5";
+string termversion; termversion := "1.0";
+% scaling factor, width and height of the figure
+a := 1cm; w := 12.700a; h := 7.620a; % (5in, 3in)
+% temporary variable for storing the path and images
+save p, img, ima; path p; string img, ima;
+% -------------------------
+% Different initialisations
+% -------------------------
+% for additional user-defined settings
+gp_setup_before;
+% text scaling factor for the whole figure
+gp_scale_text := 1;
+% pointsize scaling factor
+gp_set_pointsize(1);
+% linewidth scaling factor for individual lines
+gp_set_linewidth(1);
+% for additional user-defined settings
+gp_setup_after;
+% -------------------------
+gp_set_linetype(-2);
+p := (1.339a,0.842a)--(1.462a,0.842a);
+gp_draw(p);
+p := (12.081a,0.842a)--(11.958a,0.842a);
+gp_draw(p);
+gp_put_text((1.133a, 0.842a), align(right), \sometxt[gp]{-1});
+p := (1.339a,1.479a)--(1.462a,1.479a);
+gp_draw(p);
+p := (12.081a,1.479a)--(11.958a,1.479a);
+gp_draw(p);
+gp_put_text((1.133a, 1.479a), align(right), \sometxt[gp]{-0.8});
+p := (1.339a,2.115a)--(1.462a,2.115a);
+gp_draw(p);
+p := (12.081a,2.115a)--(11.958a,2.115a);
+gp_draw(p);
+gp_put_text((1.133a, 2.115a), align(right), \sometxt[gp]{-0.6});
+p := (1.339a,2.751a)--(1.462a,2.751a);
+gp_draw(p);
+p := (12.081a,2.751a)--(11.958a,2.751a);
+gp_draw(p);
+gp_put_text((1.133a, 2.751a), align(right), \sometxt[gp]{-0.4});
+p := (1.339a,3.388a)--(1.462a,3.388a);
+gp_draw(p);
+p := (12.081a,3.388a)--(11.958a,3.388a);
+gp_draw(p);
+gp_put_text((1.133a, 3.388a), align(right), \sometxt[gp]{-0.2});
+p := (1.339a,4.025a)--(1.462a,4.025a);
+gp_draw(p);
+p := (12.081a,4.025a)--(11.958a,4.025a);
+gp_draw(p);
+gp_put_text((1.133a, 4.025a), align(right), \sometxt[gp]{0});
+p := (1.339a,4.661a)--(1.462a,4.661a);
+gp_draw(p);
+p := (12.081a,4.661a)--(11.958a,4.661a);
+gp_draw(p);
+gp_put_text((1.133a, 4.661a), align(right), \sometxt[gp]{0.2});
+p := (1.339a,5.298a)--(1.462a,5.298a);
+gp_draw(p);
+p := (12.081a,5.298a)--(11.958a,5.298a);
+gp_draw(p);
+gp_put_text((1.133a, 5.298a), align(right), \sometxt[gp]{0.4});
+p := (1.339a,5.934a)--(1.462a,5.934a);
+gp_draw(p);
+p := (12.081a,5.934a)--(11.958a,5.934a);
+gp_draw(p);
+gp_put_text((1.133a, 5.934a), align(right), \sometxt[gp]{0.6});
+p := (1.339a,6.571a)--(1.462a,6.571a);
+gp_draw(p);
+p := (12.081a,6.571a)--(11.958a,6.571a);
+gp_draw(p);
+gp_put_text((1.133a, 6.571a), align(right), \sometxt[gp]{0.8});
+p := (1.339a,7.207a)--(1.462a,7.207a);
+gp_draw(p);
+p := (12.081a,7.207a)--(11.958a,7.207a);
+gp_draw(p);
+gp_put_text((1.133a, 7.207a), align(right), \sometxt[gp]{1});
+p := (1.339a,0.842a)--(1.339a,0.965a);
+gp_draw(p);
+p := (1.339a,7.207a)--(1.339a,7.084a);
+gp_draw(p);
+gp_put_text((1.339a, 0.421a), align(center), \sometxt[gp]{-10});
+p := (4.025a,0.842a)--(4.025a,0.965a);
+gp_draw(p);
+p := (4.025a,7.207a)--(4.025a,7.084a);
+gp_draw(p);
+gp_put_text((4.025a, 0.421a), align(center), \sometxt[gp]{-5});
+p := (6.710a,0.842a)--(6.710a,0.965a);
+gp_draw(p);
+p := (6.710a,7.207a)--(6.710a,7.084a);
+gp_draw(p);
+gp_put_text((6.710a, 0.421a), align(center), \sometxt[gp]{0});
+p := (9.396a,0.842a)--(9.396a,0.965a);
+gp_draw(p);
+p := (9.396a,7.207a)--(9.396a,7.084a);
+gp_draw(p);
+gp_put_text((9.396a, 0.421a), align(center), \sometxt[gp]{5});
+p := (12.081a,0.842a)--(12.081a,0.965a);
+gp_draw(p);
+p := (12.081a,7.207a)--(12.081a,7.084a);
+gp_draw(p);
+gp_put_text((12.081a, 0.421a), align(center), \sometxt[gp]{10});
+p := (1.339a,7.207a)--(1.339a,0.842a)--(12.081a,0.842a)--(12.081a,7.207a)--cycle;
+gp_draw(p);
+gp_set_linetype(0);
+gp_set_color(lt(-2));
+gp_put_text((10.516a, 6.874a), align(right), \sometxt[gp]{sin(x)});
+gp_set_linetype(0);
+p := (10.722a,6.874a)--(11.669a,6.874a);
+gp_draw(p);
+p := (1.339a,5.756a)--(1.448a,5.185a)--(1.556a,4.567a)--(1.665a,3.926a)
+ --(1.773a,3.290a)--(1.882a,2.684a)--(1.990a,2.132a)--(2.099a,1.657a)
+ --(2.207a,1.278a)--(2.316a,1.011a)--(2.424a,0.867a)--(2.533a,0.851a)
+ --(2.641a,0.964a)--(2.750a,1.202a)--(2.858a,1.554a)--(2.967a,2.007a)
+ --(3.075a,2.542a)--(3.184a,3.137a)--(3.292a,3.769a)--(3.401a,4.410a)
+ --(3.509a,5.036a)--(3.618a,5.621a)--(3.726a,6.141a)--(3.835a,6.575a)
+ --(3.943a,6.905a)--(4.052a,7.118a)--(4.160a,7.205a)--(4.269a,7.163a)
+ --(4.377a,6.993a)--(4.486a,6.702a)--(4.594a,6.303a)--(4.703a,5.810a)
+ --(4.811a,5.246a)--(4.920a,4.631a)--(5.028a,3.992a)--(5.137a,3.354a)
+ --(5.245a,2.743a)--(5.354a,2.185a)--(5.462a,1.701a)--(5.571a,1.312a)
+ --(5.679a,1.033a)--(5.788a,0.876a)--(5.896a,0.847a)--(6.005a,0.947a)
+ --(6.113a,1.172a)--(6.222a,1.514a)--(6.330a,1.957a)--(6.439a,2.485a)
+ --(6.547a,3.075a)--(6.656a,3.704a)--(6.764a,4.345a)--(6.873a,4.974a)
+ --(6.981a,5.564a)--(7.090a,6.092a)--(7.198a,6.535a)--(7.307a,6.877a)
+ --(7.415a,7.102a)--(7.524a,7.202a)--(7.632a,7.173a)--(7.741a,7.016a)
+ --(7.849a,6.737a)--(7.958a,6.348a)--(8.066a,5.864a)--(8.175a,5.306a)
+ --(8.283a,4.695a)--(8.392a,4.057a)--(8.500a,3.418a)--(8.609a,2.803a)
+ --(8.717a,2.239a)--(8.826a,1.746a)--(8.934a,1.347a)--(9.043a,1.056a)
+ --(9.151a,0.886a)--(9.260a,0.844a)--(9.368a,0.931a)--(9.477a,1.144a)
+ --(9.585a,1.474a)--(9.694a,1.908a)--(9.802a,2.428a)--(9.911a,3.013a)
+ --(10.019a,3.639a)--(10.128a,4.280a)--(10.236a,4.912a)--(10.345a,5.507a)
+ --(10.453a,6.042a)--(10.562a,6.495a)--(10.670a,6.847a)--(10.779a,7.085a)
+ --(10.887a,7.198a)--(10.996a,7.182a)--(11.104a,7.038a)--(11.213a,6.771a)
+ --(11.321a,6.392a)--(11.430a,5.917a)--(11.538a,5.365a)--(11.647a,4.759a)
+ --(11.755a,4.123a)--(11.864a,3.482a)--(11.972a,2.864a)--(12.081a,2.293a);
+gp_draw(p);
+gp_set_linetype(-2);
+p := (1.339a,7.207a)--(1.339a,0.842a)--(12.081a,0.842a)--(12.081a,7.207a)--cycle;
+gp_draw(p);
+setbounds currentpicture to unitsquare xyscaled (w,h); % 0 0 1 1
+\stopGNUPLOTpage
+\startGNUPLOTpage % Graphic Nr. 2
+string gnuplotversion; gnuplotversion := "4.5";
+string termversion; termversion := "1.0";
+% scaling factor, width and height of the figure
+a := 1cm; w := 12.700a; h := 7.620a; % (5in, 3in)
+% temporary variable for storing the path and images
+save p, img, ima; path p; string img, ima;
+% -------------------------
+% Different initialisations
+% -------------------------
+% for additional user-defined settings
+gp_setup_before;
+% text scaling factor for the whole figure
+gp_scale_text := 1;
+% pointsize scaling factor
+gp_set_pointsize(1);
+% linewidth scaling factor for individual lines
+gp_set_linewidth(1);
+% for additional user-defined settings
+gp_setup_after;
+% -------------------------
+gp_set_linetype(-2);
+p := (1.339a,0.842a)--(1.462a,0.842a);
+gp_draw(p);
+p := (12.081a,0.842a)--(11.958a,0.842a);
+gp_draw(p);
+gp_put_text((1.133a, 0.842a), align(right), \sometxt[gp]{-0.6});
+p := (1.339a,1.903a)--(1.462a,1.903a);
+gp_draw(p);
+p := (12.081a,1.903a)--(11.958a,1.903a);
+gp_draw(p);
+gp_put_text((1.133a, 1.903a), align(right), \sometxt[gp]{-0.4});
+p := (1.339a,2.964a)--(1.462a,2.964a);
+gp_draw(p);
+p := (12.081a,2.964a)--(11.958a,2.964a);
+gp_draw(p);
+gp_put_text((1.133a, 2.964a), align(right), \sometxt[gp]{-0.2});
+p := (1.339a,4.025a)--(1.462a,4.025a);
+gp_draw(p);
+p := (12.081a,4.025a)--(11.958a,4.025a);
+gp_draw(p);
+gp_put_text((1.133a, 4.025a), align(right), \sometxt[gp]{0});
+p := (1.339a,5.085a)--(1.462a,5.085a);
+gp_draw(p);
+p := (12.081a,5.085a)--(11.958a,5.085a);
+gp_draw(p);
+gp_put_text((1.133a, 5.085a), align(right), \sometxt[gp]{0.2});
+p := (1.339a,6.146a)--(1.462a,6.146a);
+gp_draw(p);
+p := (12.081a,6.146a)--(11.958a,6.146a);
+gp_draw(p);
+gp_put_text((1.133a, 6.146a), align(right), \sometxt[gp]{0.4});
+p := (1.339a,7.207a)--(1.462a,7.207a);
+gp_draw(p);
+p := (12.081a,7.207a)--(11.958a,7.207a);
+gp_draw(p);
+gp_put_text((1.133a, 7.207a), align(right), \sometxt[gp]{0.6});
+p := (1.339a,0.842a)--(1.339a,0.965a);
+gp_draw(p);
+p := (1.339a,7.207a)--(1.339a,7.084a);
+gp_draw(p);
+gp_put_text((1.339a, 0.421a), align(center), \sometxt[gp]{-10});
+p := (4.025a,0.842a)--(4.025a,0.965a);
+gp_draw(p);
+p := (4.025a,7.207a)--(4.025a,7.084a);
+gp_draw(p);
+gp_put_text((4.025a, 0.421a), align(center), \sometxt[gp]{-5});
+p := (6.710a,0.842a)--(6.710a,0.965a);
+gp_draw(p);
+p := (6.710a,7.207a)--(6.710a,7.084a);
+gp_draw(p);
+gp_put_text((6.710a, 0.421a), align(center), \sometxt[gp]{0});
+p := (9.396a,0.842a)--(9.396a,0.965a);
+gp_draw(p);
+p := (9.396a,7.207a)--(9.396a,7.084a);
+gp_draw(p);
+gp_put_text((9.396a, 0.421a), align(center), \sometxt[gp]{5});
+p := (12.081a,0.842a)--(12.081a,0.965a);
+gp_draw(p);
+p := (12.081a,7.207a)--(12.081a,7.084a);
+gp_draw(p);
+gp_put_text((12.081a, 0.421a), align(center), \sometxt[gp]{10});
+p := (1.339a,7.207a)--(1.339a,0.842a)--(12.081a,0.842a)--(12.081a,7.207a)--cycle;
+gp_draw(p);
+gp_set_linetype(0);
+gp_set_color(lt(-2));
+gp_put_text((10.516a, 6.874a), align(right), \sometxt[gp]{cos(atan(x))*sin(x)});
+gp_set_linetype(0);
+p := (10.722a,6.874a)--(11.669a,6.874a);
+gp_draw(p);
+p := (1.339a,4.312a)--(1.448a,4.221a)--(1.556a,4.118a)--(1.665a,4.007a)
+ --(1.773a,3.892a)--(1.882a,3.777a)--(1.990a,3.668a)--(2.099a,3.568a)
+ --(2.207a,3.482a)--(2.316a,3.415a)--(2.424a,3.370a)--(2.533a,3.350a)
+ --(2.641a,3.357a)--(2.750a,3.392a)--(2.858a,3.456a)--(2.967a,3.547a)
+ --(3.075a,3.663a)--(3.184a,3.802a)--(3.292a,3.958a)--(3.401a,4.128a)
+ --(3.509a,4.304a)--(3.618a,4.480a)--(3.726a,4.649a)--(3.835a,4.805a)
+ --(3.943a,4.939a)--(4.052a,5.046a)--(4.160a,5.117a)--(4.269a,5.148a)
+ --(4.377a,5.134a)--(4.486a,5.072a)--(4.594a,4.959a)--(4.703a,4.794a)
+ --(4.811a,4.578a)--(4.920a,4.315a)--(5.028a,4.008a)--(5.137a,3.663a)
+ --(5.245a,3.289a)--(5.354a,2.896a)--(5.462a,2.494a)--(5.571a,2.097a)
+ --(5.679a,1.721a)--(5.788a,1.384a)--(5.896a,1.107a)--(6.005a,0.917a)
+ --(6.113a,0.845a)--(6.222a,0.928a)--(6.330a,1.211a)--(6.439a,1.734a)
+ --(6.547a,2.510a)--(6.656a,3.492a)--(6.764a,4.557a)--(6.873a,5.539a)
+ --(6.981a,6.315a)--(7.090a,6.838a)--(7.198a,7.121a)--(7.307a,7.204a)
+ --(7.415a,7.132a)--(7.524a,6.942a)--(7.632a,6.665a)--(7.741a,6.328a)
+ --(7.849a,5.952a)--(7.958a,5.555a)--(8.066a,5.153a)--(8.175a,4.760a)
+ --(8.283a,4.386a)--(8.392a,4.041a)--(8.500a,3.734a)--(8.609a,3.471a)
+ --(8.717a,3.255a)--(8.826a,3.090a)--(8.934a,2.977a)--(9.043a,2.915a)
+ --(9.151a,2.901a)--(9.260a,2.932a)--(9.368a,3.003a)--(9.477a,3.110a)
+ --(9.585a,3.244a)--(9.694a,3.400a)--(9.802a,3.569a)--(9.911a,3.745a)
+ --(10.019a,3.921a)--(10.128a,4.091a)--(10.236a,4.247a)--(10.345a,4.386a)
+ --(10.453a,4.502a)--(10.562a,4.593a)--(10.670a,4.657a)--(10.779a,4.692a)
+ --(10.887a,4.699a)--(10.996a,4.679a)--(11.104a,4.634a)--(11.213a,4.567a)
+ --(11.321a,4.481a)--(11.430a,4.381a)--(11.538a,4.272a)--(11.647a,4.157a)
+ --(11.755a,4.042a)--(11.864a,3.931a)--(11.972a,3.828a)--(12.081a,3.737a);
+gp_draw(p);
+gp_set_linetype(-2);
+p := (1.339a,7.207a)--(1.339a,0.842a)--(12.081a,0.842a)--(12.081a,7.207a)--cycle;
+gp_draw(p);
+setbounds currentpicture to unitsquare xyscaled (w,h); % 0 0 1 1
+\stopGNUPLOTpage
+\stoptext
diff --git a/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.pdf b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.pdf
new file mode 100644
index 0000000000..23be828b9a
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.pdf
Binary files differ
diff --git a/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.tex b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.tex
new file mode 100644
index 0000000000..2275d74dd0
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/doc/context/third/gnuplot/gnuplot-context-doc.tex
@@ -0,0 +1,419 @@
+\setupcolors
+ [state=start]
+\enableregime
+ [utf-8]
+\setupinteraction
+ [state=start,color=blue]
+
+
+\usemodule
+ [gnuplot]
+
+\setuplayout
+ [backspace=2cm,
+ width=middle,
+ topspace=2cm,
+ heigh=middle,
+ header=0pt,
+ headerdistance=0pt]
+\setupwhitespace
+ [medium]
+
+\setuphead
+ [title]
+ [align=middle,
+ before={\strut\blank[24pt]},
+ style=\bfc]
+\setuphead
+ [section]
+ [style=\bfb]
+\setuphead
+ [subsection]
+ [style=\bfa]
+
+\setupitemize
+ [headstyle=bold]
+
+% \usetypescript
+% [antykwa-torunska]
+% \setupbodyfont
+% [antykwa]
+
+\definetyping
+ [GP]
+ [escape={[[,]]},lines=no]
+\setuptyping
+ [TEX]
+ [stlye=\tfx]
+
+% \usetypescript[lucida][ec] \setupbodyfont[lucida]
+\setupbodyfont
+ [lucidanova]
+
+\definecolor
+ [maincolor]
+ [darkblue]
+\definecolor
+ [lightblue]
+ [r=0.9,g=0.9,b=1]
+\definecolor
+ [screen]
+ [s=0.9]
+
+
+\logo [GNUPLOT] {{\sc gnuplot}}
+\logo [TEXLIVE] {\TeX\ Live}
+\logo [MIKTEX] {MiK\TeX}
+\logo [TIKZ] {Tik{\it Z}}
+\logo [MKII] {{\sc mkii}}
+\logo [CONTEXTMKII] {\ConTeXt\ \MKII}
+
+\defineframedtext
+ [background]
+ [background=color,backgroundcolor=gray,width=\textwidth,frame=off,offset=2pt,style=type]
+
+\defineframedtext
+ [myinput]
+ [background=color,backgroundcolor=lightblue,width=\textwidth,frame=off,offset=2pt,style=type]
+
+% \setupbackgrounds
+% [background]
+% [background=color,backgroundcolor=gray,width=\textwidth,frame=off]
+
+\starttext
+
+\title{Using \color[maincolor]{\tt\bf context} and \color[maincolor]{\tt\bf tikz} terminals for gnuplot in \ConTeXt}
+
+\centerline{Mojca Miklavec, \date[y=2013,m=4,d=19][year,--,mm,--,day]}
+
+{\it With special thanks to Hans Hagen, Taco Hoekwater, Aditya Mahajan, Marco Patzer and others.}
+
+\blank
+
+\placelist[section]
+
+\blank
+
+\section{Requirements}
+
+\startitemize[n,packed]
+\item Any \ConTeXt\ installation (\ConTeXt\ Distribution, \TEXLIVE\ 2011 or later, \dots).
+\item Gnuplot 4.6.0 or later with {\tt context} and/or {\tt tikz} terminal built in. The {\tt gnuplot} binary (or {\tt gnuplot.exe} under Windows) has to be in {\tt PATH}.
+\item Gnuplot module for ConTeXt.
+\item For running \GNUPLOT\ on the fly or when using \CONTEXTMKII, you need to have {\tt write18} enabled. Usually this can be set with {\tt shell\_escape = t} in {\tt texmf.cnf}.
+\stopitemize
+
+
+\section{Installation}
+
+\subsection{Gnuplot}
+
+As long as you have gnuplot $\geq 4.6.0$ installed, you should have the {\tt context} terminal built in. If not, you might want to compile gnuplot from CVS sources:
+
+\starttyping
+cd gnuplot
+./prepare
+./configure [--prefix=$PWD/install]
+make
+make install
+\stoptyping
+
+The option \type{--prefix=$PWD/install} (but you can choose anything) -- if chosen -- will install gnuplot locally even if you lack root permissions. Just make sure that you add the resulting binary to {\tt PATH}.
+
+Once you have the gnuplot binary running, you can check the list of supported terminals by typing
+\startlines
+\tt{gnuplot>} \bf set term
+\stoplines
+into \GNUPLOT\ shell. Make sure that it lists:
+\starttyping
+ context ConTeXt with MetaFun (for PDF documents)
+ tikz Lua PGF/TikZ terminal for TeX and friends
+\stoptyping
+
+The module also supports some other terminals like {\tt png}, {\tt metapost}, {\tt postscript} and {\tt pdf}, but their integration with \CONTEXT\ is limited.
+
+\subsection{{\tt\bf t-gnuplot} module for \CONTEXT}
+
+Under \CONTEXT\ Distribution you can install the gnuplot module and \TIKZ\ with an additional switch when running {\tt first-setup}, for example:
+\startbackground
+\tt ./first-setup.sh --modules=gnuplot,tikz
+\stopbackground
+
+If you have installed a {\tt complete} or {\tt context} scheme under \TEXLIVE, gnuplot module and \TIKZ\ might already be installed. Else you can use:
+\startbackground
+tlmgr install context-gnuplot\\
+tlmgr install pgf
+\stopbackground
+
+Under \MIKTEX\ the module is installed automatically when it is first used (but at the moment of writing \MIKTEX\ doesn't support \ConTeXt).
+
+\page
+\section{Simple examples}
+
+\subsection[calling gnuplot directly]{Calling gnuplot directly}
+
+Let's first create a simple file (we will call it {\tt\it example.plt}, but you may choose any name) with the contents below.
+
+For {\tt context} terminal:
+\startmyinput
+set term context size 5in,3in standalone\\
+set output "fullpage-example.tex"\\
+plot sin(x)\\
+plot cos(atan(x))*sin(x)
+\stopmyinput
+
+For {\tt tikz} terminal:
+\startmyinput
+set term tikz context size 5in,3in standalone createstyle\\
+set output "fullpage-example.tex"\\
+plot sin(x)\\
+plot cos(atan(x))*sin(x)
+\stopmyinput
+
+In both cases the option {\tt standalone} is used to create a complete \CONTEXT\ document with one plot per page, including header and {\tt\textbackslash starttext \dots\ \textbackslash stoptext}, so that it can be compiled directly. The option {\tt createstyle} is used to create three files with required macros in working directory\footnote{An alternative is to place those three files somewhere where kpathsea can find them and omit the option {\tt createstyle}, just make sure that the versions of {\tt tikz} terminal and the files in your \TeX\ tree remain compatible.}.
+
+Both terminals should give you almost equivalent results apart from default plot size. You are highly encouraged to specify the desired plot size explicitly. You may scale the plot later on, but you probably want to get the desired proportions from the start.
+
+\page
+Run gnuplot with
+\startbackground
+gnuplot example.plt
+\stopbackground
+and compile the result with any of the following three commands (depending on your preferred engine):
+\startbackground
+\hbox{\hbox to 25em{context fullpage-example.tex \hss} {\rm\# for \LuaTeX}}
+\hbox{\hbox to 25em{texexec fullpage-example.tex \hss} {\rm\# for \pdfTeX}}
+\hbox{\hbox to 25em{texexec --xtx fullpage-example.tex \hss} {\rm\# for \XeTeX} }
+\stopbackground
+
+They are almost equivalent except that \XeTeX\ lacks some advanced features (some patterns). The only major difference is the choice of fonts. If you want to typeset Arabic labels or use system fonts, you will probably want to choose \LuaTeX\ or \XeTeX. If you are using many graphical elements (3D plots, images, \dots), you might want to go for \LuaTeX.
+
+You should get a {\sc pdf} document with two full-page plots that you can include into your document with \type{\externalfigure[fullpage-example][page=2]} for example.
+
+\placefigure[force]{Second page from \type{fullpage-example}, included with \type+\externalfigure+}{\externalfigure[fullpage-example][page=2]}
+
+\page
+\subsection{Calling gnuplot from \TeX}
+
+As you can see you will always get Latin Modern font at 12pt unless you explicitly change it with {\tt header "\textbackslash setupbodyfont[somefontname,10pt]"} or with {\tt font "somefontname,10pt"}. An easier way to make sure that the same font is used and to avoid having to call gnuplot manually is to simply type the gnuplot code inside your \CONTEXT\ document:
+
+\startTEX
+\usemodule
+ [gnuplot]
+\setupGNUPLOTterminal
+ [context]
+ [width=5in,height=2.5in,fontscale=0.9]
+\setupGNUPLOTterminal
+ [tikz]
+ [width=5in,height=2.5in,fontscale=0.9]
+\starttext
+
+\startGNUPLOTinclusions
+set samples 400
+set key left Left reverse
+\stopGNUPLOTinclusion
+
+\startGNUPLOTscript[myfunction]
+set zeroaxis
+set format y "%.1f"
+plot [-4:2][0:2] 1 t '' lt 0, exp(x) t '$e^x$' lt 1 lw 3
+plot cos(atan(x))*sin(x) t '$\cos(\arctan(x))\sin(x)$' lw 3 lc 3
+\stopGNUPLOTscript
+
+\placefigure{none}{\useGNUPLOTgraphic[myfunction][2]}
+
+\setupGNUPLOT
+ [terminal=tikz]
+
+\placefigure{none}{\useGNUPLOTgraphic[myfunction][1]}
+
+\stoptext
+\stopTEX
+
+\page
+\bgroup
+\setuptype[option=TEX]
+With \type+\setupGNUPLOT[terminal=<termname>]+ you can select any supported gnuplot terminal before drawing a plot.
+
+With \type+\setupGNUPLOT[<termname>][<option>=<value>]+ you can set some terminal-specific options.
+
+Anything inside \type+\startGNUPLOTinclusions ... \stopGNUPLOTinclusion+ will be applied to every plot.
+
+The command \type+\startGNUPLOTscript[<name>]+ creates new plots that can be included with \type+\useGNUPLOTgraphic[<name>][<number>][<option>=<value>]+. Both the number of plot and additional parameters (like \type+width=.7\textwidth+ for example) are optional.
+\egroup
+
+\bgroup
+\setupGNUPLOTterminal
+ [context]
+ [width=5in,height=2.5in,fontscale=0.9]
+\setupGNUPLOTterminal
+ [tikz]
+ [width=5in,height=2.5in,fontscale=0.9]
+\startGNUPLOTscript[myfunction]
+set samples 400
+set key left Left reverse
+set zeroaxis
+set format y "%.1f"
+plot [-4:2][0:2] 1 t '' lt 0, exp(x) t '$e^x$' lt 1 lw 3
+plot cos(atan(x))*sin(x) t '$\cos(\arctan(x))\sin(x)$' lw 3 lc 3
+\stopGNUPLOTscript
+\placefigure[force]{Framed second plot using {\tt context} terminal}
+ {\framed[offset=overlay,frame=off,background=color,backgroundcolor=lightblue]{\useGNUPLOTgraphic[myfunction][2]}}
+\setupGNUPLOT[terminal=tikz]
+\placefigure[force]{Framed first plot using {\tt tikz} terminal}
+ {\framed[offset=overlay,frame=off,background=color,backgroundcolor=lightblue]{\useGNUPLOTgraphic[myfunction][1]}}
+\egroup
+
+
+\subsection{Including pre-generated plots}
+
+\bgroup
+\setuptype[option=TEX]
+Instead of defining \type+\startGNUPLOTscript+ and letting \CONTEXT\ call \GNUPLOT\ on the fly, you can also run \GNUPLOT\ in advance and only include the resulting {\tt filename.tex}. This is something that you might want to do when running calculation-intensive \GNUPLOT\ scripts which take a long time.
+
+You can follow the same steps as in section \in[calling gnuplot directly], except that you should not specify the {\tt standalone} flag (and you should not compile the plot, only the main document).
+
+The resulting file can be included\footnote{{\tt\textbackslash include filename.tex} won't work} with
+\startTEX
+\processGNUPLOTfile[<name>][<filename.tex>]
+\stopTEX
+and you can get the graphic with the same command as usual:
+\startTEX
+\useGNUPLOTgraphic[<name>]
+\stopTEX
+plus any optional parameters.
+
+\egroup
+
+\page
+\section{Terminal options}
+
+\subsection{\tt\bf context}
+
+\startGP
+set term context { default }
+ { defaultsize | size <xsize> {in|cm}, <ysize> {in|cm} }
+ { [[\bf input]] | standalone }
+ { [[\bf timestamp]] | notimestamp }
+ { [[\bf noheader]] | header "<header>" }
+ { [[\bf color]] | colour | monochrome }
+ { [[\bf rounded]] | mitered | beveled }
+ { [[\bf round]] | butt | squared }
+ { [[\bf dashed]] | solid }
+ { dashlength | dl <DL> }
+ { linewidth | lw <LW> }
+ { fontscale <fontscale> }
+ { [[\bf mppoints]] | texpoints }
+ { [[\bf inlineimages]] | externalimages }
+ { [[\bf defaultfont]] | font {<fontsize>} |
+ font "<fontname>{,<fontsize>}" {fontsize} }
+\stopGP
+
+\page
+\subsection{\tt\bf tikz}
+
+\def\my#1{\color[blue]{\bf#1}}
+\startGP
+set term tikz { latex | tex | [[\my{context}]] }
+ { [[\my{size}]] <x>{unit},<y>{unit} }
+ { scale <x>,<y> }
+ { nofulldoc | nostandalone | fulldoc | standalone }
+ { [[\bf color]] | monochrome }
+ { [[\bf dashed]] | solid }
+ { nooriginreset | originreset }
+ { nogparrows | gparrows }
+ { nogppoints | gppoints }
+ { picenvironment | nopicenvironment }
+ { noclip | clip }
+ { notightboundingbox | tightboundingbox }
+ { background "<colorpec>" }
+ { plotsize <x>{unit},<y>{unit} }
+ { [[\my{charsize}]] <x>{unit},<y>{unit} }
+ { font "<fontdesc>" }
+ { fontscale <fontscale> }
+ { {preamble | header} "<preamble_string>" }
+ { tikzplot <ltn>,... }
+ { notikzarrows | tikzarrows }
+ { rgbimages | cmykimages }
+ { noexternalimages | externalimages }
+ { bitmap | nobitmap }
+ { providevars <var name>,... }
+ { [[\my{createstyle}]] }
+ { help }
+\stopGP
+
+\page
+\section{Comparison of supported terminals}
+
+The gnuplot module for \ConTeXt\ supports the following terminals:
+\startitemize[packed]
+\head bitmap terminals\par
+ \startitemize[packed,joinedup]
+ \item\tt png, pngcairo
+ \stopitemize
+\head vector terminals\par
+ \startitemize[packed,joinedup]
+ \item\tt {\bf context}, {\bf tikz}
+ \item\tt metapost, postscript, pdf, pdfcairo
+ \stopitemize
+\stopitemize
+
+\startGNUPLOTscript[bitmap]
+# this should not be done
+set term pngcairo truecolor size 1000,1000 linewidth 4
+set palette defined ( 0 "#FFF8BF", 1 "#FFC20B", 2 "red", 3 "#501080", 4 "blue", 5 "#88BBEE")
+
+unset border
+set dummy u,v
+set format cb "%.1f"
+unset key
+set parametric
+set view 60, 30, 1.5, 0.9
+set isosamples 200, 200
+set size ratio -1
+set noxtics
+set noytics
+set noztics
+set urange [ -3.14159 : 3.14159 ] noreverse nowriteback
+set vrange [ -3.14159 : 3.14159 ] noreverse nowriteback
+set pm3d depthorder
+unset colorbox
+f(x,y) = sin(-sqrt((x+5)**2+(y-7)**2)*0.5)
+GPFUN_f = "f(x,y) = sin(-sqrt((x+5)**2+(y-7)**2)*0.5)"
+splot cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v) with pm3d, \
+ 1+cos(u)+.5*cos(u)*cos(v),.5*sin(v),sin(u)+.5*sin(u)*cos(v) with pm3d
+\stopGNUPLOTscript
+
+\setupGNUPLOT[terminal=pngcairo]
+\placefigure{An example of graphic generated with png terminal}{\hbox to \textwidth{\hss\framed{\useGNUPLOTgraphic[bitmap][scale=700]}\hss}}
+
+\page
+\section{Known bugs}
+
+\subsection{Buggy implementation in \ConTeXt\ module}
+
+\startitemize[packed]
+\item Point sizes of \TeX\ symbols for points have to be fine-tuned for proper size.
+\item Points don't scale properly. Line widths should not be scaled when bigger points are requested. Also, when thicker lines are used, points don't inherit that thickness. The reason is buggy implementation that stores all points as pictures in the beginning instead of drawing each point separately when that is requested.
+\item Patterns fills are a semi-hack. They are composed out of little tiles and drawn next to each other. This doesn't look properly when rendered. This also means that line widths don't scale properly.
+\item MetaPost could be highly optimized. In particular the transparency should be handled more efficiently.
+\stopitemize
+
+\subsection{Support in \ConTeXt\ core}
+
+\startitemize[packed]
+\item Switching to a different font for font labels doesn't work in {\sc mkiv} and uses an ugly hack in {\sc mkii}.
+\item External images don't work in {\sc mkiv} at the moment. Use {\tt images=inline} ({\tt inlineimages} in gnuplot). This is because the only acceptable {\sc mkii} syntax is {\tt externalfigure "name.png"}, while {\sc mkiv} requires {\tt draw externalfigure "name.png"}. This has to be fixed in \ConTeXt\ core.
+\item Transparent inline images are not yet supported.
+\item There might be still some memory leaks in MetaPost. The major ones were fixed.
+\stopitemize
+
+\subsection{Limitations}
+
+\startitemize[packed]
+\item Plots with many graphical elements don't work in {\sc mkii} since \TeX\ runs out of memory.
+\item Inline bitmap images are not (and might never be) supported in {\sc mkii}. If you want to use external bitmap images, use the option {\tt externalimages} in {\tt context} terminal ({\tt images=external} in \ConTeXt).
+\stopitemize
+
+
+\stoptext
diff --git a/macros/context/contrib/context-gnuplot/metapost/context/third/gnuplot/mp-gnuplot.mp b/macros/context/contrib/context-gnuplot/metapost/context/third/gnuplot/mp-gnuplot.mp
new file mode 100644
index 0000000000..5d645c4fe2
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/metapost/context/third/gnuplot/mp-gnuplot.mp
@@ -0,0 +1,969 @@
+
+if known context_gnuplot : endinput; fi;
+
+boolean context_gnuplot ; context_gnuplot := true;
+
+ string gnuplotversion;
+
+ def gp_setup_before =
+ enddef;
+
+ def gp_setup_after =
+ enddef;
+
+ newinternal gp_align_center, gp_align_left, gp_align_right;
+ gp_align_center := 0;
+ gp_align_left := 1;
+ gp_align_right := 2;
+
+
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %%
+ %% Colors & Dashes
+ %%
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ boolean gp_use_color; gp_use_color := true;
+ boolean gp_use_dashed; gp_use_dashed := true;
+
+ % default background color (should we set it somewhere?)
+ color gp_color_background; gp_color_background := white;
+ % default foreground color (also set in t-gnuplot.tex)
+ color gp_color_foreground; gp_color_foreground := black;
+ % current color
+ color gp_color_current; gp_color_current := gp_color_foreground;
+
+ % colors corresponding to linetypes
+ %
+ % LT_AXIS (-1)
+ % LT_BLACK (-2)
+ % LT_NODRAW (-3)
+ % LT_BACKGROUND (-4)
+ % LT_UNDEFINED (-5)
+
+ color gp_color_lt[];
+ numeric gp_num_linetype; gp_num_linetype := 9;
+ gp_color_lt[-4] = gp_color_background;
+ gp_color_lt[-3] = gp_color_background; % TODO: nodraw!!!
+ gp_color_lt[-2] = gp_color_foreground;
+ % TODO: do we need this to be configurable?
+ gp_color_lt[-1] = gp_color_foreground; % coordinate axes
+ gp_color_lt[0] = red;
+ gp_color_lt[1] = green;
+ gp_color_lt[2] = blue;
+ gp_color_lt[3] = magenta;
+ gp_color_lt[4] = cyan;
+ gp_color_lt[5] = yellow;
+ gp_color_lt[6] = black;
+ gp_color_lt[7] = (1,.3,0); % orange; perhaps it's defined already
+ gp_color_lt[8] = .5white;
+
+ % dashes corresponding to linetypes - copied from PostScript terminal
+ % perhaps check the other terminals as well!!!
+
+ % scaling factor for dash lengths
+ gp_scale_dashlength := 1;
+ % TODO: set some sensible default scaling factor (compare with PostScript)
+ gp_dashlength := 1;
+
+ % TODO: watch out linecaps and linejoins !!! (possibly rewrite)
+ % TODO: compare with PostScript lines and possibly fix,
+ % so that they will match
+ %
+ % TODO: write a macro to make these definitions slightly nicer
+ %
+ % linecap := butt; ???
+ picture gp_dash_lt[];
+ gp_dash_lt[-3] := dashpattern(on 0 off 1) scaled (gp_scale_dashlength*gp_dashlength); % TODO: this is wrong anyway; it should generate an XORed line
+ gp_dash_lt[-2] := dashpattern();
+ % for axes
+ gp_dash_lt[-1] := dashpattern(on 2 off 1) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[0] := dashpattern();
+ gp_dash_lt[1] := dashpattern(on 4 off 2) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[2] := dashpattern(on 2 off 3) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[3] := dashpattern(on 1 off 1.5) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[4] := dashpattern(on 6 off 2 on 1 off 2) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[5] := dashpattern(on 3 off 3 on 1 off 3) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[6] := dashpattern(on 2 off 2 on 2 off 6) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[7] := dashpattern(on 1 off 2 on 6 off 2 on 1 off 2) scaled (gp_scale_dashlength*gp_dashlength);
+ gp_dash_lt[8] := dashpattern(on 2 off 2 on 2 off 2 on 2 off 2 on 2 off 4) scaled (gp_scale_dashlength*gp_dashlength);
+
+ % returns the color for the given linetype modulo gp_num_linetype
+ vardef gp_color_linetype(expr linetype) =
+ (if linetype < -3:
+ gp_color_lt[-3]
+% else:
+ elseif linetype >= gp_num_linetype:
+ gp_color_lt[(linetype mod gp_num_linetype)]
+% gp_color_lt[1]
+ else:
+ gp_color_lt[linetype]
+ fi)
+ % TODO: chech if the number is defined
+ % TODO: check if gp_use_color
+% if linetype = -2 :
+% gp_color_lt[-2]
+% elseif linetype = -1 :
+% gp_color_lt[-1]
+% elseif linetype = -1 :
+% gp_color_lt[-3]
+% else :
+% % TODO: check if the number is defined
+% % check, if the number is positive
+% gp_color_lt[ (linetype mod gp_num_linetype) ]
+% fi;
+ enddef;
+
+ def gp_dash_linetype(expr linetype) =
+ (if linetype < -3:
+ gp_dash_lt[-3]
+ elseif linetype >= gp_num_linetype:
+ gp_dash_lt[(linetype mod gp_num_linetype)]
+ else:
+ gp_dash_lt[linetype]
+ fi)
+ % TODO: chech if the number is defined
+ % TODO: check if gp_use_color
+% if linetype = -2 :
+% gp_color_lt[-2]
+% elseif linetype = -1 :
+% gp_color_lt[-1]
+% elseif linetype = -1 :
+% gp_color_lt[-3]
+% else :
+% % TODO: check if the number is defined
+% % check, if the number is positive
+% gp_color_lt[ (linetype mod gp_num_linetype) ]
+% fi;
+ enddef;
+
+ % gp_set_color(lt(3))
+ % gp_set_color(rgb(.5,.5.0))
+ % gp_set_color(frac(.3))
+ def gp_set_color(text t) =
+ begingroup;
+ % sets the color to the given (r,g,b) triple
+ def rgb(expr r,g,b) =
+ gp_color_current := (r,g,b);
+ enddef;
+
+ % sets the color so that it matches the color of linetype
+ def lt(expr linetype) =
+ gp_color_current := gp_color_linetype(linetype);
+ enddef;
+
+ % sets the color either to gray or according to the current palette
+ def frac(expr fraction) =
+ % "if palette = gray"
+ if gp_palette_color_mode = gp_palette_color_mode_rgb:
+ gp_color_current := (
+ gp_palette_function(gp_palette_formula_r,fraction),
+ gp_palette_function(gp_palette_formula_g,fraction),
+ gp_palette_function(gp_palette_formula_b,fraction));
+ else:
+ % TODO - more advanced paletter
+ gp_color_current := fraction*white; % XXX: should this be (1-frac)*white perhaps?
+ fi;
+ enddef;
+
+ % execute the content - calls one of the definitions from above
+ t;
+ endgroup;
+ enddef;
+
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %%
+ %% Palette
+ %%
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ % for "colorMode==SMPAL_COLOR_MODE_GRADIENT"
+ % These variables are used to define interpolated color palettes:
+ % there are gradient_num pairs of (frac/gray, color) mappings
+% numeric gp_palette.gradient_num;
+% numeric gp_palette.gradient[].frac;
+% color gp_palette.gradient[].abc;
+ %
+ % see palette_color_mode in color.h
+ newinternal
+ gp_palette_color_mode_none,
+ gp_palette_color_mode_gray,
+ gp_palette_color_mode_rgb,
+ gp_palette_color_mode_functions,
+ gp_palette_color_mode_gradient;
+
+ gp_palette_color_mode_none := 0;
+ gp_palette_color_mode_gray := 1; % grayscale only
+ gp_palette_color_mode_rgb := 2; % one of several fixed transformations
+ gp_palette_color_mode_functions := 3; % user-defined transforms
+ gp_palette_color_mode_gradient := 4; % interpolated table: explicitely defined or read from file
+
+ % there is no palette initialized by default
+ numeric gp_palette_color_mode;
+ gp_palette_color_mode := gp_palette_color_mode_none;
+
+ % mapping formulae for gp_palette_color_mode_rgb
+ numeric gp_palette_formula_r; gp_palette_formula_r := 3; % f(x)=x
+ numeric gp_palette_formula_g; gp_palette_formula_g := 3;
+ numeric gp_palette_formula_b; gp_palette_formula_b := 3;
+
+ % explicitely defined color gradient
+ color gp_palette_gradient_colors[];
+ numeric gp_palette_gradient_pos[];
+ % number of defined colors
+ numeric gp_palette_gradient_n;
+
+ % gamma for gray scale palettes only
+ numeric gp_palette_gamma;
+
+% % color model: RGB/HSV/CMY; YIQ, XYZ and others not supported
+% newinternal gp_palette.model.rgb, gp_palette.model.hsv, gp_palette.model.cmy;%, gp_palette.model.yiq, gp_palette.model.xyz;
+% gp_palette.rgb := 0;
+% gp_palette.hsv := 1;
+% gp_palette.cmy := 2;
+ %gp_palette.yiq := 3;
+ %gp_palette.xyz := 4;
+
+ % color model - RGB/HSV/CMY (ie. one of the models above)
+% numeric gp_palette.model;
+
+ % for "colorMode == SMPAL_COLOR_MODE_FUNCTIONS"
+ % R, G abd B for RGB
+ % H, S and V for HSV
+ % C, M and Y for CMY
+% color gp_palette.abc;
+
+ % gamma (only used for gray scale palettes)
+% numeric gp_palette.gamma; gp_palette.gamma = 1;
+
+ def gp_make_palette(text t) =
+ begingroup;
+ % let hack: save comma
+% let@=,;
+
+ def color_mode(text mode) =
+ % let hack
+% let,=@; let@=,;
+ save gray, rgb, functions, gradient;
+ gray = gp_palette_color_mode_gray;
+ rgb = gp_palette_color_mode_rgb;
+ functions = gp_palette_color_mode_functions;
+ gradient = gp_palette_color_mode_gradient;
+
+ gp_palette_color_mode := mode;
+
+ % if mode=gray:
+ % % TODO
+ % elseif mode=rgb:
+ % % TODO
+ % elseif mode=functions:
+ % % TODO
+ % elseif mode=gradient:
+ % % TODO
+ % fi;
+
+ % let hack: ignore next comma
+% let,=;;
+ enddef;
+
+ % for gp_palette_color_mode_rgb: numbers of formulae
+ def formulae(expr r, g, b) =
+ % let hack
+% let,=@; let@=,;
+ gp_palette_color_mode := gp_palette_color_mode_rgb;
+ gp_palette_formula_r := r;
+ gp_palette_formula_g := g;
+ gp_palette_formula_b := b;
+ % let hack: ignore next comma
+% let,=;;
+ enddef;
+
+ % TODO: for gradient definitions
+ def colors(text tc) =
+ gp_palette_gradient_n := 0;
+ for i=tc:
+ gp_palette_gradient_colors[gp_palette_gradient_n] := i;
+ counter := counter+1;
+ endfor;
+ % let hack
+% let,=@; let@=,;
+ % code goes here
+ % [TODO]
+ %
+ % let hack: ignore the following comma
+% let,=;;
+ enddef;
+
+ % TODO: for gradient definitions
+ def positions(text tp) =
+ gp_palette_gradient_n := 0;
+ for i=tp:
+ gp_palette_gradient_pos[gp_palette_gradient_n] := i;
+ counter := counter+1;
+ endfor;
+ % let hack
+% let,=@; let@=,;
+ % code goes here
+ % [TODO]
+ %
+ % let hack: ignore the following comma
+% let,=;;
+ enddef;
+
+ t;
+ % let hack: restore comma
+% let,=@;
+ endgroup;
+ enddef;
+
+ % Functions copied from GetColorValueFromFormula from getcolor.c
+ vardef gp_palette_function(expr ff, tt) =
+ save f, t;
+ f = ff;
+ t = tt;
+ % the input gray is supposed to be in interval [0,1]
+ % for a negative formula, reverse the palette (negate x)
+ if f < 0:
+ t := -t;
+ f := -f;
+ fi;
+
+ t :=
+ if f = 0: 0
+ elseif f = 1: 0.5
+ elseif f = 2: 1
+ elseif f = 3: t
+ elseif f = 4: t*t
+ elseif f = 5: t*t*t
+ elseif f = 6: t*t*t*t
+ elseif f = 7: sqrt(t)
+ elseif f = 8: sqrt(sqrt(t))
+ elseif f = 9: sind(90t)
+ elseif f = 10: cosd(90t)
+ elseif f = 11: abs(t-0.5)
+ elseif f = 12: (2t-1)*(2t-1)
+ elseif f = 13: sind(180t)
+ elseif f = 14: abs(cosd(180t))
+ elseif f = 15: sind(360t)
+ elseif f = 16: cosd(360t)
+ elseif f = 17: abs(sind(360t))
+ elseif f = 18: abs(cosd(360t))
+ elseif f = 19: abs(sind(720t))
+ elseif f = 20: abs(cosd(720t))
+ elseif f = 21: 3t
+ elseif f = 22: 3t-1
+ elseif f = 23: 3t-2
+ elseif f = 24: abs(3t-1)
+ elseif f = 25: abs(3t-2)
+ elseif f = 26: 1.5t-0.5
+ elseif f = 27: 1.5t-1.0
+ elseif f = 28: abs(1.5t-0.5)
+ elseif f = 29: abs(1.5t-1.0)
+ elseif f = 30: if t <= 0.25: 0
+ elseif t <= 0.57: t/0.32-0.78125
+ else : 1
+ fi
+ elseif f = 31: if t <= 0.42: 0
+ elseif t <= 0.92: 2t-0.84
+ else : 1
+ fi
+ elseif f = 32: if t <= 0.42: 4t
+ elseif t <= 0.92: -2t+1.84
+ else : t/0.08-11.5
+ fi
+ elseif f = 33: abs(2t-0.5)
+ elseif f = 34: 2t
+ elseif f = 35: 2t-0.5
+ elseif f = 36: 2t-1.0
+ else : t % ERROR
+ fi;
+
+ if t < 0: 0
+ elseif t < 1: t
+ else : 1
+ fi
+
+ % TODO: add a note to getcolor.c
+ enddef;
+
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %%
+ %% Scaling factors
+ %%
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ % --> fixed
+ % default linewidth in metapost: 0.5bp
+ numeric gp_internal_linewidth; gp_internal_linewidth := 0.5bp;
+
+ % --> set globally by user (usually with TeX code)
+ % scaling factor for points
+ numeric gp_scale_pointsize; gp_scale_pointsize := 1;
+ % scaling factor for line widths (for the whole plot)
+ numeric gp_scale_linewidth; gp_scale_linewidth := 1;
+
+ % --> calculated based on gnuplot's directives in the middle of plot
+ % the true linewidth; equivalent of gp_set_linewidth(1);
+ numeric gp_linewidth; gp_linewidth := gp_scale_linewidth*gp_internal_linewidth;
+
+ numeric gp_linetype; gp_linetype := -3;
+
+ % set the scaling factor for point sizes
+ def gp_set_pointsize(expr pointsize) =
+ gp_scale_pointsize := pointsize;
+ enddef;
+ % set the scaling factor for line widths
+ def gp_set_linewidth(expr linewidth) =
+ gp_linewidth := linewidth*gp_scale_linewidth*gp_internal_linewidth;
+ enddef;
+ % set the linetype:
+ % -3: solid with XOR, probably don't need that
+ % -2: border, perhaps always with linejoin mitered
+ % -1: axis
+ % 0 or more : the rest
+ def gp_set_linetype(expr linetype) =
+ gp_linetype := linetype;
+ gp_color_current := gp_color_linetype(linetype);
+ enddef;
+
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %%
+ %% FONTS & strings
+ %%
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ % support for textext("...") has been dropped for two reasons:
+ % - I don't intend to support it any more
+ % - Extremely inefficient anyway (almost useless for gnuplot-generated graphics)
+
+ % scalig factor for text labels (relative to the document font)
+ % if you set the font to 10pt and use scaling factor 0.7,
+ % you'll get 7pt, not 10pt (but using the scaled 10pt design size ;)
+ numeric gp_scale_text; gp_scale_text := 1;
+
+ % Returns an image with colored text according to current color
+ % This only makes sense for mkiv text. Mkii is handled with \sometxt
+ % and colors are working properly there. We could be using textext("")
+ % in the low level, but we redefined the command just before calling this funtion
+ % for text labels, so we really need something more low-level (rawtextext in this case)
+ vardef gp_colored_mkiv_text(expr str) =
+
+ % since withcolor doesn't work any more, we need to provide a workaround.
+ save r, g, b;
+ r = redpart gp_color_current;
+ g = greenpart gp_color_current;
+ b = bluepart gp_color_current;
+
+ rawtextext("\colored[r=" & decimal(r) & ",g=" & decimal(g) & ",b=" & decimal(b) & "]{" & str & "}")
+ enddef;
+
+ % Returns the (image of) text: vertically centered and horizontally aligned
+ % according to the parameter align, which can be one of:
+ % - gp_align_center
+ % - gp_align_left
+ % - gp_align_right
+ %
+ % The code used to support string as input parameter (for textext("...")),
+ % but this only complicated the thing and is of no real use any more.
+ vardef gp_the_text(expr align, pict) =
+ save shift_x, shift_y;
+
+ shift_y = -ypart center pict;
+ shift_x = -xpart
+ % horizontal alignment
+ if align = gp_align_center : center
+ elseif align = gp_align_left : llcorner
+ elseif align = gp_align_right : lrcorner fi
+ pict;
+
+ (pict shifted (shift_x, shift_y))
+ enddef;
+
+ % % obsolete code to be removed
+ %
+ % % horizontal alignment
+ % if align = gp_align_center :
+ % pict := pict shifted (xpart(.5[llcorner pict,lrcorner pict]),0);
+ % elseif align = gp_align_left :
+ % pict := pict shifted (- xpart(llcorner pict),0);
+ % elseif align = gp_align_right :
+ % pict := pict shifted (- xpart(lrcorner pict),0);
+ % fi;
+ % % Hans wanted to persuade me to make the code shorter
+ % % (zero is replicated three times), but I prefer clarity against sparing
+ % % two bytes
+ %
+ % pict
+
+
+ % Places text on position "pos":
+ % - vertically centered
+ % - horizontal alignment is determined from optional arguments - centered by default
+ % - angle of rotation from optional parameters
+ % - magnification is determined from "gp_scale_text"
+ %
+ % syntax:
+ % gp_put_text((1cm,2cm),\sometxt[gp]{without optional parameters})
+ % gp_put_text(origin, align_left, angle(15), \sometxt[gp]{with an arbitrary number of optional parameters})
+ %
+ % The default alignment is centered, the default angle of rotation is zero.
+ % \sometxt{} or (theoretically) any picture argument should come last
+ def gp_put_text(expr pos)(text t) =
+ save arg_angle; arg_angle=0;
+ save arg_align; arg_align=gp_align_center;
+
+ save pict; picture pict;
+
+ begingroup;
+ % save the original meaning of textext
+ save textext;
+
+ save left, right, center;
+ left = gp_align_left;
+ right = gp_align_right;
+ center = gp_align_center;
+
+ % dirty trick: let makes the comma behave as semicolon,
+ % allowing comma-separated functions in "text t"
+ let@=,;
+
+ def angle(expr a) =
+ let,=@; let@=,;
+ arg_angle := a;
+ let,=;;
+ enddef;
+
+ % def align_left =
+ % let,=@; let@=,;
+ % arg_align := gp_align_left;
+ % let,=;;
+ % enddef;
+ % def align_right =
+ % let,=@; let@=,;
+ % arg_align := gp_align_right;
+ % let,=;;
+ % enddef;
+ % def align_center =
+ % let,=@; let@=,;
+ % arg_align := gp_align_center;
+ % let,=;;
+ % enddef;
+
+ def align(expr a) =
+ let,=@; let@=,;
+ arg_align := a;
+ let,=;;
+ enddef;
+
+ % this is only for mkiv
+ % in mkii, \sometxt behaves as an image and has no problems
+ % while in mkiv it falls back to textext.drt,
+ % and that one has problems if comma gets redefined,
+ % so we need to change definition of comma back to its original value first
+ %
+ % this trick also comes in handy to handle colors & fonts properly
+ vardef textext@#(expr txt) =
+ let,=@;
+
+ gp_colored_mkiv_text(txt)
+ enddef ;
+
+ % converts the whole argument into a picture
+ %
+ % The problem is that \sometxt[gp]{text} is the very last command and cannot be captured as an image otherwise
+ % if you know a cleaner solution, please let me know
+ vardef dummy(text tt) =
+ tt
+ enddef;
+ pict := dummy(t);
+
+ let,=@;
+ endgroup;
+
+ draw (gp_the_text(arg_align, pict) scaled gp_scale_text rotated arg_angle shifted pos) withcolor gp_color_current;
+ enddef;
+
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %%
+ %% drawing and filling
+ %%
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %
+ % low-level interface may change !!!
+ picture gp_pattern_image[], gp_pattern_current, gp_pattern_type[];
+ numeric gp_num_patterns; gp_num_patterns := 11;
+ % TODO: redraw the patterns if necessary (if linewidth changes for example)
+ numeric gp_pattern_linewidth; gp_pattern_linewidth := .5gp_scale_linewidth*gp_internal_linewidth;
+
+ numeric gp_pattern_name_empty; gp_pattern_name_empty := 0;
+ numeric gp_pattern_name_full ; gp_pattern_name_full := 3;
+
+ % default patterns (PostScript-like)
+ %
+ % 0: empty
+ gp_pattern_image[0] := image(
+ setbounds currentpicture to unitsquare;
+ );
+ % 3: full
+ gp_pattern_image[3] := image(
+ % fill unitsquare enlarged .1;
+ % setbounds currentpicture to unitsquare;
+ fill fullsquare xyscaled (5cm,5cm);
+% fill fullsquare xyscaled (2.5w,2.5h);
+ );
+ % 4: \
+ gp_pattern_image[4] := image( % TODO!!!
+ draw ((1,-1)--(-1,1)) scaled 2mm withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to unitsquare scaled 2mm;
+ );
+ % 5: /
+ gp_pattern_image[5] := image( % TODO!!!
+ draw ((-1,-1)--(1,1)) scaled 2mm withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to unitsquare scaled 2mm;
+ );
+ % 6: \
+ gp_pattern_image[6] := image( % TODO!!!
+ draw ((1,-2)--(-1,2)) scaled 2mm withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to unitsquare scaled 2mm;
+ );
+ % 7: /
+ gp_pattern_image[7] := image( % TODO!!!
+ draw ((-1,-2)--(1,2)) scaled 2mm withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to unitsquare scaled 2mm;
+ );
+ % 1: 4+5
+ gp_pattern_image[1] := image(
+ draw gp_pattern_image[4];
+ draw gp_pattern_image[5];
+ );
+ % 2: 4+5, bolj tesno skupaj
+ gp_pattern_image[2] := image(
+ draw gp_pattern_image[1];
+ draw gp_pattern_image[1] shifted (.5bbwidth(gp_pattern_image[1]),0);
+ );
+
+ % argument: 2 times radius
+ vardef gp_pattern_image_circles(expr d) = image (
+ draw fullcircle scaled d withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to fullsquare scaled d;
+ ) enddef;
+
+ vardef gp_pattern_image_roof(expr d) = image (
+ save p; path p; p = fullcircle cutbefore (point 4 of fullcircle);
+ draw p scaled d withpen pencircle scaled gp_pattern_linewidth;
+ draw p shifted (.5,.5) scaled d withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to unitsquare shifted (0,-.5) scaled d;
+ ) enddef;
+
+ vardef gp_pattern_image_brick(expr width, height) = image (
+% save p; path p; p = unitsquare shifted (-.25,-.5) scaled;
+ draw unitsquare shifted (-.25,-.5) xyscaled (width,height) withpen pencircle scaled gp_pattern_linewidth;
+ draw unitsquare shifted ( .25, .5) xyscaled (width,height) withpen pencircle scaled gp_pattern_linewidth;
+ setbounds currentpicture to unitsquare yscaled 2 shifted (-.25,-.5) xyscaled (width,height);
+ ) enddef;
+
+ for i=0 upto 7:
+ gp_pattern_type[i] := gp_pattern_image[i];
+ endfor;
+
+ gp_pattern_type[8] := gp_pattern_image_circles(2mm);
+ gp_pattern_type[9] := gp_pattern_image_roof(1.5mm);
+ gp_pattern_type[10] := gp_pattern_image_brick(2mm,1mm);
+
+ % TODO: filling with a pattern: not implemented yet (implement approximately 8 patterns)
+ def gp_fill_pattern(expr p, pattern) =
+ save arg_pattern; arg_pattern = pattern mod gp_num_patterns;
+
+ if arg_is_transparent = false:
+ fill p withcolor gp_color_background;
+ fi;
+ if pattern = gp_pattern_name_full:
+ % fill the area
+ fill p withcolor gp_color_current;
+ elseif pattern = gp_pattern_name_empty :
+ % nothing to do
+ else:
+ gp_pattern_current := gp_pattern_type[arg_pattern];
+ % TODO: check if it exists!
+
+ save maxi; maxi = max(10,bbwidth (p)/bbwidth (gp_pattern_current)+1);
+ save maxj; maxj = max(10,bbheight(p)/bbheight(gp_pattern_current)+1);
+ draw image (
+ for i=-1 upto maxi:
+ for j=0 upto maxj:
+ draw gp_pattern_current shifted (i*bbwidth(gp_pattern_current),j*bbheight(gp_pattern_current)) shifted llcorner p withcolor gp_color_current;
+ endfor;
+ endfor;
+ clip currentpicture to p;
+ );
+ fi;
+ enddef;
+
+ % fill a closed path with current color with density "density"
+ % (0: background color, 1: current color)
+ %
+ % TODO (once in the future): fill with smooth shading & pattern fill
+ %
+ % p - path to be filled
+ % density - density of the color (between 0 and 1)
+ def gp_fill_density(expr p, density) =
+ fill (p) withcolor density[gp_color_background,gp_color_current];
+ enddef;
+ def gp_fill_transparent(expr p, density) =
+ fill (p) withcolor transparent(1,density,gp_color_current);
+ enddef;
+ % filling (default: with density 1)
+ % optional parameters:
+ % transparent | (present or not, may be combined with both pattern and the solid fill)
+ % density(float between [0,1])
+ % pattern(number of pattern)
+ %
+ % If pattern is called at least once, [transparent] pattern will be drawn,
+ % otherwise just a [transparent] solid area.
+ def gp_fill(text t) =
+ % mandatory parameter: path to be stroked
+ save arg_path; path arg_path;
+ save arg_pattern;
+ % default density is 100 %
+ save arg_density; arg_density := 1;
+ % for transparent density fills or transparent patterns
+ % (but transparent patterns are not implemented yet)
+ save arg_is_transparent; boolean arg_is_transparent; arg_is_transparent := false;
+
+ % since we redefine "transparent", we need to restore the original macro again
+ % before calling gp_fill_transparent, which uses that one
+ begingroup;
+ save transparent, density, pattern;
+
+ let@=,;
+
+ % this function can be called for transparent (solid) fills or patterns
+ def transparent =
+ let,=@; let@=,;
+ arg_is_transparent := true;
+ let,=;;
+ enddef;
+
+ def density(expr dens) =
+ let,=@; let@=,;
+ arg_density := dens;
+ let,=;;
+ enddef;
+
+ def pattern(expr pat) =
+ let,=@; let@=,;
+ arg_pattern := pat;
+ let,=;;
+ enddef;
+
+ % execute the content ("read function arguments")
+ let,=;;
+ % this will read the first mandatory argument (path) into arg_p and execute any subsequent functions
+ arg_path := t;
+ let,=@;
+ endgroup;
+
+ % pattern fill
+ if known arg_pattern:
+ gp_fill_pattern(p, arg_pattern);
+ % transparent solid fill
+ elseif arg_is_transparent:
+ gp_fill_transparent(p, arg_density);
+ % non-transparent solid fill
+ else:
+ gp_fill_density(p,arg_density);
+ fi;
+ enddef;
+
+ def gp_draw(expr p) =
+ draw (p) withcolor gp_color_current withpen pencircle scaled gp_linewidth
+ % axes should be drawn with only one half of the default linewidth
+ % TODO: do we need this to be configurable?
+ if gp_linetype = -1: scaled .5 fi
+ if gp_use_dashed or (gp_linetype = -1): dashed gp_dash_linetype(gp_linetype) fi;
+ enddef;
+
+ def gp_dot(expr x,y) =
+ drawdot shifted (x,y) withcolor gp_color_current withpen pencircle scaled gp_linewidth;
+ enddef;
+
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ %%
+ %% Poins & Symbols
+ %%
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ % should we draw the points using metapost or typeset them using TeX?
+ newinternal gp_points_with_metapost, gp_points_with_tex;
+ gp_points_with_metapost := 0;
+ gp_points_with_tex := 1;
+
+ numeric gp_points_with;
+ % default is to draw the points with metapost rather than typeset them
+ % with TeX
+ gp_points_with := gp_points_with_metapost;
+
+ % the number of point types
+ numeric gp_num_points_with_tex, gp_num_points_with_metapost;
+
+ % returns a path representing a closed n-polygon, starting at upper corner (12 o'clock)
+ vardef fullpolygon(expr n) =
+ (for i=1 upto n: (up rotated ((i-1)*360/n)) -- endfor cycle)
+ enddef;
+ % this is used in drawing and filling rectangles (not for points)
+ % arguments are lower left and size (size is usually smaller that coordinates - spares some bytes)
+ vardef gp_rect(expr ll_corner, size) =
+ (ll_corner -- (xpart ll_corner + xpart size,ypart ll_corner) -- (ll_corner shifted size) -- (xpart ll_corner,ypart ll_corner + ypart size) -- cycle)
+ enddef;
+
+ %-------------------------%
+ % point shape definitions %
+ %-------------------------%
+ path gp_point_shape[];
+
+ % 0 - plus
+ gp_point_shape[0] := (0,0)--(-1,0)--(1,0)--(0,0)--(0,1)--(0,-1)--cycle;
+ % 1 - X
+ gp_point_shape[1] := (0,0)--(-1,-1)--(1,1)--(0,0)--(-1,1)--(1,-1)--cycle;
+ % 2 - star (+ & x)
+ gp_point_shape[2] :=
+ (0,0)--(-1, 0)--(1, 0)--
+ (0,0)--(-1,-1)--(1, 1)--
+ (0,0)--(-1, 1)--(1,-1)--
+ (0,0)--( 0,-1)--(0, 1)--cycle;
+ % 3, 4 - box
+ gp_point_shape[3] := (-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle;
+ gp_point_shape[4] := gp_point_shape[3];
+ % 5, 6 - circle
+ gp_point_shape[5] := fullcircle scaled 1.5;
+ gp_point_shape[6] := gp_point_shape[5];
+ % 7, 8 - triangle
+ % scaled is such a way that it's exactly 2 units wide
+ gp_point_shape[7] := fullpolygon(3) scaled (1/cosd(30));
+ gp_point_shape[8] := gp_point_shape[7];
+ % 9, 10 - upside down triangle
+ gp_point_shape[9] := gp_point_shape[8] scaled -1;
+ gp_point_shape[10] := gp_point_shape[9];
+ % 11, 12 - diamond
+ gp_point_shape[11] := (-1,0)--(0,-1)--(1,0)--(0,1)--cycle;
+ gp_point_shape[12] := gp_point_shape[11];
+ % 13, 14 - pentagon
+ gp_point_shape[13] := fullpolygon(5) scaled (1/cosd(18));
+ gp_point_shape[14] := gp_point_shape[13];
+
+ picture gp_point_pt[];
+
+ % context.trm defines CONTEXT_HTIC = 3.5bp, so points have to match that size
+ % all the shapes above are two units big, so we need to scale with 0.5*3.5bp
+ %
+ % TODO: I should introduce a new variable instead of hardcoding the value
+ % TODO: when points are scaled, line width should not scale along
+ % TODO: when line widths are scaled, line widths of points should scale as well
+ for i=0,1,2,3,5,7,9,11,13:
+ gp_point_pt[i] := image(draw gp_point_shape[i]
+ scaled 1.75bp withpen pencircle scaled gp_linewidth;);
+ endfor;
+ for i=4,6,8,10,12,14:
+ gp_point_pt[i] := image(filldraw gp_point_shape[i]
+ scaled 1.75bp withpen pencircle scaled gp_linewidth;);
+ endfor;
+
+ % we have defined 15 point types so far
+ gp_num_points_with_metapost := 15;
+
+ %-------------------------%
+
+ % n is the number of symbol, but might a be a figure as well
+ vardef gp_the_symbol(expr n) =
+ picture pict;
+ % using metapost
+ if numeric n :
+ if n = -1 :
+ % point type -1 is always a dot
+ pict := image( fill fullcircle scaled 2gp_linewidth; );
+ else :
+ if gp_points_with = gp_points_with_metapost :
+ % TODO
+ pict := gp_point_pt[(n mod gp_num_points_with_metapost)];
+ else :
+ % MK IV; TODO: not sure if relying on context_mlib is safe
+ if known context_mlib :
+ pict := gp_colored_mkiv_text("\convertnumber{\currentGNUPLOTpointset}{" & decimal(n mod gp_num_points_with_tex + 1) & "}");
+ % MK II
+ else :
+ pict := sometxt((n mod gp_num_points_with_tex) + 1 + 500);
+ % XXX: 500 is a hack (see m/t-gnuplot.tex) and might need a fix
+ fi;
+ fi;
+ fi;
+ elseif picture n:
+ pict := n;
+ else:
+ pict := nullpicture;
+ fi
+ pict shifted -center pict
+ enddef ;
+ % the points have to be scaled slightly more
+ def gp_point(expr x, y, n) =
+ draw (gp_the_symbol(n) scaled (gp_scale_pointsize) shifted (x,y)) withcolor gp_color_current;
+ enddef ;
+
+% TODO: this needs some more testing and synchronization with gnuplot source code first
+%
+% def gp_image_rgb(expr size, ll, ur) =
+% draw bitmapimage (xpart size,ypart size,img) xyscaled (ur shifted -ll) shifted ll;
+% enddef ;
+% def gp_image_rgb_alpha(expr size, ll, ur) =
+% draw bitmapimage (xpart size,ypart size,img) withmask mask xyscaled (ur shifted -ll) shifted ll;
+% enddef ;
+
+ endinput
+
+end.
+
+
+% How to trick metapost to use comma between macros
+%
+%beginfig(1);
+%
+%def do_something(expr xx, yy)(text t) =
+% save c; color c; c = black;
+% save d; numeric d; d = 5pt;
+%
+% let@=,;
+%
+% def set_color(expr col) =
+% let,=@;
+% let@=,;
+% c := col;
+% let,=;;
+% enddef;
+%
+% def set_size(expr size) =
+% let,=@;
+% let@=,;
+% d := size;
+% let,=;;
+% enddef;
+%
+% t;
+% let,=@;
+%
+% fill fullcircle scaled d shifted (xx,yy) withcolor c;
+%enddef;
+%
+%do_something(1cm,2cm,set_color(blue));
+%do_something(2cm,1cm,set_size(10pt));
+%% this works OK
+%%do_something(2.5cm,1.5cm,set_color(red) set_size(8pt));
+%
+%% this works OK
+%%do_something(2cm,2.5cm,set_color(green);set_size(8pt));
+%
+%% but the following syntax looks better
+%do_something(2.5cm,1.5cm,set_color(blue),set_size(8pt),set_color(red),set_size(2pt));
+%
+%draw unitsquare scaled 3cm;
+%
+%endfig;
+%end.
diff --git a/macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkii b/macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkii
new file mode 100644
index 0000000000..cb80557c48
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkii
@@ -0,0 +1,1032 @@
+%D \module
+%D [ file=t-gnuplot.mkii,
+%D version=2013.04.19,
+%D title=\CONTEXT\ Extra Modules,
+%D subtitle=\GNUPLOT\ Inclusion,
+%D author={Mojca Miklavec, Marco Patzer, Hans Hagen, Taco Hoekwater, Aditya Mahajan},
+%D date=\currentdate,
+%D copyright=\PRAGMA]
+
+% begin info
+%
+% title : Gnuplot module
+%
+% comment : simplifies inclusion of gnuplot-generated graphs into ConTeXt documents
+% status : stable, supports both MKII and MKIV
+%
+% end info
+
+%D This module is used for creating gnuplot graphs on-the-fly and including them
+%D into documents.
+%D
+%D Known Bugs:
+%D
+%D \startitemize
+%D \item spurious space \& page
+%D \stopitemize
+%D
+%D TODO (Optimisations):
+%D
+%D \startitemize
+%D \item optimize the number of gnuplot runs (if possible, gnuplot should be run only once)
+%D \item optimize the number of times for loading/converting an already used graphic
+%D \item pstopdf is a bit slow
+%D \stopitemize
+%D
+%D TODO (Handle things that may go wrong):
+%D - gnuplot executable doesn't exist
+%D - context terminal isn't available or some other failure in gnuplot script (no file created)
+%D - write18 disabled (you may call gnuplot later - create a script)
+%D
+%D TODO (Missing functionality):
+%D
+%D - a lot ...
+
+\writestatus{loading}{Gnuplot module}
+
+\startmodule[gnuplot]
+
+\unprotect
+
+\def\c!terminal {terminal}
+\def\c!options {options}
+\def\c!pointset {pointset}
+\def\c!purge {purge}
+
+%D MPextensions
+%D
+%D XXX
+%D if possible, they should be specific to \type{\startGNUPLOTgraphic},
+%D so unvisible to \type{MPcode}
+%D (probably something like \type+\appendtoks ... to\everyGNUPLOTgraphic+)
+\startMPextensions
+ % load metapost macros (only once)
+ input mp-gnuplot.mp ;
+ % number of points defined with \setupGNUPLOTterminal[pointset=...]
+ %gp_num_points_with_tex := \gp:num:pointswithtex;
+ gp_num_points_with_tex := 3;
+ % main color should be set equal to the current text color
+ gp_color_foreground := \MPcolor{currentcolor};
+ gp_color_lt[-2] := gp_color_foreground;
+ % TODO: is there any chance to make this local to gnuplot?
+ % linejoin & linecap
+ linejoin := \@@GNUPLOT@term@context@linejoin;
+% linecap := \gp:term:context:linecap;
+ % dashes or solid? (true/false)
+ gp_use_dashed := \@@GNUPLOT@term@context@is@dashed;
+ % dashlength scale
+% gp_scale_dashlength := \gp:term:context:dashlength;
+ % linewidth scale
+ gp_scale_linewidth := \@@GNUPLOT@term@context@linewidth;
+
+ gp_scale_text := \@@GNUPLOT@term@context@fontscale;
+
+ % linejoin := \gp:term:context:linejoin;
+ % linecap := \gp:term:context:linecap;
+ % % dashes or solid? (true/false)
+ % gp_use_dashed := \gp:term:context:dashed;
+ % % dashlength scale
+ % gp_scale_dashlength := \gp:term:context:dashlength;
+ % % linewidth scale
+ % gp_scale_linewidth := \gp:term:context:linewidth;
+
+ gp_points_with := gp_points_with_\@@GNUPLOT@term@context@points;
+
+\stopMPextensions
+
+%D (hopefully) temporary solution for handling the color in expressions like
+%D \type+draw \sometxt{...} withcolor red+
+\chardef\TeXtextcolormode\zerocount
+
+%D We need a \type{\strut} in front of labels for better vertical centering.
+%D This might still fail for Zapfino and alike where \type{\strut} might be smaller
+%D than the actual font height.
+%D
+%D TODO (optional improvement): instead of placing \type{\strut} in front,
+%D create a \type{\hbox} and adjust it's dimensions to \type{\strut}'s height and depth.
+
+%D Aditya, thanks a lot for requesting it!
+%D Hans, thanks a lot for implementing this!
+\definetextext[gp]{\strut}
+%D TODO (feature request):
+%D \starttyping
+%D \sometxt[gp][ss,20pt]{abc}
+%D \stoptyping
+%D should become equivalent to
+%D \starttyping
+%D \sometxt{\switchtobodyfont[ss,20pt]\strut abc}
+%D \stoptyping
+%D
+%D Or, even more drastic perhaps, I would love to implement
+%D \starttyping
+%D \sometxt[gp][iwona,bold,c]{abc}
+%D \stoptyping
+%D meaning: iwona, bold typeface, size \quote{c}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%D The following patch has been written by Aditya and seems to work OK,
+%D however it would be more clean to have this functionality in the core.
+%D Redefining low-level macros might be a bit dangerous, esp. since
+%D they tend to change over time.
+%D
+%D At the moment the hack only works in MKII; for MKIV it needs to be fixed
+%D
+%D TODO: remind Hans to implement it in core ;)
+%D and remove this patch from the module
+%D
+\long\def\redofiltersometxt[#1]%
+{\doifnextcharelse[{\reredofiltersometxt[#1]}{\redodofiltersometxt[#1]}}
+
+\long\def\redodofiltersometxt[#1]#2%
+ {\increment\txtcounter
+ \TeXtext[#1]\txtcounter{#2}%
+ \filtersometxt}
+
+\long\def\reredofiltersometxt[#1][#2]#3%
+ {\increment\txtcounter
+ \TeXtext[#1]\txtcounter{\switchtobodyfont[#2]\strut#3}%
+ \filtersometxt}
+
+%D In MKII we have to make sure that runtime MP graphics are enabled,
+%D otherwise sizes of labels created by \type+\sometxt{}+ would be wrong
+%D if user didn't enable that option in \type{cont-usr.tex}.
+%D (Taco says it might be a bug, but let's not worry too much about it.
+%D If it will be resolved, we may delete this.)
+\runMPgraphicstrue
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\newcounter\GNUPLOTnumber
+\newdimen\GNUPLOThcharsize
+\newdimen\GNUPLOTvcharsize
+\newcounter\tikzGNUPLOTnumber
+
+% macro to calculate true character size from current font
+% (However it is not 100 % that it gets the job properly done
+% in case that other font settings are used inside the picture.)
+\def\calculateGNUPLOTcharsize{%
+ \global\GNUPLOThcharsize=1.05\fontcharwd\font`0%
+ \global\GNUPLOTvcharsize=1.05\fontcharht\font`0%
+ \global\advance\GNUPLOTvcharsize by 1.05\fontchardp\font`g%
+}
+
+%D \macros
+%D {startGNUPLOTinclusions, resetGNUPLOTinclusions}
+%D
+%D For those who want to have two or more graphs with similar options,
+%D these options may be included inside \type{\startMPinclusions ... \stopMPinclusions}
+%D and will be place on the top of the created \GNUPLOT\ script.
+%D
+%D \starttyping
+%D \startGNUPLOTinclusions
+%D set xlabel '$x$'
+%D set ylabel '$y$'
+%D set format y "%.1f"
+%D \stopGNUPLOTinclusions
+%D
+%D \startGNUPLOTscript[sin]
+%D plot sin(x)
+%D \stopGNUPLOTscript
+%D \startGNUPLOTscript[cos]
+%D plot cos(x)
+%D \stopGNUPLOTscript
+%D \stoptyping
+\long\def\startGNUPLOTinclusions
+ {\def\stopGNUPLOTinclusions{\ifx\savebuffer\undefined \else \savebuffer[gnuplot-inclusions]\fi}%
+ \dostartbuffer[gnuplot-inclusions][startGNUPLOTinclusions][stopGNUPLOTinclusions]}
+
+% \def\resetGNUPLOTinclusions{\let\GNUPLOTinclusions\empty}
+
+% creates an empty file (there must be a cleaner way to do it)
+\def\resetGNUPLOTinclusions
+ {\immediate\openout\scratchwrite=\jobname-gnuplot-inclusions.tmp
+ \immediate\closeout\scratchwrite}
+
+%\startbuffer[gnuplot-inclusions]\stopbuffer\ifx\savebuffer\undefined \else \savebuffer[gnuplot-inclusions]\fi}
+
+\resetGNUPLOTinclusions
+
+%D On the other hand, one can probably achieve the same effect
+%D when drawing two plots inside the same script, like that:
+%D
+%D \starttyping
+%D \startGNUPLOTscript[sin and cos]
+%D set xlabel '$x$'
+%D set ylabel '$y$'
+%D set format y "%.1f"
+%D plot sin(x)
+%D plot cos(x)
+%D \stopGNUPLOTscript
+%D \stoptyping
+%D
+%D and then recall the graphics using \type{\useGNUPLOTgraphic[sin and cos][1]}.
+
+%D \macros
+%D {startGNUPLOTscript}
+%D
+%D \starttyping
+%D \startGNUPLOTscript{some name}
+%D plot sin(x)
+%D \stopGNUPLOTscript
+%D \stoptyping
+
+\def\startGNUPLOTscript
+ {\bgroup\dosingleempty\dostartGNUPLOTscript}
+
+% \def\redostartGNUPLOTscript#1
+% {\obeylines
+% \catcode`\%=\@@letter
+% \catcode`\|=\@@letter
+% \catcode`\$=\@@letter
+% \dodostartGNUPLOTscript{#1}%
+% %\dostartGNUPLOTscript[#2]
+% }
+%
+% \def\dostartGNUPLOTscript[#1]%
+% {\iffirstargument
+% \obeylines
+% \catcode`\%=\@@letter
+% \catcode`\|=\@@letter
+% \catcode`\$=\@@letter
+% \dodostartGNUPLOTscript{#1}%
+% \else
+% \redostartGNUPLOTscript
+% \fi
+% }
+
+\def\dostartGNUPLOTscript[#1]%
+ {%\iffirstargument
+ \dodostartGNUPLOTscript{#1}%
+ %\else
+ % \dodostartGNUPLOTscript
+ %\fi
+}
+
+% gps:n:{name} = gnuplotscript : number : {name} - number of script with name {name}
+\long\def\dodostartGNUPLOTscript#1%#2\stopGNUPLOTscript
+ {\doglobal\increment\GNUPLOTnumber
+ \letgvalue{gps:n:#1}\GNUPLOTnumber
+ % in case of LuaTeX we need to write the buffer into file explicitely
+ \def\stopGNUPLOTscript{\egroup \ifx\savebuffer\undefined \else \savebuffer[gnuplot-\GNUPLOTnumber]\fi}%
+ \dostartbuffer[gnuplot-\GNUPLOTnumber][startGNUPLOTscript][stopGNUPLOTscript]%
+ }
+
+% When are the graphics processed/read?
+%
+% - \gps:n:{name} (gnuplot script:name:{name}) holds the number of gnuplot script;
+% - that number was defined if we created the plot using \startGNUPLOTscript{name}
+% - and most probably undefined if we only issued \processGNUPLOTfile[name][filename]
+% => if, at the time of issuing \processGNUPLOTfile[name][filename], \gps:n:{name} is not defined,
+% it should be defined at that time
+% - \gpe:{name}:{terminal} (gnuplot graphic executed:{name}:{terminal})
+% is defined if we executed the command for that specific name and for that terminal
+% (once it will probably be 0 for a failed run and 1 for a successful one)
+
+% \useGNUPLOTgraphic[name] has three different ways of working:
+% - it can be called after \processGNUPLOTfile[name][filename]
+% which defined MP graphics that are now used
+% - it can be called for the first time under the current terminal
+% in that case it compiles the graphic and includes it
+% - it can be called for the second, third, ... time
+% in which case it only includes files without compiling it
+
+%D \macros
+%D {useGNUPLOTgraphic}
+
+% 5 ways of calling it:
+% - \useGNUPLOTgraphic{name}
+% - \useGNUPLOTgraphic[name]
+% - \useGNUPLOTgraphic[name][1,2,5]
+% - \useGNUPLOTgraphic[name][width=.8\textwidth]
+% - \useGNUPLOTgraphic[name][1,2,5][width=.8\textwidth]
+
+% this code takes care of reading arguments
+\def\useGNUPLOTgraphic
+ {\dotripleempty\douseGNUPLOTgraphic}
+
+\def\douseGNUPLOTgraphic[#1][#2][#3]%
+ {\doifelse{#3}{}{%
+ % < 3 arguments
+ \doifelse{#2}{}%
+ % 1 argument
+ % as in \useGNUPLOTgraphic[name]
+ {\dodouseGNUPLOTgraphic[#1][][]}%
+ % % as in \useGNUPLOTgraphic{name}
+ % {\redouseGNUPLOTgraphic[][][]}%
+ % 2 arguments
+ {\doifassignmentelse{#2}%
+ % as in \useGNUPLOTgraphic[name][width=.8\textwidth]
+ {\dodouseGNUPLOTgraphic[#1][][#2]}%
+ % as in \useGNUPLOTgraphic[name][1,2,5]
+ {\dodouseGNUPLOTgraphic[#1][#2][]}%
+ }%
+ }%
+ % as in \useGNUPLOTgraphic[name][1,2,5][width=.8\textwidth]
+ {\dodouseGNUPLOTgraphic[#1][#2][#3]}%
+ }
+
+\def\redouseGNUPLOTgraphic[#1][#2][#3]#4%
+ {\dodouseGNUPLOTgraphic[#4][][]}
+
+% and this code actually does something with it
+
+%D \doifundefined {string} {...}
+%D \doifdefined {string} {...}
+%D \doifundefinedelse {string} {then ...} {else ...}
+%D \doifdefinedelse {string} {then ...} {else ...}
+
+% \doifGNUPLOTscriptdefined{name}{...}
+
+% if gnuplot script with {name} and current terminal has already been processed
+% \doifGNUPLOTscriptprocessed{name}{...}
+
+% \letGNUPLOTscriptprocessed[optional terminal]{name} signals that the gnuplot script named {name}
+% has already been processed with the current terminal
+% TODO: currently it is always defined to be one as soon as one tries to process it,
+% even if en error is produced; in future it would be helpful if it would be set to zero
+% if it was unsuccessfully executed;
+% that is needed, since scripts are processed only when one first asks for including the graphic
+\def\letGNUPLOTscriptprocessed
+ {\dosingleempty\doletGNUPLOTscriptprocessed}
+%\def\doletGNUPLOTscriptprocessed[#1]#2%
+%{\letgvalue{gpe:#1:\@@GNUPLOTterminal}\plusone}
+\def\doletGNUPLOTscriptprocessed[#1]#2%
+ {\iffirstargument
+ % terminal has been specified
+ \letgvalue{gpe:#2:#1}\plusone
+ \else
+ % no terminal specified - use the current one
+ \letgvalue{gpe:#2:\@@GNUPLOTterminal}\plusone
+ \fi}
+
+% Although that should preferably not happen, one might come to an idea of defining
+% a gnuplot script with the same name as already defined.
+% In that case the old script cannot be referenced any more, but we can still try
+% to do out best to make it work anyway. We have to do two things:
+% - claim that script with that name hasn't been defined yet, so that processing will happen again
+% (TODO: do it in a more elegant way for all known terminals)
+% - undefine any metapost graphics (TODO: I have no idea yet how it can be done)
+%
+% \resetGNUPLOTscriptprocessed{name}
+\def\resetGNUPLOTscriptprocessed#1%
+ {\bgroup
+ % claim that the script with {name} (#1) hasn't been processed with {terminal} (##1) yet
+ \def\undefineGNUPLOTscriptforterminal##1{\letbeundefined{gpe:#1:##1}}%
+ % TODO: the list of available terminals should be generated automatically
+ \processcommalist[context,postscript,ps,eps,pdf,metapost,mp,png]\undefineGNUPLOTscriptforterminal
+ \resetGNUPLOTgraphics{#1}%
+ \egroup}
+
+% \resetGNUPLOTgraphics{name} undefines any gnuplot graphic defined with \startGNUPLOTgraphic[name][number]
+\def\resetGNUPLOTgraphics#1%
+ {\doloop
+ {\doifMPgraphicelse{gpg:#1:\recurselevel}%
+ % TODO: be aware - @@MPG is low-level ConTeXt variable which might change without notice,
+ % it would be better to call this \undefineMPgraphic{gpg:#1:\recurselevel} or something similar
+ {\letbeundefined{@@MPGgpg:#1:\recurselevel}}%
+ {\exitloop}}%
+ \doloop
+ {\doifdefinedelse{TKZp:#1:\recurselevel}%
+ {\letbeundefined{TKZp:#1:\recurselevel}}%
+ {\exitloop}}%
+ }%
+
+% TODO: this can probably be done in a better way
+\def\doifGNUPLOTscriptprocessed#1#2%
+ {\doifdefined {gpe:#1:\@@GNUPLOTterminal}{#2}}
+\def\doifGNUPLOTscriptprocessedelse#1#2#3%
+ {\doifdefinedelse {gpe:#1:\@@GNUPLOTterminal}{#2}{#3}}
+\def\doifGNUPLOTscriptnotprocessed#1#2%
+ {\doifundefined {gpe:#1:\@@GNUPLOTterminal}{#2}}
+\def\doifGNUPLOTscriptnotprocessedelse#1#2#3%
+ {\doifundefinedelse{gpe:#1:\@@GNUPLOTterminal}{#2}{#3}}
+
+
+% private
+% \writeandprocessGNUPLOTscript{name}
+\def\writeandprocessGNUPLOTscript#1%
+ % TODO: if gps:n:#1 (holding the script content) is not defined,
+ % error or warning should be issued
+ %
+ % only process the script if it has been defined and not processed before for the current terminal
+ {\doifdefined{gps:n:#1}{\doifGNUPLOTscriptnotprocessed{#1}{%
+ % \gpe:{name}:{terminal} is defined
+ %\setgvalue{gpe:#1:\@@GNUPLOTterminal}{#2}
+ \letGNUPLOTscriptprocessed{#1}%
+ % call to gnuplot and processing/converting the graphics is only needed in the first ConTeXt run
+ \doifmode{*\v!first}{% (perhaps also: if files haven't changed)
+ \bgroup
+ % for TikZ we want to calculate character size, so that it gets printed
+ % to inclusions with \@@GNUPLOToptions
+ \calculateGNUPLOTcharsize
+ % catcodes trickery & alike
+ %\the\everyGNUPLOTscript
+ % open file for writing
+ \immediate\openout\scratchwrite=\GNUPLOTfile.plt
+ % TODO: terminal-specific options (default or provided by the user)
+ \immediate\write\scratchwrite{\letterhash\space Do not modify this file - all changes will be overwritten}%
+ \immediate\write\scratchwrite{\letterhash\space Change \jobname.tex instead.}%
+ \immediate\write\scratchwrite{set terminal \@@GNUPLOToutput\space\@@GNUPLOToptions}%
+ % add common inclusions in scripts for multiple similar plots
+ %\doifnotempty
+ % {\GNUPLOTinclusions}{\immediate\write\scratchwrite{\GNUPLOTinclusions}}%
+ % include common gnuplot 'inclusions'
+ \immediate\write\scratchwrite{load '\jobname-gnuplot-inclusions.tmp'}%
+ % output file
+ \immediate\write\scratchwrite{set output "\@@GNUPLOTresult"}%
+ % write main contents of the script, like "plot sin(x)"
+ %\immediate\write\scratchwrite{\getvalue{gps:d:\GNUPLOTnumber}}%
+ \immediate\write\scratchwrite{load '\GNUPLOTfile.tmp'}%
+ \immediate\closeout\scratchwrite
+ \egroup
+ % TODO:
+ % - check the state of write18 and warn the user if it's disabled,
+ % otherwise just everyone will start complaining that the module doens't work
+ % - check if execution was successful; possible pitfalls:
+ % - gnuplot doesn't exist as a binary
+ % - gnuplot doesn't support context terminal
+ % or some other error in script which results in empty output file
+ %
+ % run gnuplot & execute the script that has just been written
+ % \executesystemcommand{mtxrun --ifchanged=\GNUPLOTfile.plt\space
+ % --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
+ \executesystemcommand{mtxrun --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
+ \writestatus{aaa}{mtxrun --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
+ \convertGNUPLOTgraphic
+ }%
+ % for ConTeXt terminal only - read the result
+ \doif{\@@GNUPLOTterminal}{context}{\processGNUPLOTfile[#1][\@@GNUPLOTresult]}%
+ \doif{\@@GNUPLOTterminal}{tikz} {\processGNUPLOTfile[#1][\@@GNUPLOTresult]}%
+ }}}
+
+% \dodouseGNUPLOTgraphic[name][numbers][options]
+\def\dodouseGNUPLOTgraphic[#1][#2][#3]%
+ {\bgroup
+ \doifdefinedelse{gps:n:#1}
+ {\edef\GNUPLOTnumber{\getvalue{gps:n:#1}}%
+ \edef\GNUPLOTfile {\jobname-gnuplot-\GNUPLOTnumber}%
+ \writeandprocessGNUPLOTscript{#1}%
+ % "ctxtools --purge" should delete the gnuplot script and other intermediate files
+ % (but it seems that they are deleted automatically already; preferred or not?)
+% \registertempfile{\GNUPLOTfile.plt}
+% \registertempfile{\@@GNUPLOTresult}
+% \registertempfile{\@@GNUPLOTfinalresult}
+ \doifelse{\@@GNUPLOTterminal}{context}%
+ % for ConTeXt terminal only
+ {\doifelse{#2}{}%
+ % if no explicit figure number was specified, include all the figures
+ {\doloop
+ {\doifMPgraphicelse{gpg:#1:\recurselevel}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:\recurselevel}}}%
+ {\exitloop}}}%
+ % if numbers were specified, include the figures specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\doifMPgraphicelse
+ {gpg:#1:##1}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:##1}}}%
+ % if graphic doesn't exist: draw a dummy frame instead and warn the user
+ {\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }{%
+ % for TikZ terminal only
+ \doifelse{\@@GNUPLOTterminal}{tikz}%
+ {\doifelse{#2}{}%
+ % if no explicit figure number was specified, include all the figures
+ {\doloop
+ {\doifdefinedelse{TKZp:#1:\recurselevel}%
+ {\scale[#3]{\getvalue{TKZp:#1:\recurselevel}}}%
+ {\exitloop}}}%
+ % if numbers were specified, include the figures specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\doifdefinedelse
+ {TKZp:#1:##1}%
+ {\scale[#3]{\getvalue{TKZp:#1:##1}}}%
+ % if graphic doesn't exist: draw a dummy frame instead and warn the user
+ {\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }%
+ % for all the other terminals
+ % TODO: add more safety switches (if pages don't exist for example)
+ {\doifelse{#2}{}%
+ % if no explicite figure number was specified, include all the pages
+ % TODO: properly handle METAPOST & PNG (only works for (E)PS & PDF) !!!
+ {\getfiguredimensions[\@@GNUPLOTfinalresult]%
+ \dorecurse{\noffigurepages}{\externalfigure[\@@GNUPLOTfinalresult][page=\recurselevel,#3]}}%
+ % if numbers were specified, include the pages specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\externalfigure[\@@GNUPLOTfinalresult][page=##1,#3]}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }}%
+ }%
+ % else part - in case that graphic gets included with \processGNUPLOTfile[name][file]
+ {\doifelse{\@@GNUPLOTterminal}{context}
+ % TODO: this part has been literally copied from above; create a subroutine instead
+ {\doifelse{#2}{}%
+ % if no explicit figure number was specified, include all the figures
+ {\doloop
+ {\doifMPgraphicelse{gpg:#1:\recurselevel}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:\recurselevel}}}%
+ {\exitloop}}}%
+ % if numbers were specified, include the figures specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\doifMPgraphicelse
+ {gpg:#1:##1}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:##1}}}%
+ % if graphic doesn't exist: draw a dummy frame instead and warn the user
+ {\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }%
+ {\message{The gnuplot graphic #1 has not been defined.}}}%
+ \egroup}
+
+%D \macros
+%D {setupGNUPLOT}
+
+\def\setupGNUPLOT
+% {\dodoubleempty\getparameters[@@GNUPLOT]}
+ {\dosingleargument\dosetupGNUPLOT}
+
+\def\dosetupGNUPLOT[#1]%
+ {\getparameters[@@GNUPLOT][#1]%
+ % define all the necessary points according to the option "pointset"
+ %
+ % XXX: no idea why this is needed, but otherwise it complains that @@GNUPLOTpointset is undefined
+ \edef\currentGNUPLOTpointset{\@@GNUPLOTpointset}%
+ \startTeXtexts
+ \doloop{\doifelseconversionnumber{\@@GNUPLOTpointset}{\recurselevel}%
+ % +500 is a hack (hopefully the plot doesn't contain more than 500 labels)
+ % otherwise the points would be overwritten by labels with another \TeXtext:
+ % it might need a fix in ConTeXt core
+ {\TeXtext{\numexpr\recurselevel+500\relax}{{\convertnumber{\currentGNUPLOTpointset}{\recurselevel}}}}%
+ {\exitloop}}%
+ \stopTeXtexts
+ % \gp:num:pointswithtex is passed to metapost, so that it knows
+ % how many points are defined and chooses the proper point
+ % form a set of the defined ones
+ %
+ % for safety reasons define \gp:num:pointswithtex to be equal to 1 (it can only increase later), otherwise:
+ % - (something mod 0) won't work
+ % - if conversion is not defined, the number will retain its old value (not desirable)
+ \edef\gp:num:pointswithtex{1}%
+ \doloop{\doifelseconversionnumber{\@@GNUPLOTpointset}{\recurselevel}%
+ {\edef\gp:num:pointswithtex{\recurselevel}}%
+ {\exitloop}}%
+ % in case of TikZ terminal we have to generate the needed style files first
+ \doif{\@@GNUPLOTterminal}{tikz}
+ {\executesystemcommand{gnuplot -e "set term tikz context createstyle"}\usemodule[gnuplot-lua-tikz]}
+ % TODO:
+ % - issue a warning if user wants to use points with TeX,
+ % but pointset= is undefined (if the first point doesn't exist)
+ % - no idea what happens if conversion is infinite,
+ % so try to stop at some reasonable value (100?)
+ % - current implementation redefines the points even if only terminal type
+ % has been set to some other value (which is a stupid approach, but I
+ % wanted to have a working version first and will consider efficiency later)
+ % - it may be that the old points remain defined if conversion changes
+ % (perhaps they should be undefined again?)
+ }
+
+% Hans has written this piece of code, but:
+% - "start" and "/MIN" caused problems
+% - pgnuplot is not much more "portable" than gnuplot
+% best thing to do is to create a "gnuplot.bat" somewhere in PATH
+%
+%\def\processGNUPLOTscript
+% {\doifelse\operatingsystem{mswin}
+% {\executesystemcommand{start /MIN pgnuplot \GNUPLOTfile.plt}} % start is needed else gp fails
+% {\executesystemcommand{gnuplot \GNUPLOTfile.plt}}}
+%\def\processGNUPLOTscript
+% {\executesystemcommand{gnuplot \GNUPLOTfile.plt}}
+
+% TODO: check if write18 is enabled; if not, issue a command and warn the user that running the module might be pointless or that he/she has to run gnuplot on the produced files manually
+\def\convertGNUPLOTgraphic
+ {\doifsomething\@@GNUPLOThandle{\writestatus{conversion}{\@@GNUPLOThandle}\executesystemcommand{\@@GNUPLOThandle}}}
+
+%D \macros
+%D {processGNUPLOTfile}
+
+%D It's needed to input a file resulting from a gnuplot run (with ConTeXt terminal).
+%D It reads the file and "saves" the metapost graphics defined in that file,
+%D so that they can be used with \usegnuplotgraphic (low level: \reuseMPgraphic) later
+%D
+% \processGNUPLOTfile[NAME][filename]
+\def\processGNUPLOTfile
+ {\dodoubleargument\doprocessGNUPLOTfile}
+
+\def\doprocessGNUPLOTfile[#1][#2]%
+ {\bgroup
+ % for ConTeXt teminal
+ %--------------------
+ % we first define two commands: \startGNUPLOTgraphic & \stopGNUPLOTgraphic to read the files in;
+ % files that gnuplot creates in non-standalone mode look approximately like this:
+ %
+ % \startGNUPLOTgraphic[1]
+ % ... metapost commands to draw the graph ...
+ % \stopGNUPLOTgraphic
+ % \startGNUPLOTgraphic[2]
+ % ... metapost commands to draw the graph ...
+ % \stopGNUPLOTgraphic
+ % ...
+ %
+ % while reading the file in, metapost graphics named "gpg:{name}:{number}" are defined
+ %
+ % (we might need some additional arguments later on, but for now the figure number should suffice)
+ \def\startGNUPLOTgraphic
+ {%\obeyMPlines % <- no longer a problem
+ \def\obeyedline{}% <- thanks to this
+ \dosingleargument\dostartGNUPLOTgraphic}%
+ \long\def\dostartGNUPLOTgraphic[##1]##2\stopGNUPLOTgraphic
+ {\startreusableMPgraphic{gpg:#1:##1}##2\stopreusableMPgraphic}%
+ %------------------
+ % for TikZ terminal
+ %------------------
+ \newcounter\tikzGNUPLOTnumber
+ \let\normalstarttikzpicture=\starttikzpicture
+ \let\normalstoptikzpicture=\stoptikzpicture
+ \long\def\starttikzpicture##1\stoptikzpicture%
+ {\increment\tikzGNUPLOTnumber
+ % \long\setgvalue{TKZp:#1:\tikzGNUPLOTnumber}{##1}}
+ \long\setgvalue{TKZp:#1:\tikzGNUPLOTnumber}{\hbox{\starttikzpicture##1\stoptikzpicture}}}
+ %---------
+ % for both
+ %---------
+ % input the file
+ % TODO: error / warning if the files doesn't exist
+ \readlocfile{#2}{}{}% the third argument should be: if file not found
+ % this file is known
+ % TODO \letgvalue{gpf:#1}\plusone
+ \egroup
+ }
+
+%D \macros
+%D {startGNUPLOTpage}
+%D
+%D Used for standalone \GNUPLOT\ figures & written out by gnuplot in standalone mode.
+%D
+%D Instead of having to \type{\useGNUPLOTgraphic}, a figure is inserted directly using
+%D \type{\startMPpage ... }\type{\stopMPpage}.
+%D
+%D A high-level command is provided if some more advance features will
+%D be needed in the future and to be able to ensure backward compatibility.
+
+%D We need to preserve line breaks, otherwise metapost runs into troubles for longer input.
+\def\startGNUPLOTpage
+ {\begingroup\dostartGNUPLOTpage}
+
+\long\def\dostartGNUPLOTpage#1\stopGNUPLOTpage
+ {\endgroup\startMPpage#1\stopMPpage}
+
+% TODO: use a separate instance for MKIV
+%
+% \defineMPinstance[gnuplot]
+% \startTEXpage\startMPcode{gnuplot}
+% \stopMPcode\stopTEXpage
+
+% TODO: currently, the following definitions are used,
+% but the ones below would be preferred
+
+\def\defineGNUPLOThandle#1#2#3#4#5#6% name; output; suffix; conversion-method; gnuplot's result; final result
+ {\setvalue{@@GNUPLOT-#1}{{#2}{#3}{#4}{#5}{#6}}}
+
+\def\@@GNUPLOToutput{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}51}
+\def\@@GNUPLOTsuffix{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}52}
+\def\@@GNUPLOThandle{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}53}
+\def\@@GNUPLOTresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}54}
+\def\@@GNUPLOTfinalresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}55}
+
+% name "set term" suffix conversion (system command) gnuplot's result final result
+% (suffix is probably not needed any more since full names were introduced)
+\defineGNUPLOThandle
+ {postscript}{postscript color}{ps}{mtxrun pstopdf \GNUPLOTfile-ps.ps}
+ {\GNUPLOTfile-ps.ps}{\GNUPLOTfile-ps.pdf}
+\defineGNUPLOThandle
+ {ps}{postscript color}{ps}{mtxrun pstopdf -dAutoRotatePages=/PageByPage \GNUPLOTfile-ps.ps}
+ {\GNUPLOTfile-ps.ps}{\GNUPLOTfile-ps.pdf}
+\defineGNUPLOThandle
+ {eps}{postscript color eps}{ps}{mtxrun pstopdf -dEPSCrop \GNUPLOTfile-eps.eps}
+ {\GNUPLOTfile-eps.eps}{\GNUPLOTfile-eps.pdf}
+\defineGNUPLOThandle
+ {pdf}{pdf}{pdf}{}
+ {\GNUPLOTfile-pdf.pdf}{\GNUPLOTfile-pdf.pdf}
+\defineGNUPLOThandle
+ {pdfcairo}{pdfcairo}{pdf}{}
+ {\GNUPLOTfile-pdfcairo.pdf}{\GNUPLOTfile-pdfcairo.pdf}
+\defineGNUPLOThandle
+ {metapost}{mp}{mp}{mtxrun mptopdf \GNUPLOTfile-mp.mp}
+ {\GNUPLOTfile-mp.mp}{\GNUPLOTfile-mp-0.pdf}
+\defineGNUPLOThandle
+ {mp}{mp}{mp}{mtxrun mptopdf \GNUPLOTfile-mp.mp}
+ {\GNUPLOTfile-mp.mp}{\GNUPLOTfile-mp-0.pdf}
+\defineGNUPLOThandle
+ {png}{png}{png}{}
+ {\GNUPLOTfile-png.png}{\GNUPLOTfile-png.png}
+\defineGNUPLOThandle
+ {pngcairo}{pngcairo}{png}{}
+ {\GNUPLOTfile-pngcairo.png}{\GNUPLOTfile-pngcairo.png}
+\defineGNUPLOThandle
+ {tikz}{tikz context createstyle
+ size \@@GNUPLOT@term@tikz@width,\@@GNUPLOT@term@tikz@height\space
+ charsize \the\GNUPLOThcharsize,\the\GNUPLOTvcharsize\space
+ fontscale \@@GNUPLOT@term@tikz@fontscale
+ }{tex}{}
+ {\GNUPLOTfile-tikz.tex}{\GNUPLOTfile-tikz.tex}
+\defineGNUPLOThandle
+ {context}{context
+ size \@@GNUPLOT@term@context@width,\@@GNUPLOT@term@context@height\space
+ fontscale \@@GNUPLOT@term@context@fontscale
+ }{tex}{}
+ {\GNUPLOTfile-ctx.tex}{\GNUPLOTfile-ctx.tex}
+% \doifnotempty{\@@GNUPLOT@term@context@fontscale}{fontscale \@@GNUPLOT@term@context@fontscale}
+
+\def\defineGNUPLOTterminal
+ {\dodoubleargument\dodefineGNUPLOTterminal}
+
+\def\dodefineGNUPLOTterminal[#1][#2]%
+ {\doifassignmentelse{#2}%
+ % define a proper terminal
+ {}% TODO
+ % only define a synonym for that terminal
+ {}% TODO
+ }
+
+%D {\sl terminal}: argument to be passed to gnuplot in the form of "set term {\sl terminal}"
+%D {\sl defaultoptions}: options to be passed to gnuplot after terminal name:
+%D set term {\sl teminal} {\sl defaultoptions}
+%D not to be touched by users
+%D {\sl suffix}: filename suffix
+%D {\sl result}: the file to which gnuplot should output the result
+%D set output {\sl result}
+%D {\sl convertwith}: command for conversion from gnuplot-generated file to a file that can be read by \CONTEXT; may be empty
+%D {\sl finalresult}: file resulting after the conversion to be read by \CONTEXT
+
+%D \POSTSCRIPT\ terminal
+\defineGNUPLOTterminal
+ [postscript]
+ [terminal=postscript,
+ defaultoptions=color, % TODO: only if \setupcolors[state=start]
+ suffix=ps,
+ result=\GNUPLOTfile-ps.ps,
+ % TODO: -dAutoRotatePages=/PageByPage or remove that option from the default ones
+ convertwith={mtxrun pstopdf \GNUPLOTfile-ps.ps},
+ finalresult=\GNUPLOTfile-ps.pdf,
+ ]
+%D Define \type{ps} as a synonym for \type{postscript} terminal
+\defineGNUPLOTterminal
+ [ps]
+ [postscript]
+
+%D \EPS\ terminal:
+%D - same terminal as for \POSTSCRIPT, but slightly different handling
+\defineGNUPLOTterminal
+ [eps]
+ [terminal=postscript,
+ defaultoptions=eps color,
+ suffix=eps,
+ result=\GNUPLOTfile-eps.eps,
+ % TODO: -dEPSCrop
+ convertwith={mtxrun pstopdf -dEPSCrop \GNUPLOTfile-eps.eps},
+ finalresult=\GNUPLOTfile-eps.pdf,
+ ]
+
+%D \PDF\ terminal
+\defineGNUPLOTterminal
+ [pdf]
+ [terminal=pdf,
+ defaultoptions=,
+ suffix=pdf,
+ result=\GNUPLOTfile-pdf.pdf,
+ convertwith=,
+ finalresult=\GNUPLOTfile-pdf.pdf,
+ ]
+\defineGNUPLOTterminal
+ [pdfcairo]
+ [terminal=pdfcairo,
+ defaultoptions=,
+ suffix=pdf,
+ result=\GNUPLOTfile-pdfcairo.pdf,
+ convertwith=,
+ finalresult=\GNUPLOTfile-pdfcairo.pdf,
+ ]
+
+%D \METAPOST\ terminal:
+%D - deprecated: use the \CONTEXT\ terminal instead,
+%D which was built starting from the \METAPOST\ one,
+%D but improved in many aspects
+\defineGNUPLOTterminal
+ [mp]
+ [terminal=mp,
+ defaultoptions=,
+ suffix=mp,
+ result=\GNUPLOTfile-mp.mp,
+ convertwith={mtxrun mptopdf \GNUPLOTfile-mp.mp},
+ % TODO: support for multiple graphics
+ % one would need a switch in mptopdf, so that a single pdf would be created instead of multiple ones
+ finalresult=\GNUPLOTfile-mp-0.pdf,
+ ]
+
+\defineGNUPLOTterminal
+ [metapost]
+ [mp]
+
+%D png terminal:
+%D - bitmap
+%D - no conversion needed
+%D - new version pretty advanced
+\defineGNUPLOTterminal
+ [png]
+ [terminal=png,
+ defaultoptions=,
+ suffix=png,
+ result=\GNUPLOTfile-png.png,
+ convertwith=,
+ finalresult=\GNUPLOTfile-png.png,
+ ]
+\defineGNUPLOTterminal
+ [pngcairo]
+ [terminal=pngcairo,
+ defaultoptions=,
+ suffix=png,
+ result=\GNUPLOTfile-pngcairo.png,
+ convertwith=,
+ finalresult=\GNUPLOTfile-pngcairo.png,
+ ]
+%D \CONTEXT\ terminal (native)
+\defineGNUPLOTterminal
+ [context]
+ [terminal=context,
+ defaultoptions=,
+ suffix=tex,
+ result=\GNUPLOTfile-ctx.tex,
+ convertwith=,
+ finalresult=\GNUPLOTfile-ctx.tex,
+ ]
+
+\def\setupGNUPLOTterminal
+ {\dodoubleargument\dosetupGNUPLOTterminal}
+
+\def\dosetupGNUPLOTterminal[#1][#2]%
+ {% TODO
+ \getparameters[@@GNUPLOT@term@#1@][#2]
+ % TODO: width & height
+ % linejoin, linecap - I have to improve this !!!
+ % \edef\gp:term:context:linejoin{\@@GNUPLOT@term@context@linejoin}
+ % \edef\gp:term:context:linecap{\@@GNUPLOT@term@context@linecap}
+ % dashed or solid lines?
+ \doifsamestringelse{\@@GNUPLOT@term@context@dashed}{yes}% yes or no
+ {\edef\@@GNUPLOT@term@context@is@dashed{true}}%
+ {\edef\@@GNUPLOT@term@context@is@dashed{false}}%
+ \doifsamestring{\@@GNUPLOT@term@context@width}{default}
+ {\edef\@@GNUPLOT@term@context@width{5in}}
+ \doifsamestring{\@@GNUPLOT@term@context@height}{default}
+ {\edef\@@GNUPLOT@term@context@height{3in}}
+ % {\edef\gp:term:context:dashed{true}}%
+ % {\edef\gp:term:context:dashed{false}}%
+ % % dashlength scale
+ % \edef\gp:term:context:dashlength{\@@GNUPLOT@term@context@dashlength}
+ % % linewidth scale
+ % \edef\gp:term:context:linewidth{\@@GNUPLOT@term@context@linewidth}
+ % \doifsamestringelse{\@@GNUPLOT@term@context@points}{metapost}% tex or metapost
+ }
+
+\setupGNUPLOTterminal
+ [context]
+ [width=default, % *default* (5in) | <dimension>
+ height=default, % *default* (3in) | <dimension>
+ linejoin=rounded, % mitered | *rounded* | beveled
+ linecap=butt, % *butt* | rounded (in gnuplot: round) | squared
+ dashed=yes, % *yes* | no
+ dashlength=1, % scaling factor for dash lengths
+ linewidth=1, % scaling factor for line widths (1.0 means 0.4bp)
+ fontscale=1, % scaling factor for text labels
+ points=metapost] % *metapost* | tex (Should points be drawn with MetaPost or TeX?)
+
+\setupGNUPLOTterminal
+ [tikz]
+ [width=5in, % *5in* | default (todo) | <dimension>
+ height=3in, % *3in* | default (todo) | <dimension>
+ fontscale=1, % scaling factor for text labels
+ ]
+
+% TODO: better scaling
+\defineconversion
+ [gnuplot:pointset]
+ [{\scale[scale=800]{\mathematics{+}}},
+ {\scale[scale=800]{\mathematics{\times}}},
+ \mathematics{\ast},
+ {\scale[scale=700]{\mathematics{\square}}},
+ {\scale[scale=700]{\mathematics{\blacksquare}}},
+ \mathematics{\circ},
+ \mathematics{\bullet},
+ {\scale[scale=900]{\mathematics{\triangleup}}},
+ {\scale[scale=900]{\mathematics{\blacktriangle}}},
+ {\scale[scale=900]{\mathematics{\triangledown}}},
+ {\scale[scale=900]{\mathematics{\blacktriangledown}}},
+ {\scale[scale=800]{\mathematics{\lozenge}}},
+ {\scale[scale=800]{\mathematics{\blacklozenge}}}%,
+% {\rotate[rotation=45]{\mathematics{\square}}},
+% {\rotate[rotation=45]{\mathematics{\blacksquare}}},
+ ]
+
+%\defineGNUPLOTcolor[red] [r=1]
+%\defineGNUPLOTcolor[green] [g=1]
+%\defineGNUPLOTcolor[blue] [b=1]
+%\defineGNUPLOTcolor[magenta][r=1,b=1]
+%\defineGNUPLOTcolor[cyan] [g=1,b=1]
+%\defineGNUPLOTcolor[yellow] [r=1,g=1]
+%\defineGNUPLOTcolor[black] [s=0]
+%\defineGNUPLOTcolor[orange] [r=1,g=.3,b=0]
+%\defineGNUPLOTcolor[gray50] [s=.5]
+
+%\defineGNUPLOTcolors
+% [default] % from PostScript
+% [red,green,blue,magenta,cyan,yellow,black,orange,gray50]
+
+% TODO: testset is here only temporary & for testing
+\setupGNUPLOT[program=gnuplot,\c!terminal=context,\c!purge=\v!yes,\c!options=,\c!pointset=gnuplot:pointset] % colors=postscript
+
+%D Some additional typescripts which enable us using font "Arial" and "Helvetica"
+
+%D Pretend the font to be serif as well, so that no "ss" switch is needed
+\starttypescript [serif] [helvetica] [name]
+ \definefontsynonym [Serif] [Helvetica]
+ \definefontsynonym [SerifBold] [Helvetica-Bold]
+ \definefontsynonym [SerifItalic] [Helvetica-Oblique]
+ \definefontsynonym [SerifSlanted] [Helvetica-Oblique]
+ \definefontsynonym [SerifBoldItalic] [Helvetica-BoldOblique]
+ \definefontsynonym [SerifBoldSlanted] [Helvetica-BoldOblique]
+ \definefontsynonym [SerifCaps] [Helvetica]
+\stoptypescript
+
+\beginOLDTEX
+
+ \starttypescript [gnuplot] [texnansi,ec]
+ \definetypeface [Helvetica] [rm] [serif] [helvetica] [default] [encoding=\typescripttwo]
+ \definetypeface [Helvetica] [ss] [sans] [helvetica] [default] [encoding=\typescripttwo]
+ \definetypeface [Arial] [rm] [serif] [helvetica] [default] [encoding=\typescripttwo]
+ \definetypeface [Arial] [ss] [sans] [helvetica] [default] [encoding=\typescripttwo]
+ \stoptypescript
+ \usetypescript[gnuplot][ec]
+
+\endOLDTEX
+\beginNEWTEX
+
+ \starttypescript [gnuplot]
+ \definetypeface [Helvetica] [rm] [serif] [helvetica] [default]
+ \definetypeface [Helvetica] [ss] [sans] [helvetica] [default]
+ \definetypeface [Arial] [rm] [serif] [helvetica] [default]
+ \definetypeface [Arial] [ss] [sans] [helvetica] [default]
+ \stoptypescript
+ \usetypescript[gnuplot]
+
+\endNEWTEX
+
+\stopmodule
+
+\protect \doifnotmode{demo}{\endinput}
+
+
+\starttext
+
+\startGNUPLOTscript[exp]
+set key bottom
+set format x "%.1f"
+set format y "%.1f"
+set style fill solid 0.25 noborder
+plot [0:3] 2/sqrt(pi)*exp(-x**2) t '$\frac{2}{\sqrt{\pi}}e^{-x^2}$' with filledcurves x1 lt 3, erf(x) lc 3 lw 2
+\stopGNUPLOTscript
+
+\useGNUPLOTgraphic[exp][width=.7\textwidth]
+
+
+\startGNUPLOTinclusions
+ set title "trigonometry"
+\stopGNUPLOTinclusions
+
+\startGNUPLOTscript[sin]
+ plot sin(x)
+\stopGNUPLOTscript
+
+\startGNUPLOTscript[cos]
+ plot cos(x)
+\stopGNUPLOTscript
+
+\useGNUPLOTgraphic[sin] \endgraf
+\useGNUPLOTgraphic[cos] \endgraf
+\useGNUPLOTgraphic[cos] \endgraf
+
+\setupGNUPLOT[terminal=ps] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
+%setupGNUPLOT[terminal=pdf] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
+%setupGNUPLOT[terminal=png] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
+\setupGNUPLOT[terminal=mp] \useGNUPLOTgraphic[sin][object=no,height=2cm,options=color] \blank
+
+\stoptext
+
diff --git a/macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkiv b/macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkiv
new file mode 100644
index 0000000000..13235105de
--- /dev/null
+++ b/macros/context/contrib/context-gnuplot/tex/context/third/gnuplot/t-gnuplot.mkiv
@@ -0,0 +1,977 @@
+%D \module
+%D [ file=t-gnuplot.mkiv,
+%D version=2013.05.09,
+%D title=\CONTEXT\ Extra Modules,
+%D subtitle=\GNUPLOT\ Inclusion,
+%D author={Mojca Miklavec, Marco Patzer, Hans Hagen, Taco Hoekwater, Aditya Mahajan},
+%D date=\currentdate,
+%D copyright=\PRAGMA]
+
+% begin info
+%
+% title : Gnuplot module
+%
+% comment : simplifies inclusion of gnuplot-generated graphs into ConTeXt documents
+% status : stable, supports both MKII and MKIV
+%
+% end info
+
+%D This module is used for creating gnuplot graphs on-the-fly and including them
+%D into documents.
+%D
+%D Known Bugs:
+%D
+%D \startitemize
+%D \item spurious space \& page
+%D \stopitemize
+%D
+%D TODO (Optimisations):
+%D
+%D \startitemize
+%D \item optimize the number of gnuplot runs (if possible, gnuplot should be run only once)
+%D \item optimize the number of times for loading/converting an already used graphic
+%D \item pstopdf is a bit slow
+%D \stopitemize
+%D
+%D TODO (Handle things that may go wrong):
+%D - gnuplot executable doesn't exist
+%D - context terminal isn't available or some other failure in gnuplot script (no file created)
+%D - write18 disabled (you may call gnuplot later - create a script)
+%D
+%D TODO (Missing functionality):
+%D
+%D - a lot ...
+
+\writestatus{loading}{Gnuplot module}
+
+\startmodule[gnuplot]
+
+\unprotect
+
+\def\c!terminal {terminal}
+\def\c!options {options}
+\def\c!pointset {pointset}
+\def\c!purge {purge}
+
+%D MPextensions
+%D
+%D XXX
+%D if possible, they should be specific to \type{\startGNUPLOTgraphic},
+%D so unvisible to \type{MPcode}
+%D (probably something like \type+\appendtoks ... to\everyGNUPLOTgraphic+)
+\startMPinitializations
+ % load metapost macros (only once)
+ input mp-gnuplot.mp ;
+ % number of points defined with \setupGNUPLOTterminal[pointset=...]
+ %gp_num_points_with_tex := \gp:num:pointswithtex;
+ gp_num_points_with_tex := 3;
+ % main color should be set equal to the current text color
+ gp_color_foreground := \MPcolor{currentcolor};
+ gp_color_lt[-2] := gp_color_foreground;
+ % TODO: is there any chance to make this local to gnuplot?
+ % linejoin & linecap
+ linejoin := \@@GNUPLOT@term@context@linejoin;
+% linecap := \gp:term:context:linecap;
+ % dashes or solid? (true/false)
+ gp_use_dashed := \@@GNUPLOT@term@context@is@dashed;
+ % dashlength scale
+% gp_scale_dashlength := \gp:term:context:dashlength;
+ % linewidth scale
+ gp_scale_linewidth := \@@GNUPLOT@term@context@linewidth;
+
+ gp_scale_text := \@@GNUPLOT@term@context@fontscale;
+
+ % linejoin := \gp:term:context:linejoin;
+ % linecap := \gp:term:context:linecap;
+ % % dashes or solid? (true/false)
+ % gp_use_dashed := \gp:term:context:dashed;
+ % % dashlength scale
+ % gp_scale_dashlength := \gp:term:context:dashlength;
+ % % linewidth scale
+ % gp_scale_linewidth := \gp:term:context:linewidth;
+
+ gp_points_with := gp_points_with_\@@GNUPLOT@term@context@points;
+
+\stopMPinitializations
+
+%D We need a \type{\strut} in front of labels for better vertical centering.
+%D This might still fail for Zapfino and alike where \type{\strut} might be smaller
+%D than the actual font height.
+%D
+%D TODO (optional improvement): instead of placing \type{\strut} in front,
+%D create a \type{\hbox} and adjust it's dimensions to \type{\strut}'s height and depth.
+
+%D Aditya, thanks a lot for requesting it!
+%D Hans, thanks a lot for implementing this!
+\definetextext[gp]{\strut}
+%D TODO (feature request):
+%D \starttyping
+%D \sometxt[gp][ss,20pt]{abc}
+%D \stoptyping
+%D should become equivalent to
+%D \starttyping
+%D \sometxt{\switchtobodyfont[ss,20pt]\strut abc}
+%D \stoptyping
+%D
+%D Or, even more drastic perhaps, I would love to implement
+%D \starttyping
+%D \sometxt[gp][iwona,bold,c]{abc}
+%D \stoptyping
+%D meaning: iwona, bold typeface, size \quote{c}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% TODO: make \sometxt[gp][12pt]{text} work in MKIV
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\newcounter\GNUPLOTnumber
+\newdimen\GNUPLOThcharsize
+\newdimen\GNUPLOTvcharsize
+\newcounter\tikzGNUPLOTnumber
+
+% macro to calculate true character size from current font
+% (However it is not 100 % that it gets the job properly done
+% in case that other font settings are used inside the picture.)
+\def\calculateGNUPLOTcharsize{%
+ \global\GNUPLOThcharsize=1.05\fontcharwd\font`0%
+ \global\GNUPLOTvcharsize=1.05\fontcharht\font`0%
+ \global\advance\GNUPLOTvcharsize by 1.05\fontchardp\font`g%
+}
+
+%D \macros
+%D {startGNUPLOTinclusions, resetGNUPLOTinclusions}
+%D
+%D For those who want to have two or more graphs with similar options,
+%D these options may be included inside \type{\startMPinclusions ... \stopMPinclusions}
+%D and will be place on the top of the created \GNUPLOT\ script.
+%D
+%D \starttyping
+%D \startGNUPLOTinclusions
+%D set xlabel '$x$'
+%D set ylabel '$y$'
+%D set format y "%.1f"
+%D \stopGNUPLOTinclusions
+%D
+%D \startGNUPLOTscript[sin]
+%D plot sin(x)
+%D \stopGNUPLOTscript
+%D \startGNUPLOTscript[cos]
+%D plot cos(x)
+%D \stopGNUPLOTscript
+%D \stoptyping
+\long\def\startGNUPLOTinclusions
+ {\def\stopGNUPLOTinclusions{\ifx\savebuffer\undefined \else \savebuffer[gnuplot-inclusions]\fi}%
+ \dostartbuffer[gnuplot-inclusions][startGNUPLOTinclusions][stopGNUPLOTinclusions]}
+
+% \def\resetGNUPLOTinclusions{\let\GNUPLOTinclusions\empty}
+
+% creates an empty file (there must be a cleaner way to do it)
+\def\resetGNUPLOTinclusions
+ {\immediate\openout\scratchwrite=\jobname-gnuplot-inclusions.tmp
+ \immediate\closeout\scratchwrite}
+
+%\startbuffer[gnuplot-inclusions]\stopbuffer\ifx\savebuffer\undefined \else \savebuffer[gnuplot-inclusions]\fi}
+
+\resetGNUPLOTinclusions
+
+%D On the other hand, one can probably achieve the same effect
+%D when drawing two plots inside the same script, like that:
+%D
+%D \starttyping
+%D \startGNUPLOTscript[sin and cos]
+%D set xlabel '$x$'
+%D set ylabel '$y$'
+%D set format y "%.1f"
+%D plot sin(x)
+%D plot cos(x)
+%D \stopGNUPLOTscript
+%D \stoptyping
+%D
+%D and then recall the graphics using \type{\useGNUPLOTgraphic[sin and cos][1]}.
+
+%D \macros
+%D {startGNUPLOTscript}
+%D
+%D \starttyping
+%D \startGNUPLOTscript{some name}
+%D plot sin(x)
+%D \stopGNUPLOTscript
+%D \stoptyping
+
+\def\startGNUPLOTscript
+ {\bgroup\dosingleempty\dostartGNUPLOTscript}
+
+% \def\redostartGNUPLOTscript#1
+% {\obeylines
+% \catcode`\%=\@@letter
+% \catcode`\|=\@@letter
+% \catcode`\$=\@@letter
+% \dodostartGNUPLOTscript{#1}%
+% %\dostartGNUPLOTscript[#2]
+% }
+%
+% \def\dostartGNUPLOTscript[#1]%
+% {\iffirstargument
+% \obeylines
+% \catcode`\%=\@@letter
+% \catcode`\|=\@@letter
+% \catcode`\$=\@@letter
+% \dodostartGNUPLOTscript{#1}%
+% \else
+% \redostartGNUPLOTscript
+% \fi
+% }
+
+\def\dostartGNUPLOTscript[#1]%
+ {%\iffirstargument
+ \dodostartGNUPLOTscript{#1}%
+ %\else
+ % \dodostartGNUPLOTscript
+ %\fi
+}
+
+% gps:n:{name} = gnuplotscript : number : {name} - number of script with name {name}
+\long\def\dodostartGNUPLOTscript#1%#2\stopGNUPLOTscript
+ {\doglobal\increment\GNUPLOTnumber
+ \letgvalue{gps:n:#1}\GNUPLOTnumber
+ % in case of LuaTeX we need to write the buffer into file explicitely
+ \def\stopGNUPLOTscript{\egroup \ifx\savebuffer\undefined \else \savebuffer[gnuplot-\GNUPLOTnumber]\fi}%
+ \dostartbuffer[gnuplot-\GNUPLOTnumber][startGNUPLOTscript][stopGNUPLOTscript]%
+ }
+
+% When are the graphics processed/read?
+%
+% - \gps:n:{name} (gnuplot script:name:{name}) holds the number of gnuplot script;
+% - that number was defined if we created the plot using \startGNUPLOTscript{name}
+% - and most probably undefined if we only issued \processGNUPLOTfile[name][filename]
+% => if, at the time of issuing \processGNUPLOTfile[name][filename], \gps:n:{name} is not defined,
+% it should be defined at that time
+% - \gpe:{name}:{terminal} (gnuplot graphic executed:{name}:{terminal})
+% is defined if we executed the command for that specific name and for that terminal
+% (once it will probably be 0 for a failed run and 1 for a successful one)
+
+% \useGNUPLOTgraphic[name] has three different ways of working:
+% - it can be called after \processGNUPLOTfile[name][filename]
+% which defined MP graphics that are now used
+% - it can be called for the first time under the current terminal
+% in that case it compiles the graphic and includes it
+% - it can be called for the second, third, ... time
+% in which case it only includes files without compiling it
+
+%D \macros
+%D {useGNUPLOTgraphic}
+
+% 5 ways of calling it:
+% - \useGNUPLOTgraphic{name}
+% - \useGNUPLOTgraphic[name]
+% - \useGNUPLOTgraphic[name][1,2,5]
+% - \useGNUPLOTgraphic[name][width=.8\textwidth]
+% - \useGNUPLOTgraphic[name][1,2,5][width=.8\textwidth]
+
+% this code takes care of reading arguments
+\def\useGNUPLOTgraphic
+ {\dotripleempty\douseGNUPLOTgraphic}
+
+\def\douseGNUPLOTgraphic[#1][#2][#3]%
+ {\doifelse{#3}{}{%
+ % < 3 arguments
+ \doifelse{#2}{}%
+ % 1 argument
+ % as in \useGNUPLOTgraphic[name]
+ {\dodouseGNUPLOTgraphic[#1][][]}%
+ % % as in \useGNUPLOTgraphic{name}
+ % {\redouseGNUPLOTgraphic[][][]}%
+ % 2 arguments
+ {\doifassignmentelse{#2}%
+ % as in \useGNUPLOTgraphic[name][width=.8\textwidth]
+ {\dodouseGNUPLOTgraphic[#1][][#2]}%
+ % as in \useGNUPLOTgraphic[name][1,2,5]
+ {\dodouseGNUPLOTgraphic[#1][#2][]}%
+ }%
+ }%
+ % as in \useGNUPLOTgraphic[name][1,2,5][width=.8\textwidth]
+ {\dodouseGNUPLOTgraphic[#1][#2][#3]}%
+ }
+
+\def\redouseGNUPLOTgraphic[#1][#2][#3]#4%
+ {\dodouseGNUPLOTgraphic[#4][][]}
+
+% and this code actually does something with it
+
+%D \doifundefined {string} {...}
+%D \doifdefined {string} {...}
+%D \doifundefinedelse {string} {then ...} {else ...}
+%D \doifdefinedelse {string} {then ...} {else ...}
+
+% \doifGNUPLOTscriptdefined{name}{...}
+
+% if gnuplot script with {name} and current terminal has already been processed
+% \doifGNUPLOTscriptprocessed{name}{...}
+
+% \letGNUPLOTscriptprocessed[optional terminal]{name} signals that the gnuplot script named {name}
+% has already been processed with the current terminal
+% TODO: currently it is always defined to be one as soon as one tries to process it,
+% even if en error is produced; in future it would be helpful if it would be set to zero
+% if it was unsuccessfully executed;
+% that is needed, since scripts are processed only when one first asks for including the graphic
+\def\letGNUPLOTscriptprocessed
+ {\dosingleempty\doletGNUPLOTscriptprocessed}
+%\def\doletGNUPLOTscriptprocessed[#1]#2%
+%{\letgvalue{gpe:#1:\@@GNUPLOTterminal}\plusone}
+\def\doletGNUPLOTscriptprocessed[#1]#2%
+ {\iffirstargument
+ % terminal has been specified
+ \letgvalue{gpe:#2:#1}\plusone
+ \else
+ % no terminal specified - use the current one
+ \letgvalue{gpe:#2:\@@GNUPLOTterminal}\plusone
+ \fi}
+
+% Although that should preferably not happen, one might come to an idea of defining
+% a gnuplot script with the same name as already defined.
+% In that case the old script cannot be referenced any more, but we can still try
+% to do out best to make it work anyway. We have to do two things:
+% - claim that script with that name hasn't been defined yet, so that processing will happen again
+% (TODO: do it in a more elegant way for all known terminals)
+% - undefine any metapost graphics (TODO: I have no idea yet how it can be done)
+%
+% \resetGNUPLOTscriptprocessed{name}
+\def\resetGNUPLOTscriptprocessed#1%
+ {\bgroup
+ % claim that the script with {name} (#1) hasn't been processed with {terminal} (##1) yet
+ \def\undefineGNUPLOTscriptforterminal##1{\letbeundefined{gpe:#1:##1}}%
+ % TODO: the list of available terminals should be generated automatically
+ \processcommalist[context,postscript,ps,eps,pdf,metapost,mp,png]\undefineGNUPLOTscriptforterminal
+ \resetGNUPLOTgraphics{#1}%
+ \egroup}
+
+% \resetGNUPLOTgraphics{name} undefines any gnuplot graphic defined with \startGNUPLOTgraphic[name][number]
+\def\resetGNUPLOTgraphics#1%
+ {\doloop
+ {\doifMPgraphicelse{gpg:#1:\recurselevel}%
+ % TODO: be aware - @@MPG is low-level ConTeXt variable which might change without notice,
+ % it would be better to call this \undefineMPgraphic{gpg:#1:\recurselevel} or something similar
+ {\letbeundefined{@@MPGgpg:#1:\recurselevel}}%
+ {\exitloop}}%
+ \doloop
+ {\doifdefinedelse{TKZp:#1:\recurselevel}%
+ {\letbeundefined{TKZp:#1:\recurselevel}}%
+ {\exitloop}}%
+ }%
+
+% TODO: this can probably be done in a better way
+\def\doifGNUPLOTscriptprocessed#1#2%
+ {\doifdefined {gpe:#1:\@@GNUPLOTterminal}{#2}}
+\def\doifGNUPLOTscriptprocessedelse#1#2#3%
+ {\doifdefinedelse {gpe:#1:\@@GNUPLOTterminal}{#2}{#3}}
+\def\doifGNUPLOTscriptnotprocessed#1#2%
+ {\doifundefined {gpe:#1:\@@GNUPLOTterminal}{#2}}
+\def\doifGNUPLOTscriptnotprocessedelse#1#2#3%
+ {\doifundefinedelse{gpe:#1:\@@GNUPLOTterminal}{#2}{#3}}
+
+
+% private
+% \writeandprocessGNUPLOTscript{name}
+\def\writeandprocessGNUPLOTscript#1%
+ % TODO: if gps:n:#1 (holding the script content) is not defined,
+ % error or warning should be issued
+ %
+ % only process the script if it has been defined and not processed before for the current terminal
+ {\doifdefined{gps:n:#1}{\doifGNUPLOTscriptnotprocessed{#1}{%
+ % \gpe:{name}:{terminal} is defined
+ %\setgvalue{gpe:#1:\@@GNUPLOTterminal}{#2}
+ \letGNUPLOTscriptprocessed{#1}%
+ % call to gnuplot and processing/converting the graphics is only needed in the first ConTeXt run
+ \doifmode{*\v!first}{% (perhaps also: if files haven't changed)
+ \bgroup
+ % for TikZ we want to calculate character size, so that it gets printed
+ % to inclusions with \@@GNUPLOToptions
+ \calculateGNUPLOTcharsize
+ % catcodes trickery & alike
+ %\the\everyGNUPLOTscript
+ % open file for writing
+ \immediate\openout\scratchwrite=\GNUPLOTfile.plt
+ % TODO: terminal-specific options (default or provided by the user)
+ \immediate\write\scratchwrite{\letterhash\space Do not modify this file - all changes will be overwritten}%
+ \immediate\write\scratchwrite{\letterhash\space Change \jobname.tex instead.}%
+ \immediate\write\scratchwrite{set terminal \@@GNUPLOToutput\space\@@GNUPLOToptions}%
+ % add common inclusions in scripts for multiple similar plots
+ %\doifnotempty
+ % {\GNUPLOTinclusions}{\immediate\write\scratchwrite{\GNUPLOTinclusions}}%
+ % include common gnuplot 'inclusions'
+ \immediate\write\scratchwrite{load '\jobname-gnuplot-inclusions.tmp'}%
+ % output file
+ \immediate\write\scratchwrite{set output "\@@GNUPLOTresult"}%
+ % write main contents of the script, like "plot sin(x)"
+ %\immediate\write\scratchwrite{\getvalue{gps:d:\GNUPLOTnumber}}%
+ \immediate\write\scratchwrite{load '\GNUPLOTfile.tmp'}%
+ \immediate\closeout\scratchwrite
+ \egroup
+ % TODO:
+ % - check the state of write18 and warn the user if it's disabled,
+ % otherwise just everyone will start complaining that the module doens't work
+ % - check if execution was successful; possible pitfalls:
+ % - gnuplot doesn't exist as a binary
+ % - gnuplot doesn't support context terminal
+ % or some other error in script which results in empty output file
+ %
+ % run gnuplot & execute the script that has just been written
+ % \executesystemcommand{mtxrun --ifchanged=\GNUPLOTfile.plt\space
+ % --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
+ \executesystemcommand{mtxrun --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
+ \writestatus{aaa}{mtxrun --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
+ \convertGNUPLOTgraphic
+ }%
+ % for ConTeXt terminal only - read the result
+ \doif{\@@GNUPLOTterminal}{context}{\processGNUPLOTfile[#1][\@@GNUPLOTresult]}%
+ \doif{\@@GNUPLOTterminal}{tikz} {\processGNUPLOTfile[#1][\@@GNUPLOTresult]}%
+ }}}
+
+% \dodouseGNUPLOTgraphic[name][numbers][options]
+\def\dodouseGNUPLOTgraphic[#1][#2][#3]%
+ {\bgroup
+ \doifdefinedelse{gps:n:#1}
+ {\edef\GNUPLOTnumber{\getvalue{gps:n:#1}}%
+ \edef\GNUPLOTfile {\jobname-gnuplot-\GNUPLOTnumber}%
+ \writeandprocessGNUPLOTscript{#1}%
+ % "ctxtools --purge" should delete the gnuplot script and other intermediate files
+ % (but it seems that they are deleted automatically already; preferred or not?)
+% \registertempfile{\GNUPLOTfile.plt}
+% \registertempfile{\@@GNUPLOTresult}
+% \registertempfile{\@@GNUPLOTfinalresult}
+ \doifelse{\@@GNUPLOTterminal}{context}%
+ % for ConTeXt terminal only
+ {\doifelse{#2}{}%
+ % if no explicit figure number was specified, include all the figures
+ {\doloop
+ {\doifMPgraphicelse{gpg:#1:\recurselevel}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:\recurselevel}}}%
+ {\exitloop}}}%
+ % if numbers were specified, include the figures specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\doifMPgraphicelse
+ {gpg:#1:##1}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:##1}}}%
+ % if graphic doesn't exist: draw a dummy frame instead and warn the user
+ {\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }{%
+ % for TikZ terminal only
+ \doifelse{\@@GNUPLOTterminal}{tikz}%
+ {\doifelse{#2}{}%
+ % if no explicit figure number was specified, include all the figures
+ {\doloop
+ {\doifdefinedelse{TKZp:#1:\recurselevel}%
+ {\scale[#3]{\getvalue{TKZp:#1:\recurselevel}}}%
+ {\exitloop}}}%
+ % if numbers were specified, include the figures specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\doifdefinedelse
+ {TKZp:#1:##1}%
+ {\scale[#3]{\getvalue{TKZp:#1:##1}}}%
+ % if graphic doesn't exist: draw a dummy frame instead and warn the user
+ {\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }%
+ % for all the other terminals
+ % TODO: add more safety switches (if pages don't exist for example)
+ {\doifelse{#2}{}%
+ % if no explicite figure number was specified, include all the pages
+ % TODO: properly handle METAPOST & PNG (only works for (E)PS & PDF) !!!
+ {\getfiguredimensions[\@@GNUPLOTfinalresult]%
+ \dorecurse{\noffigurepages}{\externalfigure[\@@GNUPLOTfinalresult][page=\recurselevel,#3]}}%
+ % if numbers were specified, include the pages specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\externalfigure[\@@GNUPLOTfinalresult][page=##1,#3]}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }}%
+ }%
+ % else part - in case that graphic gets included with \processGNUPLOTfile[name][file]
+ {\doifelse{\@@GNUPLOTterminal}{context}
+ % TODO: this part has been literally copied from above; create a subroutine instead
+ {\doifelse{#2}{}%
+ % if no explicit figure number was specified, include all the figures
+ {\doloop
+ {\doifMPgraphicelse{gpg:#1:\recurselevel}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:\recurselevel}}}%
+ {\exitloop}}}%
+ % if numbers were specified, include the figures specified in the list only
+ {\begingroup
+ \def\useGNUPLOTgraphicN##1{\doifMPgraphicelse
+ {gpg:#1:##1}%
+ {\scale[#3]{\reuseMPgraphic{gpg:#1:##1}}}%
+ % if graphic doesn't exist: draw a dummy frame instead and warn the user
+ {\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
+ \processcommalist[#2]\useGNUPLOTgraphicN
+ \endgroup
+ }%
+ }%
+ {\message{The gnuplot graphic #1 has not been defined.}}}%
+ \egroup}
+
+%D \macros
+%D {setupGNUPLOT}
+
+\def\setupGNUPLOT
+% {\dodoubleempty\getparameters[@@GNUPLOT]}
+ {\dosingleargument\dosetupGNUPLOT}
+
+\def\dosetupGNUPLOT[#1]%
+ {\getparameters[@@GNUPLOT][#1]%
+ % define all the necessary points according to the option "pointset"
+ %
+ % XXX: no idea why this is needed, but otherwise it complains that @@GNUPLOTpointset is undefined
+ \edef\currentGNUPLOTpointset{\@@GNUPLOTpointset}%
+ % \gp:num:pointswithtex is passed to metapost, so that it knows
+ % how many points are defined and chooses the proper point
+ % form a set of the defined ones
+ %
+ % for safety reasons define \gp:num:pointswithtex to be equal to 1 (it can only increase later), otherwise:
+ % - (something mod 0) won't work
+ % - if conversion is not defined, the number will retain its old value (not desirable)
+ \edef\gp:num:pointswithtex{1}%
+ \doloop{\doifelseconversionnumber{\@@GNUPLOTpointset}{\recurselevel}%
+ {\edef\gp:num:pointswithtex{\recurselevel}}%
+ {\exitloop}}%
+ % in case of TikZ terminal we have to generate the needed style files first
+ \doif{\@@GNUPLOTterminal}{tikz}
+ {\executesystemcommand{gnuplot -e "set term tikz context createstyle"}\usemodule[gnuplot-lua-tikz]}
+ % TODO:
+ % - issue a warning if user wants to use points with TeX,
+ % but pointset= is undefined (if the first point doesn't exist)
+ % - no idea what happens if conversion is infinite,
+ % so try to stop at some reasonable value (100?)
+ % - current implementation redefines the points even if only terminal type
+ % has been set to some other value (which is a stupid approach, but I
+ % wanted to have a working version first and will consider efficiency later)
+ % - it may be that the old points remain defined if conversion changes
+ % (perhaps they should be undefined again?)
+ }
+
+% Hans has written this piece of code, but:
+% - "start" and "/MIN" caused problems
+% - pgnuplot is not much more "portable" than gnuplot
+% best thing to do is to create a "gnuplot.bat" somewhere in PATH
+%
+%\def\processGNUPLOTscript
+% {\doifelse\operatingsystem{mswin}
+% {\executesystemcommand{start /MIN pgnuplot \GNUPLOTfile.plt}} % start is needed else gp fails
+% {\executesystemcommand{gnuplot \GNUPLOTfile.plt}}}
+%\def\processGNUPLOTscript
+% {\executesystemcommand{gnuplot \GNUPLOTfile.plt}}
+
+% TODO: check if write18 is enabled; if not, issue a command and warn the user that running the module might be pointless or that he/she has to run gnuplot on the produced files manually
+\def\convertGNUPLOTgraphic
+ {\doifsomething\@@GNUPLOThandle{\writestatus{conversion}{\@@GNUPLOThandle}\executesystemcommand{\@@GNUPLOThandle}}}
+
+%D \macros
+%D {processGNUPLOTfile}
+
+%D It's needed to input a file resulting from a gnuplot run (with ConTeXt terminal).
+%D It reads the file and "saves" the metapost graphics defined in that file,
+%D so that they can be used with \usegnuplotgraphic (low level: \reuseMPgraphic) later
+%D
+% \processGNUPLOTfile[NAME][filename]
+\def\processGNUPLOTfile
+ {\dodoubleargument\doprocessGNUPLOTfile}
+
+\def\doprocessGNUPLOTfile[#1][#2]%
+ {\bgroup
+ % for ConTeXt teminal
+ %--------------------
+ % we first define two commands: \startGNUPLOTgraphic & \stopGNUPLOTgraphic to read the files in;
+ % files that gnuplot creates in non-standalone mode look approximately like this:
+ %
+ % \startGNUPLOTgraphic[1]
+ % ... metapost commands to draw the graph ...
+ % \stopGNUPLOTgraphic
+ % \startGNUPLOTgraphic[2]
+ % ... metapost commands to draw the graph ...
+ % \stopGNUPLOTgraphic
+ % ...
+ %
+ % while reading the file in, metapost graphics named "gpg:{name}:{number}" are defined
+ %
+ % (we might need some additional arguments later on, but for now the figure number should suffice)
+ \def\startGNUPLOTgraphic
+ {%\obeyMPlines % <- no longer a problem
+ \def\obeyedline{}% <- thanks to this
+ \dosingleargument\dostartGNUPLOTgraphic}%
+ \long\def\dostartGNUPLOTgraphic[##1]##2\stopGNUPLOTgraphic
+ {\startreusableMPgraphic{gpg:#1:##1}##2\stopreusableMPgraphic}%
+ %------------------
+ % for TikZ terminal
+ %------------------
+ \newcounter\tikzGNUPLOTnumber
+ \let\normalstarttikzpicture=\starttikzpicture
+ \let\normalstoptikzpicture=\stoptikzpicture
+ \long\def\starttikzpicture##1\stoptikzpicture%
+ {\increment\tikzGNUPLOTnumber
+ % \long\setgvalue{TKZp:#1:\tikzGNUPLOTnumber}{##1}}
+ \long\setgvalue{TKZp:#1:\tikzGNUPLOTnumber}{\hbox{\starttikzpicture##1\stoptikzpicture}}}
+ %---------
+ % for both
+ %---------
+ % input the file
+ % TODO: error / warning if the files doesn't exist
+ \readlocfile{#2}{}{}% the third argument should be: if file not found
+ % this file is known
+ % TODO \letgvalue{gpf:#1}\plusone
+ \egroup
+ }
+
+%D \macros
+%D {startGNUPLOTpage}
+%D
+%D Used for standalone \GNUPLOT\ figures & written out by gnuplot in standalone mode.
+%D
+%D Instead of having to \type{\useGNUPLOTgraphic}, a figure is inserted directly using
+%D \type{\startMPpage ... }\type{\stopMPpage}.
+%D
+%D A high-level command is provided if some more advance features will
+%D be needed in the future and to be able to ensure backward compatibility.
+
+%D We need to preserve line breaks, otherwise metapost runs into troubles for longer input.
+\def\startGNUPLOTpage
+ {\begingroup\dostartGNUPLOTpage}
+
+\long\def\dostartGNUPLOTpage#1\stopGNUPLOTpage
+ {\endgroup\startMPpage#1\stopMPpage}
+
+% TODO: use a separate instance for MKIV
+%
+% \defineMPinstance[gnuplot]
+% \startTEXpage\startMPcode{gnuplot}
+% \stopMPcode\stopTEXpage
+
+% TODO: currently, the following definitions are used,
+% but the ones below would be preferred
+
+\def\defineGNUPLOThandle#1#2#3#4#5#6% name; output; suffix; conversion-method; gnuplot's result; final result
+ {\setvalue{@@GNUPLOT-#1}{{#2}{#3}{#4}{#5}{#6}}}
+
+\def\@@GNUPLOToutput{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}51}
+\def\@@GNUPLOTsuffix{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}52}
+\def\@@GNUPLOThandle{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}53}
+\def\@@GNUPLOTresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}54}
+\def\@@GNUPLOTfinalresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}55}
+
+% name "set term" suffix conversion (system command) gnuplot's result final result
+% (suffix is probably not needed any more since full names were introduced)
+\defineGNUPLOThandle
+ {postscript}{postscript color}{ps}{mtxrun pstopdf \GNUPLOTfile-ps.ps}
+ {\GNUPLOTfile-ps.ps}{\GNUPLOTfile-ps.pdf}
+\defineGNUPLOThandle
+ {ps}{postscript color}{ps}{mtxrun pstopdf -dAutoRotatePages=/PageByPage \GNUPLOTfile-ps.ps}
+ {\GNUPLOTfile-ps.ps}{\GNUPLOTfile-ps.pdf}
+\defineGNUPLOThandle
+ {eps}{postscript color eps}{ps}{mtxrun pstopdf -dEPSCrop \GNUPLOTfile-eps.eps}
+ {\GNUPLOTfile-eps.eps}{\GNUPLOTfile-eps.pdf}
+\defineGNUPLOThandle
+ {pdf}{pdf}{pdf}{}
+ {\GNUPLOTfile-pdf.pdf}{\GNUPLOTfile-pdf.pdf}
+\defineGNUPLOThandle
+ {pdfcairo}{pdfcairo}{pdf}{}
+ {\GNUPLOTfile-pdfcairo.pdf}{\GNUPLOTfile-pdfcairo.pdf}
+\defineGNUPLOThandle
+ {metapost}{mp}{mp}{mtxrun mptopdf \GNUPLOTfile-mp.mp}
+ {\GNUPLOTfile-mp.mp}{\GNUPLOTfile-mp-0.pdf}
+\defineGNUPLOThandle
+ {mp}{mp}{mp}{mtxrun mptopdf \GNUPLOTfile-mp.mp}
+ {\GNUPLOTfile-mp.mp}{\GNUPLOTfile-mp-0.pdf}
+\defineGNUPLOThandle
+ {png}{png}{png}{}
+ {\GNUPLOTfile-png.png}{\GNUPLOTfile-png.png}
+\defineGNUPLOThandle
+ {pngcairo}{pngcairo}{png}{}
+ {\GNUPLOTfile-pngcairo.png}{\GNUPLOTfile-pngcairo.png}
+\defineGNUPLOThandle
+ {tikz}{tikz context createstyle
+ size \@@GNUPLOT@term@tikz@width,\@@GNUPLOT@term@tikz@height\space
+ charsize \the\GNUPLOThcharsize,\the\GNUPLOTvcharsize\space
+ fontscale \@@GNUPLOT@term@tikz@fontscale
+ }{tex}{}
+ {\GNUPLOTfile-tikz.tex}{\GNUPLOTfile-tikz.tex}
+\defineGNUPLOThandle
+ {context}{context
+ size \@@GNUPLOT@term@context@width,\@@GNUPLOT@term@context@height\space
+ fontscale \@@GNUPLOT@term@context@fontscale
+ }{tex}{}
+ {\GNUPLOTfile-ctx.tex}{\GNUPLOTfile-ctx.tex}
+% \doifnotempty{\@@GNUPLOT@term@context@fontscale}{fontscale \@@GNUPLOT@term@context@fontscale}
+
+\def\defineGNUPLOTterminal
+ {\dodoubleargument\dodefineGNUPLOTterminal}
+
+\def\dodefineGNUPLOTterminal[#1][#2]%
+ {\doifassignmentelse{#2}%
+ % define a proper terminal
+ {}% TODO
+ % only define a synonym for that terminal
+ {}% TODO
+ }
+
+%D {\sl terminal}: argument to be passed to gnuplot in the form of "set term {\sl terminal}"
+%D {\sl defaultoptions}: options to be passed to gnuplot after terminal name:
+%D set term {\sl teminal} {\sl defaultoptions}
+%D not to be touched by users
+%D {\sl suffix}: filename suffix
+%D {\sl result}: the file to which gnuplot should output the result
+%D set output {\sl result}
+%D {\sl convertwith}: command for conversion from gnuplot-generated file to a file that can be read by \CONTEXT; may be empty
+%D {\sl finalresult}: file resulting after the conversion to be read by \CONTEXT
+
+%D \POSTSCRIPT\ terminal
+\defineGNUPLOTterminal
+ [postscript]
+ [terminal=postscript,
+ defaultoptions=color, % TODO: only if \setupcolors[state=start]
+ suffix=ps,
+ result=\GNUPLOTfile-ps.ps,
+ % TODO: -dAutoRotatePages=/PageByPage or remove that option from the default ones
+ convertwith={mtxrun pstopdf \GNUPLOTfile-ps.ps},
+ finalresult=\GNUPLOTfile-ps.pdf,
+ ]
+%D Define \type{ps} as a synonym for \type{postscript} terminal
+\defineGNUPLOTterminal
+ [ps]
+ [postscript]
+
+%D \EPS\ terminal:
+%D - same terminal as for \POSTSCRIPT, but slightly different handling
+\defineGNUPLOTterminal
+ [eps]
+ [terminal=postscript,
+ defaultoptions=eps color,
+ suffix=eps,
+ result=\GNUPLOTfile-eps.eps,
+ % TODO: -dEPSCrop
+ convertwith={mtxrun pstopdf -dEPSCrop \GNUPLOTfile-eps.eps},
+ finalresult=\GNUPLOTfile-eps.pdf,
+ ]
+
+%D \PDF\ terminal
+\defineGNUPLOTterminal
+ [pdf]
+ [terminal=pdf,
+ defaultoptions=,
+ suffix=pdf,
+ result=\GNUPLOTfile-pdf.pdf,
+ convertwith=,
+ finalresult=\GNUPLOTfile-pdf.pdf,
+ ]
+\defineGNUPLOTterminal
+ [pdfcairo]
+ [terminal=pdfcairo,
+ defaultoptions=,
+ suffix=pdf,
+ result=\GNUPLOTfile-pdfcairo.pdf,
+ convertwith=,
+ finalresult=\GNUPLOTfile-pdfcairo.pdf,
+ ]
+
+%D \METAPOST\ terminal:
+%D - deprecated: use the \CONTEXT\ terminal instead,
+%D which was built starting from the \METAPOST\ one,
+%D but improved in many aspects
+\defineGNUPLOTterminal
+ [mp]
+ [terminal=mp,
+ defaultoptions=,
+ suffix=mp,
+ result=\GNUPLOTfile-mp.mp,
+ convertwith={mtxrun mptopdf \GNUPLOTfile-mp.mp},
+ % TODO: support for multiple graphics
+ % one would need a switch in mptopdf, so that a single pdf would be created instead of multiple ones
+ finalresult=\GNUPLOTfile-mp-0.pdf,
+ ]
+
+\defineGNUPLOTterminal
+ [metapost]
+ [mp]
+
+%D png terminal:
+%D - bitmap
+%D - no conversion needed
+%D - new version pretty advanced
+\defineGNUPLOTterminal
+ [png]
+ [terminal=png,
+ defaultoptions=,
+ suffix=png,
+ result=\GNUPLOTfile-png.png,
+ convertwith=,
+ finalresult=\GNUPLOTfile-png.png,
+ ]
+\defineGNUPLOTterminal
+ [pngcairo]
+ [terminal=pngcairo,
+ defaultoptions=,
+ suffix=png,
+ result=\GNUPLOTfile-pngcairo.png,
+ convertwith=,
+ finalresult=\GNUPLOTfile-pngcairo.png,
+ ]
+%D \CONTEXT\ terminal (native)
+\defineGNUPLOTterminal
+ [context]
+ [terminal=context,
+ defaultoptions=,
+ suffix=tex,
+ result=\GNUPLOTfile-ctx.tex,
+ convertwith=,
+ finalresult=\GNUPLOTfile-ctx.tex,
+ ]
+
+\def\setupGNUPLOTterminal
+ {\dodoubleargument\dosetupGNUPLOTterminal}
+
+\def\dosetupGNUPLOTterminal[#1][#2]%
+ {% TODO
+ \getparameters[@@GNUPLOT@term@#1@][#2]
+ % TODO: width & height
+ % linejoin, linecap - I have to improve this !!!
+ % \edef\gp:term:context:linejoin{\@@GNUPLOT@term@context@linejoin}
+ % \edef\gp:term:context:linecap{\@@GNUPLOT@term@context@linecap}
+ % dashed or solid lines?
+ \doifsamestringelse{\@@GNUPLOT@term@context@dashed}{yes}% yes or no
+ {\edef\@@GNUPLOT@term@context@is@dashed{true}}%
+ {\edef\@@GNUPLOT@term@context@is@dashed{false}}%
+ \doifsamestring{\@@GNUPLOT@term@context@width}{default}
+ {\edef\@@GNUPLOT@term@context@width{5in}}
+ \doifsamestring{\@@GNUPLOT@term@context@height}{default}
+ {\edef\@@GNUPLOT@term@context@height{3in}}
+ % {\edef\gp:term:context:dashed{true}}%
+ % {\edef\gp:term:context:dashed{false}}%
+ % % dashlength scale
+ % \edef\gp:term:context:dashlength{\@@GNUPLOT@term@context@dashlength}
+ % % linewidth scale
+ % \edef\gp:term:context:linewidth{\@@GNUPLOT@term@context@linewidth}
+ % \doifsamestringelse{\@@GNUPLOT@term@context@points}{metapost}% tex or metapost
+ }
+
+\setupGNUPLOTterminal
+ [context]
+ [width=default, % *default* (5in) | <dimension>
+ height=default, % *default* (3in) | <dimension>
+ linejoin=rounded, % mitered | *rounded* | beveled
+ linecap=butt, % *butt* | rounded (in gnuplot: round) | squared
+ dashed=yes, % *yes* | no
+ dashlength=1, % scaling factor for dash lengths
+ linewidth=1, % scaling factor for line widths (1.0 means 0.4bp)
+ fontscale=1, % scaling factor for text labels
+ points=metapost] % *metapost* | tex (Should points be drawn with MetaPost or TeX?)
+
+\setupGNUPLOTterminal
+ [tikz]
+ [width=5in, % *5in* | default (todo) | <dimension>
+ height=3in, % *3in* | default (todo) | <dimension>
+ fontscale=1, % scaling factor for text labels
+ ]
+
+% TODO: better scaling
+\defineconversion
+ [gnuplot:pointset]
+ [{\scale[scale=800]{\mathematics{+}}},
+ {\scale[scale=800]{\mathematics{\times}}},
+ \mathematics{\ast},
+ {\scale[scale=700]{\mathematics{\square}}},
+ {\scale[scale=700]{\mathematics{\blacksquare}}},
+ \mathematics{\circ},
+ \mathematics{\bullet},
+ {\scale[scale=900]{\mathematics{\triangleup}}},
+ {\scale[scale=900]{\mathematics{\blacktriangle}}},
+ {\scale[scale=900]{\mathematics{\triangledown}}},
+ {\scale[scale=900]{\mathematics{\blacktriangledown}}},
+ {\scale[scale=800]{\mathematics{\lozenge}}},
+ {\scale[scale=800]{\mathematics{\blacklozenge}}}%,
+% {\rotate[rotation=45]{\mathematics{\square}}},
+% {\rotate[rotation=45]{\mathematics{\blacksquare}}},
+ ]
+
+%\defineGNUPLOTcolor[red] [r=1]
+%\defineGNUPLOTcolor[green] [g=1]
+%\defineGNUPLOTcolor[blue] [b=1]
+%\defineGNUPLOTcolor[magenta][r=1,b=1]
+%\defineGNUPLOTcolor[cyan] [g=1,b=1]
+%\defineGNUPLOTcolor[yellow] [r=1,g=1]
+%\defineGNUPLOTcolor[black] [s=0]
+%\defineGNUPLOTcolor[orange] [r=1,g=.3,b=0]
+%\defineGNUPLOTcolor[gray50] [s=.5]
+
+%\defineGNUPLOTcolors
+% [default] % from PostScript
+% [red,green,blue,magenta,cyan,yellow,black,orange,gray50]
+
+% TODO: testset is here only temporary & for testing
+\setupGNUPLOT[program=gnuplot,\c!terminal=context,\c!purge=\v!yes,\c!options=,\c!pointset=gnuplot:pointset] % colors=postscript
+
+%D Some additional typescripts which enable us using font "Arial" and "Helvetica"
+
+%D Pretend the font to be serif as well, so that no "ss" switch is needed
+\starttypescript [serif] [helvetica] [name]
+ \definefontsynonym [Serif] [Helvetica]
+ \definefontsynonym [SerifBold] [Helvetica-Bold]
+ \definefontsynonym [SerifItalic] [Helvetica-Oblique]
+ \definefontsynonym [SerifSlanted] [Helvetica-Oblique]
+ \definefontsynonym [SerifBoldItalic] [Helvetica-BoldOblique]
+ \definefontsynonym [SerifBoldSlanted] [Helvetica-BoldOblique]
+ \definefontsynonym [SerifCaps] [Helvetica]
+\stoptypescript
+
+\starttypescript [gnuplot]
+ \definetypeface [Helvetica] [rm] [serif] [helvetica] [default]
+ \definetypeface [Helvetica] [ss] [sans] [helvetica] [default]
+ \definetypeface [Arial] [rm] [serif] [helvetica] [default]
+ \definetypeface [Arial] [ss] [sans] [helvetica] [default]
+\stoptypescript
+\usetypescript[gnuplot]
+
+\stopmodule
+
+\protect \doifnotmode{demo}{\endinput}
+
+
+\starttext
+
+\startGNUPLOTscript[exp]
+set key bottom
+set format x "%.1f"
+set format y "%.1f"
+set style fill solid 0.25 noborder
+plot [0:3] 2/sqrt(pi)*exp(-x**2) t '$\frac{2}{\sqrt{\pi}}e^{-x^2}$' with filledcurves x1 lt 3, erf(x) lc 3 lw 2
+\stopGNUPLOTscript
+
+\useGNUPLOTgraphic[exp][width=.7\textwidth]
+
+
+\startGNUPLOTinclusions
+ set title "trigonometry"
+\stopGNUPLOTinclusions
+
+\startGNUPLOTscript[sin]
+ plot sin(x)
+\stopGNUPLOTscript
+
+\startGNUPLOTscript[cos]
+ plot cos(x)
+\stopGNUPLOTscript
+
+\useGNUPLOTgraphic[sin] \endgraf
+\useGNUPLOTgraphic[cos] \endgraf
+\useGNUPLOTgraphic[cos] \endgraf
+
+\setupGNUPLOT[terminal=ps] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
+%setupGNUPLOT[terminal=pdf] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
+%setupGNUPLOT[terminal=png] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
+\setupGNUPLOT[terminal=mp] \useGNUPLOTgraphic[sin][object=no,height=2cm,options=color] \blank
+
+\stoptext
+