Debian Codegraph @

texmf-dist/source/luatex/luaaddplot/luaaddplot.dtx

raw
1 % \iffalse meta-comment
2 %
3 % $Id: luaaddplot.dtx 6 2024-09-22 00:02:45Z reinhard $
4 %
5 %<*driver>
6 \documentclass[a4paper,11pt]{ltxdoc}
7 \MakeShortVerb{\|}
8
9 \advance\textheight by 2\baselineskip
10 \usepackage[english]{babel}
11 \usepackage{tgpagella}
12 \usepackage[dvipsnames]{xcolor}
13 \usepackage{parskip}
14 \usepackage{siunitx}
15 \usepackage[colorlinks,bookmarksopen,bookmarksnumbered]{hyperref}
16 \hypersetup{linkcolor=BrickRed}
17
18 \author{Reinhard Kotucha}
19 \title{\textsf{luaaddplot}}
20 \date{Version 1.0a\\[.5ex]\today}
21 \begin{document}
22 \maketitle
23 \vspace*{1cm}
24
25 \begin{abstract}
26 \parindent=0pt \parskip=1ex
27 \noindent The \textsf{pgfplots} package supports plotting data from files.
28 If these files are generated by measuring devices they almost always
29 have to be pre-processed.
30
31 This packge provides a macro |\luaaddplot| which extends the
32 functionality of |\addplot| by an optional argument containing
33 a $\lambda$- expression which is evaluated for each line of the data
34 file.
35
36 The package requires Lua\TeX\ or Lua\LaTeX.
37 \end{abstract}
38 \vspace*{1cm}
39 \tableofcontents
40 \vspace*{\fill}
41
42 \newpage
43
44 \section{Introduction}
45 When plotting files generated by measuring devices the data files
46 often have to be pre-processed before they can be passed to \textsf{pgfplots}.
47 In most cases a header describing the device configuration must be
48 removed, column delimiters have to be changed, and units have to be
49 scaled (\si{\hertz} to \si{\mega\hertz}, \si{\volt} to
50 \si{\micro\volt}, etc\ldots). In some cases more complex operations
51 are required.
52
53 \subsection{Usage}
54
55 \verb| \luaaddplot|\oarg{options}| file "|%
56 $\langle$\textit{filename}$\rangle$|"|[|, |$\langle$\textit{$\lambda$-expression}$\rangle$]|;|
57
58 Everything between \cs{luaaddplot} and the keyword |file| is passed to
59 \cs{addplot}, everything between |file| and the semicolon is
60 processed by Lua and valid Lua syntax is required here. You can
61 consider code between |file| and the terminating semicolon as
62 arguments of a Lua function with braces omitted.
63
64 \subsection{Reading the data file}
65
66 Data files generated by measuring devices usually have an ASCII
67 header describing the settings. These lines are often preceded by
68 comment characters but not always. In order to avoid pre-processing
69 with another tool, all lines which do not begin with a number are
70 ignored.
71
72 Different devices use different column delimiters. Therefore tabs,
73 colons, semicolons, and commas are replaced by a space. With other
74 words, various datafile formats are converted to a to a \textsc{Matlab}
75 compatible format.
76
77 \subsection{\texorpdfstring{$\lambda$}{lambda}-Expressions}
78
79 A $\lambda$-expression is an anonymous function and looks like this:
80 \begin{verbatim}
81 function (a)
82 return a[1], a[2]
83 end
84 \end{verbatim}
85
86 The formal parameter |a| (its name can be chosen arbitrarily) is
87 replaced by an ordered list of columns for each line in the data file.
88 The return values are the $x$ and $y$ values to be plotted and are
89 passed to \cs{addplot}. Always two values must be returned. Please
90 note that the index of the first column is 1 in Lua and not 0, as in
91 many other programming languages.
92
93 In the example above the first and second column of the data file
94 will be plotted. This is the default if the $\lambda$-expression is
95 omitted.
96
97 \newpage
98
99 \section{Examples}
100
101 \subsection{Scaling}
102
103 The following is an excerpt of a data file generated by a spectrum analyzer.
104
105 \begin{verbatim}
106 x-Unit;Hz;
107 y-Unit;dBm;
108 Preamplifier;OFF;
109 Transducer;OFF;
110 Values;501;
111 0;43.660163879394531;
112 6000000;-53.616184234619141;
113 12000000;-60.31707763671875;
114 18000000;-62.548038482666016;
115 54000000;-66.722061157226563;
116 \end{verbatim}
117
118 Frequencies are in \si{\hertz} but for the plot we prefer
119 \si{\mega\hertz}. The solution is
120 \begin{verbatim}
121 \luaaddplot[blue] file "spectrum.data", function (col)
122 return col[1]/1e6, col[2]
123 end;
124 \end{verbatim}
125
126
127 \subsection{Processing}
128
129 The following is an excerpt of a data file generated by a vector
130 network analyzer.
131
132 \begin{verbatim}
133 %% Date: 2021-02-25 11:36:04
134 %% Data & Calibration Information:
135 %% Trc1:S11(Full One Port)
136 %%freq[Hz] re:Trc1_S11 im:Trc1_S11
137 6.650000000000000E+008,3.684957681171731E-001,-9.205383204111828E-001,
138 6.652500000000000E+008,3.717039523633851E-001,-9.215835251874305E-001,
139 6.655000000000000E+008,3.687706454916999E-001,-9.207422307051146E-001,
140 6.657500000000000E+008,3.574212650433284E-001,-9.176705980253278E-001,
141 6.660000000000000E+008,3.453552411257967E-001,-9.254690887689956E-001,
142 \end{verbatim}
143
144 The first column denotes the frequency in \si{\hertz}, the second and
145 third column denote the real and imaginary part of the impedance
146 $s_{11}$ respectively.
147
148 Now we want to plot
149 \begin{displaymath}
150 20 \log_{10} \sqrt{(\text{Re\,} s_{11})^2 + (\text{Im\,} s_{11})^2}
151 \end{displaymath}
152 with frequencies in \si{\mega\hertz}.
153
154 The solution is
155 \begin{verbatim}
156 \luaaddplot[green] file "s11.data",
157 function (col)
158 return col[1]/1e6, 20*math.log10(math.sqrt(col[2]^2 + col[3]^2))
159 end;
160 \end{verbatim}
161
162
163 \newpage
164 \section{Implementation}
165 \DocInput{luaaddplot.dtx}
166 \end{document}
167 %<*tex|sty|lua>
168 %</tex|sty|lua>
169
170 %</driver>
171 % \fi
172 % \begin{macrocode}
173 %<tex>%% File 'luaaddplot.tex', generated from luaaddplot.dtx'.
174 %<sty>%% File 'luaaddplot.sty', generated from luaaddplot.dtx'.
175 %<lua>-- File 'luaaddplot.lua', generated from luaaddplot.dtx'.
176 %<lua>--[[
177 %%
178 %% luaaddplot.dtx
179 %% Copyright 2022 Reinhard Kotucha <[email protected]>
180 %%
181 %% This work may be distributed and/or modified under the
182 %% conditions of the LaTeX Project Public License, either version 1.3
183 %% of this license or (at your option) any later version.
184 %% The latest version of this license is in
185 %% http://www.latex-project.org/lppl.txt
186 %% and version 1.3 or later is part of all distributions of LaTeX
187 %% version 2005/12/01 or later.
188 %%
189 %% This work has the LPPL maintenance status `maintained'.
190 %%
191 %% The Current Maintainer of this work is Reinhard Kotucha.
192 %%
193 %% This work consists of the files luaaddplot.dtx and luaaddplot.ins
194 %% and the derived files luaaddplot.tex, luaaddplot.sty, and luaaddplot.lua.
195 %<lua>--]]
196 % \end{macrocode}
197 % \subsection{luaaddplot.tex}
198 % \begin{macrocode}
199 %<*tex>
200 \directlua{require('luaaddplot')}
201 \def\luaaddplot#1file#2;{
202 \directlua{luaaddplot.opts('#1')luaaddplot.readfile(#2)}}
203 %</tex>
204 % \end{macrocode}
205 % \subsection{luaaddplot.sty}
206 % \begin{macrocode}
207 %<*sty> \ProvidesPackage{luaaddplot}[2024/09/22 v1.0a]
208 \input luaaddplot.tex
209 %</sty>
210 % \end{macrocode}
211 % \subsection{luaaddplot.lua}
212 % \begin{macrocode}
213 %<*lua>
214
215 module('luaaddplot', package.seeall)
216
217 % \end{macrocode}
218 % \DescribeMacro{readfile()}
219 % Read and pre-process file \verb|file| and apply
220 % $\lambda$-expression. If the second argument is \verb|nil| the
221 % first and second column is returned.
222 % \begin{macrocode}
223 function readfile (file, lambda)
224 % \end{macrocode}
225 % Check whether datafile exists.
226 % \begin{macrocode}
227 if not lfs.isfile(file) then
228 error('\nERROR: File "'..file..'" not found.')
229 end
230
231 local data = io.open(file)
232
233 for line in data:lines() do
234 % \end{macrocode}
235 % Remove all spaces at the beginning of a line.
236 % \begin{macrocode}
237 line = line:gsub('^%s+', '')
238 % \end{macrocode}
239 % Ignore all lines which don't begin with a number. All comments
240 % and empty lines in data files are ignored.
241 % \begin{macrocode}
242 if line:match('^%-?%.?[0-9]') then
243 % \end{macrocode}
244 % Replace possible non-space delimiters by spaces. This allows to
245 % process various datafile formats the same way as \textsc{Matlab}
246 % files without manual interaction.
247 % \begin{macrocode}
248 line = line:gsub('[\t:;,]', ' ')
249 % \end{macrocode}
250 % The result must be a table with two columns. If no $\lambda$
251 % expression is provided, the first two colums of the data file are
252 % returned. With a $\lambda$ expression as second argument arbitrary
253 % columns can be selected from a data file and can be pre-processed in
254 % any way possible.
255 % \begin{macrocode}
256 local a, b
257 local cols = line:explode (' +')
258 % \end{macrocode}
259 % Convert strings to numbers. Because $\lambda$ expressions can
260 % access any column we convert all table entries from strings
261 % to numbers, if possible, instead of only the return values.
262 % Numbers are returned to \TeX\ as floating point numbers.
263 % \begin{macrocode}
264 for i, col in ipairs(cols) do
265 cols[i] = tonumber(cols[i])
266 end
267
268 if lambda then
269 a, b = lambda(cols)
270 if a and b then
271 tex.print(string.format('%g %g', a, b))
272 end
273 else
274 tex.print(string.format('%g %g', cols[1], cols[2]))
275 end
276 end
277 end
278 tex.print('};')
279 data:close()
280 end
281 % \end{macrocode}
282 % \DescribeMacro{opts()}
283 % Pass optional arguments from \verb|\luaaddplot| to \verb|\addplot|.
284 % \begin{macrocode}
285
286 function opts (s)
287 tex.print('\\addplot'..s..' table {')
288 end
289
290 % </lua>
291 % \end{macrocode}
292
293 %\endinput
294 % Local Variables:
295 % mode: LaTeX
296 % TeX-master: t
297 % TeX-engine: luatex
298 % indent-tabs-mode: nil
299 % coding: utf-8-unix
300 % End:
301 % vim:set tabstop=2 expandtab: