summaryrefslogtreecommitdiff
path: root/tex/context/base/m-gnuplot.tex
blob: 5e612257f7fcac61cbc096220f240487e55c2327 (plain)
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
%D \module
%D   [       file=m-gnuplot,
%D        version=2006.01.04,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=\GNUPLOT\ Inclusion,
%D         author={Tobias Burnus, Hans Hagen, Taco Hoekwater, Mojca Miklavec},
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% todo: reuse (only if really needed)

% \enablemode[demo]

%D example:
%D
%D \starttyping
%D texexec --pdf --mode=demo m-gnuplot
%D \stoptyping

\unprotect

\ifx\operatingsystem\undefined \def\operatingsystem{unix} \fi % texexec will set that

\newcounter\GNUPLOTnumber

\def\startGNUPLOTinclusions
  {\bgroup
   \obeylines
   \catcode`\%=\@@letter
   \dostartGNUPLOTinclusions}

\def\dostartGNUPLOTinclusions#1\stopGNUPLOTinclusions
  {\gdef\GNUPLOTinclusions{#1}%
   \egroup}

\let\GNUPLOTinclusions\empty

\def\startGNUPLOTgraphic#1%
  {\bgroup
   \obeylines
   \catcode`\%=\@@letter
   \dostartGNUPLOTgraphic{#1}}

\def\dostartGNUPLOTgraphic#1#2\stopGNUPLOTgraphic
  {\doglobal\increment\GNUPLOTnumber
   \letgvalue{gpg:n:#1}\GNUPLOTnumber
   \setgvalue{gpg:d:\GNUPLOTnumber}{#2}%
   \egroup}

\def\useGNUPLOTgraphic
  {\dodoubleempty\douseGNUPLOTgraphic}

\def\douseGNUPLOTgraphic[#1][#2]%
  {\iffirstargument
     \@EA\dodouseGNUPLOTgraphic % like \externalfigure[name][optional args]
   \else
     \@EA\redouseGNUPLOTgraphic % like \useMPgraphic{name}
   \fi[#1][#2]}

\def\redouseGNUPLOTgraphic[#1][#2]#3%
  {\dodouseGNUPLOTgraphic[#3][]}

\newtoks\everyGNUPLOTgraphic

% gnuplot only seems to handle lf so we cannot use \mappartolineend
% also, in order to prevent fuzzy runs (or no runs) we add a ; to
% each line

\appendtoks
    \obeylines
    \def\obeyedline{;\outputnewlinechar}%
\to \everyGNUPLOTgraphic

\appendtoks
    \let\%\letterpercent
    \def\ {\letterbackslash\outputnewlinechar}%
\to \everyGNUPLOTgraphic

\def\dodouseGNUPLOTgraphic[#1][#2]%
  {\bgroup
   \doifdefined{gpg:n:#1}
     {\edef\GNUPLOTnumber{\getvalue{gpg:n:#1}}%
      \edef\GNUPLOTfile  {\bufferprefix gnuplot-\GNUPLOTnumber}%
      \bgroup
      \the\everyGNUPLOTgraphic
      \immediate\openout\scratchwrite=\GNUPLOTfile.gpd
      \immediate\write\scratchwrite{set terminal \@@GNUPLOToutput\space\@@GNUPLOToption}%
      \doifnotempty
        {\GNUPLOTinclusions}{\immediate\write\scratchwrite{\GNUPLOTinclusions}}%
      \immediate\write\scratchwrite{set output "\GNUPLOTfile.\@@GNUPLOTsuffix"}%
      \immediate\write\scratchwrite{\getvalue{gpg:d:\GNUPLOTnumber}}%
      \immediate\write\scratchwrite{quit}%
      \immediate\closeout\scratchwrite
      \egroup
      \processGNUPLOTgraphic
      \convertGNUPLOTgraphic
      \externalfigure[\@@GNUPLOTresult][#2]}%
   \egroup}

% we assume pdf inclusion, eps wil be implemented when applicable

\def\setupGNUPLOT
  {\dodoubleempty\getparameters[@@GNUPLOT]}

\def\processGNUPLOTgraphic
  {\doifelse\operatingsystem{mswin}
     {\executesystemcommand{start /MIN pgnuplot \GNUPLOTfile.gpd}} % start is needed else gp fails
     {\executesystemcommand{gnuplot \GNUPLOTfile.gpd}}}

\def\convertGNUPLOTgraphic
  {\doifsomething\@@GNUPLOThandle{\executesystemcommand{\@@GNUPLOThandle}}}

\def\defineGNUPLOThandle#1#2#3#4#5% name output suffix conversion-method result
  {\setvalue{@@GNUPLOT-#1}{{#2}{#3}{#4}{#5}}}

\def\@@GNUPLOToutput{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTmethod}41}
\def\@@GNUPLOTsuffix{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTmethod}42}
\def\@@GNUPLOThandle{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTmethod}43}
\def\@@GNUPLOTresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTmethod}44}

\defineGNUPLOThandle{postscript}{postscript}{ps} {texmfstart newpstopdf --method=raw \GNUPLOTfile.ps}{\GNUPLOTfile.pdf}
\defineGNUPLOThandle{ps}        {postscript}{ps} {texmfstart newpstopdf --method=raw \GNUPLOTfile.ps}{\GNUPLOTfile.pdf}
\defineGNUPLOThandle{pdf}       {pdf}       {pdf}{}                                                  {\GNUPLOTfile.pdf}
\defineGNUPLOThandle{metapost}  {mp}        {mp} {texmfstart mptopdf    \GNUPLOTfile.mp}             {\GNUPLOTfile-0.pdf}
\defineGNUPLOThandle{mp}        {mp}        {mp} {texmfstart mptopdf    \GNUPLOTfile.mp}             {\GNUPLOTfile-0.pdf}
\defineGNUPLOThandle{png}       {png}       {png}{}                                                  {\GNUPLOTfile.png}

\setupGNUPLOT[\c!method=ps,\c!option=]

\protect \doifnotmode{demo}{\endinput}

\starttext

\startGNUPLOTinclusions
    set title "trigonometry"
\stopGNUPLOTinclusions

\startGNUPLOTgraphic{sin}
    plot sin(x)
\stopGNUPLOTgraphic

\startGNUPLOTgraphic{cos}
    plot cos(x)
\stopGNUPLOTgraphic

\useGNUPLOTgraphic{sin} \endgraf
\useGNUPLOTgraphic{cos} \endgraf
\useGNUPLOTgraphic[cos] \endgraf

\setupGNUPLOT[method=ps]  \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
%setupGNUPLOT[method=pdf] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
\setupGNUPLOT[method=png] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
\setupGNUPLOT[method=mp]  \useGNUPLOTgraphic[sin][object=no,height=2cm,option=color] \blank

\stoptext