-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgnuplot-tikz.lua
More file actions
2501 lines (2184 loc) · 77.6 KB
/
gnuplot-tikz.lua
File metadata and controls
2501 lines (2184 loc) · 77.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
This is the execution script for the `Lua generic terminal' driver.
This script provides an interface to the PGF/TikZ package for LaTeX.
Copyright 2008 Peter Hedwig <peter@affenbande.org>
Permission to use, copy, and distribute this software and its
documentation for any purpose with or without fee is hereby granted,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation.
Permission to modify the software is granted, but not the right to
distribute the complete modified source code. Modifications are to
be distributed as patches to the released version. Permission to
distribute binaries produced by compiling modified sources is granted,
provided you
1. distribute the corresponding source modifications from the
released version in the form of a patch file along with the binaries,
2. add special version identification to distinguish your version
in addition to the base release version number,
3. provide your name and address as the primary contact for the
support of your modified version, and
4. retain our contact information in regard to use of the base
software.
Permission to distribute the released version of the source code along
with corresponding source modifications in the form of a patch file is
granted with same provisions 2 through 4 for binary distributions.
This software is provided "as is" without express or implied warranty
to the extent permitted by applicable law.
$Date: 2014/09/23 03:55:25 $
$Author: sfeam $
$Rev: 100 $
]]--
--[[
`term' gnuplot term_api -> local interface
`gp' local -> gnuplot interface
are both initialized by the terminal
]]--
--
-- internal variables
--
local pgf = {}
local gfx = {}
-- the terminal default size in cm
pgf.DEFAULT_CANVAS_SIZE_X = 12.5
pgf.DEFAULT_CANVAS_SIZE_Y = 8.75
-- tic default size in cm
pgf.DEFAULT_TIC_SIZE = 0.18
-- the terminal resolution in "dots" per cm.
pgf.DEFAULT_RESOLUTION = 1000
-- default font size in TeX pt
pgf.DEFAULT_FONT_SIZE = 10
-- default sizes for CM@10pt and default resolution
-- there is no need to adapt these values when changing
-- pgf.DEFAULT_FONT_SIZE or pgf.DEFAULT_RESOLUTION !
pgf.DEFAULT_FONT_H_CHAR = 184
pgf.DEFAULT_FONT_V_CHAR = 308
pgf.STYLE_FILE_BASENAME = "gnuplot-lua-tikz" -- \usepackage{gnuplot-lua-tikz}
pgf.REVISION = string.sub("$Rev: 100 $",7,-3)
pgf.REVISION_DATE = string.gsub("$Date: 2014/09/23 03:55:25 $",
"$Date: ([0-9]+).([0-9]+).([0-9]+) .*","%1/%2/%3")
pgf.styles = {}
-- the styles are used in conjunction with the 'tikzarrows'
-- and the style number directly corresponds to the used
-- angle in the gnuplot style definition
pgf.styles.arrows = {
[1] = {"gp arrow 1", ">=latex"},
[2] = {"gp arrow 2", ">=angle 90"},
[3] = {"gp arrow 3", ">=angle 60"},
[4] = {"gp arrow 4", ">=angle 45"},
[5] = {"gp arrow 5", ">=o"},
[6] = {"gp arrow 6", ">=*"},
[7] = {"gp arrow 7", ">=diamond"},
[8] = {"gp arrow 8", ">=open diamond"},
[9] = {"gp arrow 9", ">={]}"},
[10] = {"gp arrow 10", ">={[}"},
[11] = {"gp arrow 11", ">=)"},
[12] = {"gp arrow 12", ">=("}
}
-- plot styles are corresponding with linetypes and must have the same number of entries
-- see option 'tikzplot' for usage
pgf.styles.plotstyles_axes = {
[1] = {"gp plot axes", ""},
[2] = {"gp plot border", ""},
}
pgf.styles.plotstyles = {
[1] = {"gp plot 0", "smooth"},
[2] = {"gp plot 1", "smooth"},
[3] = {"gp plot 2", "smooth"},
[4] = {"gp plot 3", "smooth"},
[5] = {"gp plot 4", "smooth"},
[6] = {"gp plot 5", "smooth"},
[7] = {"gp plot 6", "smooth"},
[8] = {"gp plot 7", "smooth"}
}
pgf.styles.linetypes_axes = {
[1] = {"gp lt axes", "dotted"}, -- An lt of -1 is used for the X and Y axes.
[2] = {"gp lt border", "solid"}, -- An lt of -2 is used for the border of the plot.
[3] = {"gp lt nodraw", "solid"}, -- Although -3 mean don't draw, we still fill this up.
[4] = {"gp lt background", "solid"} -- lt bgnd
}
pgf.styles.linetypes = {
[1] = {"gp lt plot 0", ""}, -- first graph
[2] = {"gp lt plot 1", ""}, -- second ...
[3] = {"gp lt plot 2", ""},
[4] = {"gp lt plot 3", ""},
[5] = {"gp lt plot 4", ""},
[6] = {"gp lt plot 5", ""},
[7] = {"gp lt plot 6", ""},
[8] = {"gp lt plot 7", ""}
}
pgf.styles.dashtypes = {
[1] = {"gp dt 1", "solid"},
[2] = {"gp dt 2", "dash pattern=on 7.5*\\gpdashlength off 7.5*\\gpdashlength"},
[3] = {"gp dt 3", "dash pattern=on 3.75*\\gpdashlength off 5.625*\\gpdashlength"},
[4] = {"gp dt 4", "dash pattern=on 1*\\gpdashlength off 2.8125*\\gpdashlength"},
[5] = {"gp dt 5", "dash pattern=on 11.25*\\gpdashlength off 3.75*\\gpdashlength on 1*\\gpdashlength off 3.75*\\gpdashlength"},
[6] = {"gp dt 6", "dash pattern=on 5.625*\\gpdashlength off 5.625*\\gpdashlength on 1*\\gpdashlength off 5.625*\\gpdashlength"},
[7] = {"gp dt 7", "dash pattern=on 3.75*\\gpdashlength off 3.75*\\gpdashlength on 3.75*\\gpdashlength off 11.25*\\gpdashlength"},
[8] = {"gp dt 8", "dash pattern=on 1*\\gpdashlength off 3.75*\\gpdashlength on 11.25*\\gpdashlength off 3.75*\\gpdashlength on 1*\\gpdashlength off 3.75*\\gpdashlength"}
}
-- corresponds to pgf.styles.linetypes
pgf.styles.lt_colors_axes = {
[1] = {"gp lt color axes", "black!30"},
[2] = {"gp lt color border", "black"},
}
pgf.styles.lt_colors = {
[1] = {"gp lt color 0", "red"},
[2] = {"gp lt color 1", "green"},
[3] = {"gp lt color 2", "blue"},
[4] = {"gp lt color 3", "magenta"},
[5] = {"gp lt color 4", "cyan"},
[6] = {"gp lt color 5", "yellow"},
[7] = {"gp lt color 6", "orange"},
[8] = {"gp lt color 7", "purple"}
}
pgf.styles.patterns = {
[1] = {"gp pattern 0", "white"},
[2] = {"gp pattern 1", "pattern=north east lines"},
[3] = {"gp pattern 2", "pattern=north west lines"},
[4] = {"gp pattern 3", "pattern=crosshatch"},
[5] = {"gp pattern 4", "pattern=grid"},
[6] = {"gp pattern 5", "pattern=vertical lines"},
[7] = {"gp pattern 6", "pattern=horizontal lines"},
[8] = {"gp pattern 7", "pattern=dots"},
[9] = {"gp pattern 8", "pattern=crosshatch dots"},
[10] = {"gp pattern 9", "pattern=fivepointed stars"},
[11] = {"gp pattern 10", "pattern=sixpointed stars"},
[12] = {"gp pattern 11", "pattern=bricks"}
}
pgf.styles.plotmarks = {
[1] = {"gp mark 0", "mark size=.5\\pgflinewidth,mark=*"}, -- point (-1)
[2] = {"gp mark 1", "mark=+"},
[3] = {"gp mark 2", "mark=x"},
[4] = {"gp mark 3", "mark=star"},
[5] = {"gp mark 4", "mark=square"},
[6] = {"gp mark 5", "mark=square*"},
[7] = {"gp mark 6", "mark=o"},
[8] = {"gp mark 7", "mark=*"},
[9] = {"gp mark 8", "mark=triangle"},
[10] = {"gp mark 9", "mark=triangle*"},
[11] = {"gp mark 10", "mark=triangle,every mark/.append style={rotate=180}"},
[12] = {"gp mark 11", "mark=triangle*,every mark/.append style={rotate=180}"},
[13] = {"gp mark 12", "mark=diamond"},
[14] = {"gp mark 13", "mark=diamond*"},
[15] = {"gp mark 14", "mark=otimes"},
[16] = {"gp mark 15", "mark=oplus"}
}
--[[===============================================================================================
helper functions
]]--===============================================================================================
-- from the Lua wiki
explode = function(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
local trim = function(s) return (string.gsub(s,"^%s*(.-)%s*$", "%1")) end
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr, trim(string.sub(str,pos,st-1))) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr, trim(string.sub(str,pos))) -- Attach chars right of last divider
return arr
end
--[[===============================================================================================
The PGF/TikZ output routines
]]--===============================================================================================
pgf.transform_xcoord = function(coord)
return (coord+gfx.origin_xoffset)*gfx.scalex
end
pgf.transform_ycoord = function(coord)
return (coord+gfx.origin_yoffset)*gfx.scaley
end
pgf.format_coord = function(xc, yc)
return string.format("%.3f,%.3f", pgf.transform_xcoord(xc), pgf.transform_ycoord(yc))
end
pgf.write_doc_begin = function(preamble)
gp.write(gfx.format[gfx.opt.tex_format].docheader)
gp.write(preamble)
gp.write(gfx.format[gfx.opt.tex_format].begindocument)
end
pgf.write_doc_end = function()
gp.write(gfx.format[gfx.opt.tex_format].enddocument)
end
pgf.write_graph_begin = function (font, noenv)
local global_opt = "" -- unused
if gfx.opt.full_doc then
gp.write(gfx.format[gfx.opt.tex_format].beforetikzpicture)
end
if noenv then
gp.write("%% ") -- comment out
end
gp.write(string.format("%s[gnuplot%s]\n", gfx.format[gfx.opt.tex_format].begintikzpicture, global_opt))
gp.write(string.format("%%%% generated with GNUPLOT %sp%s (%s; terminal rev. %s, script rev. %s)\n",
term.gp_version, term.gp_patchlevel, _VERSION, string.sub(term.lua_term_revision,7,-3), pgf.REVISION))
if not gfx.opt.notimestamp then
gp.write(string.format("%%%% %s\n", os.date()))
end
if font ~= "" then
gp.write(string.format("\\tikzset{every node/.append style={font=%s}}\n", font))
end
if gfx.opt.fontscale ~= nil then
gp.write(string.format("\\tikzset{every node/.append style={scale=%.2f}}\n", gfx.opt.fontscale))
end
if not gfx.opt.lines_colored then
gp.write("\\gpmonochromelines\n")
end
if gfx.opt.bgcolor ~= nil then
gp.write(string.format("\\gpsetbgcolor{%.3f,%.3f,%.3f}\n", gfx.opt.bgcolor[1], gfx.opt.bgcolor[2], gfx.opt.bgcolor[3]))
end
if gfx.opt.dashlength ~= nil then
gp.write(string.format("\\def\\gpdashlength{%.2f\\pgflinewidth}\n", gfx.opt.dashlength))
end
end
pgf.write_graph_end = function(noenv)
if noenv then
gp.write("%% ") -- comment out
end
if gfx.opt.full_doc then
gp.write(gfx.format[gfx.opt.tex_format].beforeendtikzpicture)
end
gp.write(gfx.format[gfx.opt.tex_format].endtikzpicture .. "\n")
if gfx.opt.full_doc then
gp.write(gfx.format[gfx.opt.tex_format].aftertikzpicture)
end
end
pgf.draw_path = function(t)
local use_plot = false
local c_str = '--'
-- is the current linetype in the list of plots?
if #gfx.opt.plot_list > 0 then
for k, v in pairs(gfx.opt.plot_list) do
if gfx.linetype_idx_set == v then
use_plot = true
c_str = ' '
break
end
end
end
gp.write("\\draw[gp path")
if gfx.opacity < 1.0 then
gp.write(string.format(",opacity=%.3f", gfx.opacity))
end
gp.write("] ")
if use_plot then
gp.write("plot["..pgf.styles.plotstyles[((gfx.linetype_idx_set) % #pgf.styles.plotstyles)+1][1].."] coordinates {")
end
gp.write("("..pgf.format_coord(t[1][1], t[1][2])..")")
for i = 2,#t-1 do
-- pretty printing
if (i % 6) == 0 then
gp.write("%\n ")
end
gp.write(c_str.."("..pgf.format_coord(t[i][1], t[i][2])..")")
end
if (#t % 6) == 0 then
gp.write("%\n ")
end
-- check for a cyclic path
if (t[1][1] == t[#t][1]) and (t[1][2] == t[#t][2]) and (not use_plot) then
gp.write("--cycle")
else
gp.write(c_str.."("..pgf.format_coord(t[#t][1], t[#t][2])..")")
end
if use_plot then
gp.write("}")
end
gp.write(";\n")
end
pgf.draw_arrow = function(t, direction, headstyle)
gp.write("\\draw[gp path")
if direction ~= '' and direction ~= nil then
gp.write(","..direction)
end
if headstyle > 0 then
gp.write(",gp arrow "..headstyle)
end
if gfx.opacity < 1.0 then
gp.write(string.format(",opacity=%.3f", gfx.opacity))
end
gp.write("]")
gp.write("("..pgf.format_coord(t[1][1], t[1][2])..")")
for i = 2,#t do
if (i % 6) == 0 then
gp.write("%\n ")
end
gp.write("--("..pgf.format_coord(t[i][1], t[i][2])..")")
end
gp.write(";\n")
end
pgf.draw_points = function(t, pm)
gp.write("\\gppoint{"..pm.."}{")
for i,v in ipairs(t) do
gp.write("("..pgf.format_coord(v[1], v[2])..")")
end
gp.write("}\n")
end
pgf.set_linetype = function(linetype)
gp.write("\\gpsetlinetype{"..linetype.."}\n")
end
pgf.set_dashtype = function(dashtype)
gp.write("\\gpsetdashtype{"..dashtype.."}\n")
end
pgf.set_color = function(color)
gp.write("\\gpcolor{"..color.."}\n")
end
pgf.set_linewidth = function(width)
gp.write(string.format("\\gpsetlinewidth{%.2f}\n", width))
end
pgf.set_pointsize = function(size)
gp.write(string.format("\\gpsetpointsize{%.2f}\n", 4*size))
end
pgf.write_text_node = function(t, text, angle, justification, font)
local node_options = justification
if angle ~= 0 then
node_options = node_options .. ",rotate=" .. angle
end
if font ~= '' then
node_options = node_options .. ",font=" .. font
end
if gfx.opacity < 1.0 then
node_options = node_options .. string.format(",text opacity=%.3f", gfx.opacity)
end
gp.write(string.format("\\node[%s] at (%s) {%s};\n",
node_options, pgf.format_coord(t[1], t[2]), text))
end
pgf.draw_fill = function(t, pattern, color, saturation, opacity)
local fill_path = ''
local fill_style = color
if saturation < 100 then
fill_style = fill_style .. ",color=.!"..saturation;
end
fill_path = fill_path .. '('..pgf.format_coord(t[1][1], t[1][2])..')'
-- draw 2nd to n-1 corners
for i = 2,#t-1 do
if (i % 5) == 0 then
-- pretty printing
fill_path = fill_path .. "%\n "
end
fill_path = fill_path .. '--('..pgf.format_coord(t[i][1], t[i][2])..')'
end
if (#t % 5) == 0 then
gp.write("%\n ")
end
-- draw last corner
-- 'cycle' is just for the case that we want to draw a
-- line around the filled area
if (t[1][1] == t[#t][1]) and (t[1][2] == t[#t][2]) then -- cyclic
fill_path = fill_path .. '--cycle'
else
fill_path = fill_path
.. '--('..pgf.format_coord(t[#t][1], t[#t][2])..')--cycle'
end
if pattern == '' then
-- solid fills
-- fill_style = 'color='..color
if opacity < 100 then
fill_style = fill_style..string.format(",opacity=%.2f", opacity/100)
else
-- fill_style = "" -- color ?
end
else
-- pattern fills
fill_style = fill_style..','..pattern..',pattern color=.'
end
local out = ''
if (pattern ~= '') and (opacity == 100) then
-- have to fill bg for opaque patterns
gp.write("\\def\\gpfillpath{"..fill_path.."}\n"
.. "\\gpfill{color=gpbgfillcolor} \\gpfillpath;\n"
.. "\\gpfill{"..fill_style.."} \\gpfillpath;\n")
else
gp.write("\\gpfill{"..fill_style.."} "..fill_path..";\n")
end
end
pgf.load_image_file = function(ll, ur, xfile)
gp.write(string.format("\\gploadimage{%.3f}{%.3f}{%.3f}{%.3f}{%s}\n",
pgf.transform_xcoord(ll[1]), pgf.transform_ycoord(ll[2]),
(pgf.transform_xcoord(ur[1]) - pgf.transform_xcoord(ll[1])),
(pgf.transform_ycoord(ur[2]) - pgf.transform_ycoord(ll[2])),
xfile))
end
pgf.draw_raw_rgb_image = function(t, m, n, ll, ur, xfile)
local gw = gp.write
local sf = string.format
local xs = sf("%.3f", pgf.transform_xcoord(ur[1]) - pgf.transform_xcoord(ll[1]))
local ys = sf("%.3f", pgf.transform_ycoord(ur[2]) - pgf.transform_ycoord(ll[2]))
gw("\\def\\gprawrgbimagedata{%\n ")
for cnt = 1,#t do
gw(sf("%02x%02x%02x", 255*t[cnt][1]+0.5, 255*t[cnt][2]+0.5, 255*t[cnt][3]+0.5))
if (cnt % 16) == 0 then
gw("%\n ")
end
end
gw("}%\n")
gw("\\gprawimage{rgb}{"..sf("%.3f", pgf.transform_xcoord(ll[1])).."}"
.."{"..sf("%.3f", pgf.transform_ycoord(ll[2])).."}"
.."{"..m.."}{"..n.."}{"..xs.."}{"..ys.."}{\\gprawrgbimagedata}{"..xfile.."}\n")
end
pgf.draw_raw_cmyk_image = function(t, m, n, ll, ur, xfile)
local gw = gp.write
local sf = string.format
local min = math.min
local max = math.max
local mf = math.floor
local UCRBG = {1,1,1,1} -- default corrections
local rgb2cmyk255 = function(r,g,b)
local c = 1-r
local m = 1-g
local y = 1-b
local k = min(c,m,y)
c = mf(255*min(1, max(0, c - UCRBG[1]*k))+0.5)
m = mf(255*min(1, max(0, m - UCRBG[2]*k))+0.5)
y = mf(255*min(1, max(0, y - UCRBG[3]*k))+0.5)
k = mf(255*min(1, max(0, UCRBG[4]*k))+0.5)
return c,m,y,k
end
local xs = sf("%.3f", pgf.transform_xcoord(ur[1]) - pgf.transform_xcoord(ll[1]))
local ys = sf("%.3f", pgf.transform_ycoord(ur[2]) - pgf.transform_ycoord(ll[2]))
gw("\\def\\gprawcmykimagedata{%\n ")
for cnt = 1,#t do
gw(sf("%02x%02x%02x%02x", rgb2cmyk255(t[cnt][1],t[cnt][2],t[cnt][3])))
if (cnt % 12) == 0 then
gw("%\n ")
end
end
gw("}%\n")
gw("\\gprawimage{cmyk}{"..sf("%.3f", pgf.transform_xcoord(ll[1])).."}"
.."{"..sf("%.3f", pgf.transform_ycoord(ll[2])).."}"
.."{"..m.."}{"..n.."}{"..xs.."}{"..ys.."}{\\gprawcmykimagedata}{"..xfile.."}\n")
end
pgf.write_clipbox_begin = function (ll, ur)
gp.write(gfx.format[gfx.opt.tex_format].beginscope.."\n")
gp.write(string.format("\\clip (%s) rectangle (%s);\n",
pgf.format_coord(ll[1],ll[2]),pgf.format_coord(ur[1],ur[2])))
end
pgf.write_clipbox_end = function()
gp.write(gfx.format[gfx.opt.tex_format].endscope.."\n")
end
pgf.write_boundingbox = function(t, num)
gp.write("%% coordinates of the plot area\n")
gp.write("\\gpdefrectangularnode{gp plot "..num.."}{"
..string.format("\\pgfpoint{%.3fcm}{%.3fcm}", pgf.transform_xcoord(t.xleft), pgf.transform_ycoord(t.ybot)).."}{"
..string.format("\\pgfpoint{%.3fcm}{%.3fcm}", pgf.transform_xcoord(t.xright), pgf.transform_ycoord(t.ytop)).."}\n")
end
pgf.write_variables = function(t)
gp.write("%% gnuplot variables\n")
for k, v in pairs(t) do
gp.write(string.format("\\gpsetvar{%s}{%s}\n",k,v))
end
end
-- write style to seperate file, or whatever...
pgf.create_style = function()
local name_common = pgf.STYLE_FILE_BASENAME.."-common.tex"
local name_latex = pgf.STYLE_FILE_BASENAME..".sty"
local name_tex = pgf.STYLE_FILE_BASENAME..".tex"
local name_context = "t-"..pgf.STYLE_FILE_BASENAME..".tex"
-- LaTeX
local f_latex = io.open(name_latex, "w+")
f_latex:write([[
%%
%% LaTeX wrapper for gnuplot-tikz style file
%%
\NeedsTeXFormat{LaTeX2e}
]])
f_latex:write("\\ProvidesPackage{"..pgf.STYLE_FILE_BASENAME.."}%\n")
f_latex:write(" ["..pgf.REVISION_DATE.." (rev. "..pgf.REVISION..") GNUPLOT Lua terminal style]\n\n")
f_latex:write([[
\RequirePackage{tikz}
\usetikzlibrary{arrows,patterns,plotmarks,backgrounds}
]])
f_latex:write("\\input "..name_common.."\n")
f_latex:write([[
\endinput
]])
f_latex:close()
-- ConTeXt
local f_context = io.open(name_context, "w+")
f_context:write([[
%%
%% ConTeXt wrapper for gnuplot-tikz style file
%%
\usemodule[tikz]
\usetikzlibrary[arrows,patterns,plotmarks,backgrounds]
\edef\tikzatcode{\the\catcode`\@}
\edef\tikzbarcode{\the\catcode`\|}
\edef\tikzexclaimcode{\the\catcode`\!}
\catcode`\@=11
\catcode`\|=12
\catcode`\!=12
]])
f_context:write("\\input "..name_common.."\n")
f_context:write([[
\catcode`\@=\tikzatcode
\catcode`\|=\tikzbarcode
\catcode`\!=\tikzexclaimcode
\endinput
]])
f_context:close()
-- plain TeX
local f_tex = io.open(name_tex, "w+")
f_tex:write([[
%%
%% plain TeX wrapper for gnuplot-tikz style file
%%
\input tikz.tex
\usetikzlibrary{arrows,patterns,plotmarks,backgrounds}
\edef\tikzatcode{\the\catcode`\@}
\catcode`\@=11
]])
f_tex:write("\\input "..name_common.."\n\n")
f_tex:write([[
\catcode`\@=\tikzatcode
\endinput
]])
f_tex:close()
-- common
local f = io.open(name_common, "w+")
f:write([[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Common style file for TeX, LaTeX and ConTeXt
%%
%% It is associated with the 'gnuplot.lua' script, and usually generated
%% automatically. So take care whenever you make any changes!
%%
% check for the correct TikZ version
\def\gpchecktikzversion#1.#2\relax{%
\ifnum#1<2%
\PackageError{gnuplot-lua-tikz}{PGF/TikZ version >= 2.0 is required, but version \pgfversion\space was found}{}%
\fi}
\expandafter\gpchecktikzversion\pgfversion\relax
% FIXME: is there a more elegant way to determine the output format?
\def\pgfsysdriver@a{pgfsys-dvi.def} % ps
\def\pgfsysdriver@b{pgfsys-dvipdfm.def} % pdf
\def\pgfsysdriver@c{pgfsys-dvipdfmx.def} % pdf
\def\pgfsysdriver@d{pgfsys-dvips.def} % ps
\def\pgfsysdriver@e{pgfsys-pdftex.def} % pdf
\def\pgfsysdriver@f{pgfsys-tex4ht.def} % html
\def\pgfsysdriver@g{pgfsys-textures.def} % ps
\def\pgfsysdriver@h{pgfsys-vtex.def} % ps
\def\pgfsysdriver@i{pgfsys-xetex.def} % pdf
\newif\ifgppdfout\gppdfoutfalse
\newif\ifgppsout\gppsoutfalse
\ifx\pgfsysdriver\pgfsysdriver@a
\gppsouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@b
\gppdfouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@c
\gppdfouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@d
\gppsouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@e
\gppdfouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@f
% tex4ht
\else\ifx\pgfsysdriver\pgfsysdriver@g
\gppsouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@h
\gppsouttrue
\else\ifx\pgfsysdriver\pgfsysdriver@i
\gppdfouttrue
\fi\fi\fi\fi\fi\fi\fi\fi\fi
% uncomment the following lines to make font values "appendable"
% and if you are really sure about that ;-)
% \pgfkeyslet{/tikz/font/.@cmd}{\undefined}
% \tikzset{font/.initial={}}
% \def\tikz@textfont{\pgfkeysvalueof{/tikz/font}}
%
% image related stuff
%
\def\gp@rawimage@pdf#1#2#3#4#5#6{%
\def\gp@tempa{cmyk}%
\def\gp@tempb{#1}%
\ifx\gp@tempa\gp@tempb%
\def\gp@temp{/CMYK}%
\else%
\def\gp@temp{/RGB}%
\fi%
\pgf@sys@bp{#4}\pgfsysprotocol@literalbuffered{0 0}\pgf@sys@bp{#5}%
\pgfsysprotocol@literalbuffered{0 0 cm}%
\pgfsysprotocol@literalbuffered{BI /W #2 /H #3 /CS \gp@temp}%
\pgfsysprotocol@literalbuffered{/BPC 8 /F /AHx ID}%
\pgfsysprotocol@literal{#6 > EI}%
}
\def\gp@rawimage@ps#1#2#3#4#5#6{%
\def\gp@tempa{cmyk}%
\def\gp@tempb{#1}%
\ifx\gp@tempa\gp@tempb%
\def\gp@temp{4}%
\else%
\def\gp@temp{3}%
\fi%
\pgfsysprotocol@literalbuffered{0 0 translate}%
\pgf@sys@bp{#4}\pgf@sys@bp{#5}\pgfsysprotocol@literalbuffered{scale}%
\pgfsysprotocol@literalbuffered{#2 #3 8 [#2 0 0 -#3 0 #3]}%
\pgfsysprotocol@literalbuffered{currentfile /ASCIIHexDecode filter}%
\pgfsysprotocol@literalbuffered{false \gp@temp\space colorimage}%
\pgfsysprotocol@literal{#6 >}%
}
\def\gp@rawimage@html#1#2#3#4#5#6{%
% FIXME: print a warning message here
}
\ifgppdfout
\def\gp@rawimage{\gp@rawimage@pdf}
\else
\ifgppsout
\def\gp@rawimage{\gp@rawimage@ps}
\else
\def\gp@rawimage{\gp@rawimage@html}
\fi
\fi
\def\gploadimage#1#2#3#4#5{%
\pgftext[left,bottom,x=#1cm,y=#2cm] {\pgfimage[interpolate=false,width=#3cm,height=#4cm]{#5}};%
}
\def\gp@set@size#1{%
\def\gp@image@size{#1}%
}
\def\gp@rawimage@#1#2#3#4#5#6#7#8{
\tikz@scan@one@point\gp@set@size(#6,#7)\relax%
\tikz@scan@one@point\pgftransformshift(#2,#3)\relax%
\pgftext {%
\pgfsys@beginpurepicture%
\gp@image@size% fill \pgf@x and \pgf@y
\gp@rawimage{#1}{#4}{#5}{\pgf@x}{\pgf@y}{#8}%
\pgfsys@endpurepicture%
}%
}
%% \gprawimage{color model}{xcoord}{ycoord}{# of xpixel}{# of ypixel}{xsize}{ysize}{rgb/cmyk hex data RRGGBB/CCMMYYKK ...}{file name}
%% color model is 'cmyk' or 'rgb' (default)
\def\gprawimage#1#2#3#4#5#6#7#8#9{%
\ifx	&%
\gp@rawimage@{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}
\else
\ifgppsout
\gp@rawimage@{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}
\else
\gploadimage{#2}{#3}{#6}{#7}{#9}
\fi
\fi
}
%
% gnuplottex comapatibility
% (see http://www.ctan.org/tex-archive/help/Catalogue/entries/gnuplottex.html)
%
\def\gnuplottexextension@lua{\string tex}
\def\gnuplottexextension@tikz{\string tex}
%
% gnuplot variables getter and setter
%
\def\gpsetvar#1#2{%
\expandafter\xdef\csname gp@var@#1\endcsname{#2}
}
\def\gpgetvar#1{%
\csname gp@var@#1\endcsname %
}
%
% some wrapper code
%
% short for a filled path
\def\gpfill#1{\path[line width=0.1\gpbaselw,draw,fill,#1]}
% short for changing the line width
\def\gpsetlinewidth#1{\pgfsetlinewidth{#1\gpbaselw}}
% short for changing the line type
\def\gpsetlinetype#1{\tikzset{gp path/.style={#1,#1 add}}}
% short for changing the dash pattern
\def\gpsetdashtype#1{\tikzset{gp path/.append style={#1}}}
% short for changing the point size
\def\gpsetpointsize#1{\tikzset{gp point/.style={mark size=#1\gpbasems}}}
% wrapper for color settings
\def\gpcolor#1{\tikzset{global #1}}
\tikzset{rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\tikzset{color=.}}}
\tikzset{global rgb color/.code={\pgfutil@definecolor{.}{rgb}{#1}\pgfutil@color{.}}}
\tikzset{global color/.code={\pgfutil@color{#1}}}
% prevent plot mark distortions due to changes in the PGF transformation matrix
% use `\gpscalepointstrue' and `\gpscalepointsfalse' for enabling and disabling
% point scaling
%
\newif\ifgpscalepoints
\tikzset{gp shift only/.style={%
\ifgpscalepoints\else shift only\fi%
}}
\def\gppoint#1#2{%
\path[solid] plot[only marks,gp point,mark options={gp shift only},#1] coordinates {#2};%
}
%
% char size calculation, that might be used with gnuplottex
%
% Example code (needs gnuplottex.sty):
%
% % calculate the char size when the "gnuplot" style is used
% \tikzset{gnuplot/.append style={execute at begin picture=\gpcalccharsize}}
%
% \tikzset{gnuplot/.append style={font=\ttfamily\footnotesize}}
%
% \begin{tikzpicture}[gnuplot]
% \begin{gnuplot}[terminal=lua,%
% terminaloptions={tikz solid nopic charsize \the\gphcharsize,\the\gpvcharsize}]
% test
% \end{gnuplot}
% \end{tikzpicture}
%
%%%
% The `\gpcalccharsize' command fills the lengths \gpvcharsize and \gphcharsize with
% the values of the current default font used within nodes and is meant to be called
% within a tikzpicture environment.
%
\newdimen\gpvcharsize
\newdimen\gphcharsize
\def\gpcalccharsize{%
\pgfinterruptboundingbox%
\pgfsys@begininvisible%
\node at (0,0) {%
\global\gphcharsize=1.05\fontcharwd\font`0%
\global\gpvcharsize=1.05\fontcharht\font`0%
\global\advance\gpvcharsize by 1.05\fontchardp\font`g%
};%
\pgfsys@endinvisible%
\endpgfinterruptboundingbox%
}
%
% define a rectangular node in tikz e.g. for the plot area
%
% #1 node name
% #2 coordinate of "south west"
% #3 coordinate of "north east"
%
\def\gpdefrectangularnode#1#2#3{%
\expandafter\gdef\csname pgf@sh@ns@#1\endcsname{rectangle}
\expandafter\gdef\csname pgf@sh@np@#1\endcsname{%
\def\southwest{#2}%
\def\northeast{#3}%
}
\pgfgettransform\pgf@temp%
% once it is defined, no more transformations will be applied, I hope
\expandafter\xdef\csname pgf@sh@nt@#1\endcsname{\pgf@temp}%
\expandafter\xdef\csname pgf@sh@pi@#1\endcsname{\pgfpictureid}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% You may want to adapt the following to fit your needs (in your
%% individual style file and/or within your document).
%%
%
% style for every plot
%
\tikzset{gnuplot/.style={%
>=stealth',%
line cap=round,%
line join=round,%
}}
\tikzset{gp node left/.style={anchor=mid west,yshift=-.12ex}}
\tikzset{gp node center/.style={anchor=mid,yshift=-.12ex}}
\tikzset{gp node right/.style={anchor=mid east,yshift=-.12ex}}
% basic plot mark size (points)
\newdimen\gpbasems
\gpbasems=.4pt
% basic linewidth
\newdimen\gpbaselw
\gpbaselw=.4pt
% this is the default color for pattern backgrounds
\colorlet{gpbgfillcolor}{white}
% set background color and fill color
\def\gpsetbgcolor#1{%
\pgfutil@definecolor{gpbgfillcolor}{rgb}{#1}%
\tikzset{tight background,background rectangle/.style={fill=gpbgfillcolor},show background rectangle}%
}
% this should reverse the normal text node presets, for the
% later referencing as described below
\tikzset{gp refnode/.style={coordinate,yshift=.12ex}}
% to add an empty label with the referenceable name "my node"
% to the plot, just add the following line to your gnuplot
% file:
%
% set label "" at 1,1 font ",gp refnode,name=my node"
%
% enlargement of the bounding box in standalone mode (only used by LaTeX/ConTeXt)
\def\gpbboxborder{0mm}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% The following TikZ-styles are derived from the 'pgf.styles.*' tables
%% in the Lua script.
%% To change the number of used styles you should change them there and
%% regenerate this style file.
%%
]])
f:write("% arrow styles settings\n")
for i = 1, #pgf.styles.arrows do
f:write("\\tikzset{"..pgf.styles.arrows[i][1].."/.style={"..pgf.styles.arrows[i][2].."}}\n")
end
f:write("\n% plotmark settings\n")
for i = 1, #pgf.styles.plotmarks do
f:write("\\tikzset{"..pgf.styles.plotmarks[i][1].."/.style={"..pgf.styles.plotmarks[i][2].."}}\n")
end
f:write("\n% pattern settings\n")
for i = 1, #pgf.styles.patterns do
f:write("\\tikzset{"..pgf.styles.patterns[i][1].."/.style={"..pgf.styles.patterns[i][2].."}}\n")
end
f:write("\n% if the 'tikzplot' option is used the corresponding lines will be smoothed by default\n")
for i = 1, #pgf.styles.plotstyles_axes do
f:write("\\tikzset{"..pgf.styles.plotstyles_axes[i][1].."/.style="..pgf.styles.plotstyles_axes[i][2].."}\n")
end
for i = 1, #pgf.styles.plotstyles do
f:write("\\tikzset{"..pgf.styles.plotstyles[i][1].."/.style="..pgf.styles.plotstyles[i][2].."}\n")
end
-- line styles for borders etc ...
f:write("\n% linestyle settings\n")
for i = 1, #pgf.styles.linetypes_axes do
f:write("\\tikzset{"..pgf.styles.linetypes_axes[i][1].."/.style="..pgf.styles.linetypes_axes[i][2].."}\n")
end
f:write("\n% linestyle \"addon\" settings for overwriting a default linestyle within the\n")
f:write("% TeX document via eg. \\tikzset{gp lt plot 1 add/.style={fill=black,draw=none}} etc.\n")
for i = 1, #pgf.styles.linetypes_axes do
f:write("\\tikzset{"..pgf.styles.linetypes_axes[i][1].." add/.style={}}\n")
end
for i = 1, #pgf.styles.linetypes do
f:write("\\tikzset{"..pgf.styles.linetypes[i][1].." add/.style={}}\n")
end
--[[ The line types. These don't do anything anymore, since we have the dash types.
But they were use to hook in from TikZ and change the respective styles
from the LaTeX script. We keep those definitions for backward compatibility.
]]--
for i = 1, #pgf.styles.linetypes do
f:write("\\tikzset{"..pgf.styles.linetypes[i][1].."/.style={"..pgf.styles.linetypes[i][2].."}}\n")
end
f:write("\n% linestyle color settings\n")
for i = 1, #pgf.styles.lt_colors_axes do
f:write("\\colorlet{"..pgf.styles.lt_colors_axes[i][1].."}{"..pgf.styles.lt_colors_axes[i][2].."}\n")
end
-- dash styles for the plots
f:write("\n% dash type settings\n")
f:write("% Define this as a macro so that the dash patterns expand later with the current \\pgflinewidth.\n")
f:write("\\def\\gpdashlength{\\pgflinewidth}\n")