texlab 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,437 +0,0 @@
1
- # This is the boot file to be loaded before parsing a .texlab file
2
- # encoding: ASCII-8bit
3
- require "easystats"
4
- require "plusminus"
5
- require "to_latex"
6
- require "gnuplot"
7
- require "open3"
8
-
9
- # tweak Array
10
- class Array
11
- def extract_options!
12
- if last.is_a? Hash
13
- pop
14
- else
15
- {}
16
- end
17
- end
18
- end
19
-
20
- # tweak Hash
21
- class Hash
22
- # Object-like behavior
23
- def method_missing name, *args
24
- name_string = name.to_s
25
- case name_string[-1]
26
- when "="
27
- self[name_string[0..-2].to_sym] = args[0]
28
- when "!"
29
- self[name_string[0..-2].to_sym] = {}
30
- when "?"
31
- !! self[name_string[0..-2]]
32
- else
33
- self[name]
34
- end
35
- end
36
- end
37
-
38
- module Enumerable
39
- # map-hash
40
- def mash
41
- res = {}
42
- each do |*x|
43
- k,v = yield(*x)
44
- res[k] = v
45
- end
46
- res
47
- end
48
- end
49
-
50
- class Hash
51
- def mash! &block
52
- replace mash(&block)
53
- end
54
- end
55
-
56
-
57
- # Average method for array with uncertainty calculation
58
- class Array
59
- def average
60
- m = mean
61
- m.pm(Math.sqrt(m.delta**2 + (standard_deviation*3)**2))
62
- end
63
- end
64
-
65
-
66
- # latex generation helper
67
- require "texlab/texlabfile"
68
-
69
- $_latexfile = TexlabFile.new $_erbout
70
-
71
- def documentHeader *args
72
- $_latexfile.documentHeader *args
73
- end
74
-
75
- def env *args
76
- $_latexfile.env(*args){yield}
77
- end
78
-
79
- def puts string
80
- $_erbout << string.to_latex << "\n"
81
- end
82
-
83
- # table generation
84
- #
85
-
86
- def table title, settings={}
87
- @_table = {}
88
- yield
89
- entries = @_table
90
-
91
- unless @_tables
92
- $_latexfile.table(entries, title, settings)
93
- else
94
- @_tables << [entries, title, settings]
95
- end
96
- end
97
-
98
- def column title
99
- @_table_columns << (@_table_columns.last[title] || {})
100
-
101
- result = yield
102
- subcolumns = @_table_columns.pop
103
-
104
- unless subcolumns.empty?
105
- result = subcolumns
106
- end
107
-
108
- @_table_columns.last[title] = result
109
- end
110
-
111
- def row title
112
- @_table_columns = [@_table[title] || {}]
113
- yield
114
- @_table[title] = @_table_columns.first
115
- end
116
-
117
- def tables *args
118
- opts = args.extract_options!
119
- caption = args.shift
120
-
121
- @_tables = []
122
- yield
123
-
124
- $_latexfile.tables(@_tables, caption, opts)
125
-
126
- @_tables = nil
127
- end
128
-
129
-
130
- # Figure generation
131
- #
132
-
133
- def figure *args
134
- opts = args.extract_options!
135
- caption = args.shift
136
- format = opts[:width] ? "width=#{opts[:width]}" : opts[:format]
137
- label = opts[:label]
138
- filename = opts[:filename]
139
- if @_figures
140
- @_figures << Figure.new(format, filename, caption, label)
141
- else
142
- raise "not implemented"
143
- end
144
- end
145
-
146
- def figures *args
147
- opts = args.extract_options!
148
- placement = opts.delete(:placement) || "htbp"
149
- label = opts.delete(:label)
150
- newPageThreshold = opts.delete(:newPageThreshold) || 29
151
- caption = args.shift
152
-
153
- @_figures = []
154
- yield
155
-
156
- $_latexfile.figures(caption, @_figures, newPageThreshold, placement, label)
157
-
158
- @_figures = nil
159
- end
160
-
161
-
162
- # plotting
163
- #
164
-
165
- require "gnuplot"
166
-
167
- # override to have "w+" mode
168
- def Gnuplot.open( persist=true )
169
- cmd = Gnuplot.gnuplot( persist ) or raise 'gnuplot not found'
170
- IO::popen( cmd, "w+") { |io| yield io }
171
- end
172
-
173
- def plot *args
174
- opts = args.extract_options!
175
- title = args[0]
176
-
177
- placement = opts.delete(:placement) || (opts[:label] ? "htbp" : "H")
178
- width = opts.delete(:width) || "17cm"
179
- height = opts.delete(:height) || "10cm"
180
- cmd = opts.delete(:cmd) || "plot"
181
- label = opts.delete(:label)
182
-
183
- env :figure, "[#{placement}]" do
184
- env :center do
185
- @_datasets = []
186
- yield
187
- @_datasets
188
-
189
- gnuplot = Gnuplot.gnuplot(true) or raise "gnuplot not found"
190
- Open3.popen3(gnuplot) do |gp, out, err, external|
191
- gp <<<<-GP
192
- set terminal latex size #{width}, #{height}
193
- GP
194
-
195
- Gnuplot::Plot.new(gp, cmd) do |plot|
196
- opts.each do |key, value|
197
- case value
198
- when true
199
- plot.send key
200
- else
201
- plot.send key, value.gsub(/([\\ ])/, "\\\\\\1")
202
- end
203
- end
204
-
205
- @_datasets.each do |ds|
206
- plot.data << ds
207
- end
208
- end
209
- gp.close
210
- $_erbout << out.readlines.join("\n")
211
-
212
- if not external.value.success?
213
- errlines = err.readlines
214
- raise "could not plot:\n" + errlines.join("\n")
215
- end
216
- end
217
- end
218
- $_erbout << "\\caption{#{title}}" if title
219
- $_erbout << "\\label{#{label}}" if label
220
- end
221
- end
222
-
223
- def splot *args, &block
224
- opts = args.extract_options!
225
- opts.cmd = "splot"
226
- plot *args, opts, &block
227
- end
228
-
229
- def dataset *args
230
- opts = args.extract_options!
231
-
232
- data = args.shift
233
- data_is_expr = !!data
234
- if data
235
- # simple function
236
- ds = Gnuplot::DataSet.new data
237
- else
238
- # data
239
- @_datastrings = []
240
- yield
241
-
242
- c = @_datastrings.first.count
243
- data = (0...c).map do |i|
244
- @_datastrings.map{|t|t[i]}
245
- end
246
-
247
- ds = Gnuplot::DataSet.new data
248
- end
249
-
250
- # check args
251
- raise ArgumentError, "Unnecessary parameters: #{args}" unless args.empty?
252
-
253
- # tweak args (syntax candy)
254
- opts[:title] = nil unless data_is_expr or opts.has_key? :title
255
- if opts.key? :title and not opts[:title]
256
- opts.delete(:title)
257
- opts[:notitle] = true
258
- end
259
-
260
- # send options
261
- opts.each do |key, value|
262
- case value
263
- when true
264
- ds.send key
265
- else
266
- ds.send :"#{key}=", value
267
- end
268
- end
269
-
270
- @_datasets << ds
271
-
272
- end
273
-
274
- def datastring arg
275
- @_datastrings << arg
276
- end
277
-
278
- def data *args
279
- datastring args
280
- end
281
-
282
-
283
- # fitting
284
- def fit expr, opts={}
285
- raise "via: is a necessary parameter" if not opts[:via]
286
- via = opts[:via]
287
- vars = via.split(",")
288
-
289
- using = opts[:using]
290
-
291
- @_datasets = []
292
-
293
- dataset do
294
- yield
295
- end
296
-
297
- ds = @_datasets.first
298
-
299
- errlines = []
300
- result = {}
301
-
302
- gnuplot = Gnuplot.gnuplot(true) or raise "gnuplot not found"
303
- Open3.popen3(gnuplot) do |gp, out, err, external|
304
- vars.each do |var|
305
- gp << "#{var} = #{rand}\n"
306
- end
307
- gp <<<<-GP
308
- fit #{expr} '-' #{"using " + using if using} via #{via}
309
- GP
310
- gp << ds.to_gplot
311
- gp.close
312
-
313
- errlines = err.readlines
314
- errlines.each do |l|
315
- if l =~ /\A([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*([0-9eE.+-]+)\s*\+\/-\s*([0-9eE.+-]+)/
316
- result[$1.to_sym] = $2.to_f.pm($3.to_f)
317
- end
318
- end
319
- end
320
-
321
- vars.map do |v|
322
- result[v.to_sym] or raise "could not fit:\n" + errlines.join("\n")
323
- end
324
- end
325
-
326
-
327
- # macros
328
-
329
- $_macros={}
330
-
331
- # a macro is available in tex (\asdf) and in ruby (:asdf.to_latex or $asdf). It will go to math
332
- # mode
333
- def macro hash
334
- text_macro hash.mash{|key, value| [key, value.latex!.to_latex_math] }
335
- end
336
-
337
- # a text macro is available in tex (\asdf) and in ruby (:asdf, $asdf). It will not be
338
- # forced to math mode
339
- def text_macro hash
340
- hash.each do |key, value|
341
- define_latex_macro key, value
342
- define_symbol_macro key, value
343
- define_global_macro key, value
344
- end
345
- end
346
-
347
- def define_symbol_macro key, value
348
- $_macros[key] = value
349
- end
350
-
351
- def define_latex_macro key, value
352
- $_erbout << "\\def\\#{key}{#{value}}"
353
- end
354
-
355
- def define_global_macro key, value
356
- eval "$#{key} = '#{value.gsub("\\","\\\\").gsub("'", "\\'")}'"
357
- end
358
-
359
- class Symbol
360
- # symbol's to_latex is the macro with the same name
361
- def to_latex
362
- $_macros[self] or raise "Undefined macro: #{self}"
363
- end
364
-
365
- def + str
366
- "#{to_latex}#{str}".latex!.to_latex_math
367
- end
368
-
369
- def * unit
370
- "\\unit[\\text{#{to_latex}}]{#{unit}}".latex!.to_latex_math
371
- end
372
- end
373
-
374
-
375
-
376
-
377
- %w(
378
- alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega
379
- ).each do |a|
380
- [a, a.capitalize].each do |b|
381
- key = b.to_sym
382
- value = "\\#{b}".latex!.to_latex_math
383
- define_symbol_macro key, value
384
- define_global_macro key, value
385
- end
386
- end
387
-
388
- ('a'..'z').each do |a|
389
- [a, a.capitalize].each do |b|
390
- key = b.to_sym
391
- value = "#{b}".latex!.to_latex_math
392
- define_symbol_macro key, value
393
- define_global_macro key, value
394
- end
395
- end
396
-
397
-
398
- # units
399
- $_units = {}
400
-
401
- def unit hash
402
- $_units.update hash
403
- end
404
-
405
- class Symbol
406
- def with_unit
407
- raise "No unit defined for #{self}" unless $_units[self]
408
- self * "(#{$_units[self]})"
409
- end
410
- end
411
-
412
-
413
-
414
- # debug
415
- def debug *args
416
- args.each {|a| STDERR.puts a.inspect}
417
- return *args
418
- end
419
-
420
- # include Math
421
- include Math
422
-
423
- # degree radian
424
- def degrees x
425
- x * 180.0 / Math::PI
426
- end
427
-
428
- def radians x
429
- x / 180.0 * Math::PI
430
- end
431
-
432
-
433
- # tikz
434
- def tikz *libs
435
- $_erbout << "\\usepackage{tikz}\n"
436
- $_erbout << "\\usetikzlibrary{#{libs.join(",")}}\n" if libs.any?
437
- end
data/texlab DELETED
@@ -1,3 +0,0 @@
1
- #!/bin/bash
2
-
3
- "$(dirname "$0")/bin/texlab" -g --noplugin -S "$(dirname "$0")/vim/texlab.vim" "$@"
@@ -1,92 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = "texlab"
8
- s.version = "0.1.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Bern\u{e1}t Kall\u{f3}"]
12
- s.date = "2012-04-09"
13
- s.description = "texlab a toolkit based on erb that for creating documents and doing calculations at the same time. It is capable to output the proper number of significant digits, generate tables and plots using gnuplot. It has a simple DSL."
14
- s.email = "kallo.bernat@gmail.com"
15
- s.executables = ["texlab-compile"]
16
- s.extra_rdoc_files = [
17
- "LICENSE.txt",
18
- "README.rdoc",
19
- "README.txt"
20
- ]
21
- s.files = [
22
- ".document",
23
- ".rspec",
24
- "Gemfile",
25
- "Gemfile.lock",
26
- "LICENSE.txt",
27
- "README.rdoc",
28
- "README.txt",
29
- "Rakefile",
30
- "VERSION",
31
- "bin/texlab-compile",
32
- "bin/texlab-compile.tex",
33
- "bin/texlab-console",
34
- "doc/readme.texlab",
35
- "lib/texlab.rb",
36
- "lib/texlab/boot.rb",
37
- "lib/texlab/texlabfile.rb",
38
- "spec/spec_helper.rb",
39
- "spec/texlab_spec.rb",
40
- "texlab",
41
- "texlab.gemspec",
42
- "vim/texlab.vim"
43
- ]
44
- s.homepage = "http://github.com/cie/texlab"
45
- s.licenses = ["MIT"]
46
- s.require_paths = ["lib"]
47
- s.rubygems_version = "1.8.15"
48
- s.summary = "a tool to create documents with latex and ruby"
49
-
50
- if s.respond_to? :specification_version then
51
- s.specification_version = 3
52
-
53
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
- s.add_runtime_dependency(%q<rake4latex>, [">= 0"])
55
- s.add_runtime_dependency(%q<plusminus>, [">= 0"])
56
- s.add_runtime_dependency(%q<easystats>, [">= 0"])
57
- s.add_runtime_dependency(%q<to_latex>, [">= 0"])
58
- s.add_runtime_dependency(%q<gnuplot>, [">= 0"])
59
- s.add_runtime_dependency(%q<latex>, [">= 0"])
60
- s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
61
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
62
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
63
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
64
- s.add_development_dependency(%q<rcov>, [">= 0"])
65
- else
66
- s.add_dependency(%q<rake4latex>, [">= 0"])
67
- s.add_dependency(%q<plusminus>, [">= 0"])
68
- s.add_dependency(%q<easystats>, [">= 0"])
69
- s.add_dependency(%q<to_latex>, [">= 0"])
70
- s.add_dependency(%q<gnuplot>, [">= 0"])
71
- s.add_dependency(%q<latex>, [">= 0"])
72
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
73
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
74
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
75
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
76
- s.add_dependency(%q<rcov>, [">= 0"])
77
- end
78
- else
79
- s.add_dependency(%q<rake4latex>, [">= 0"])
80
- s.add_dependency(%q<plusminus>, [">= 0"])
81
- s.add_dependency(%q<easystats>, [">= 0"])
82
- s.add_dependency(%q<to_latex>, [">= 0"])
83
- s.add_dependency(%q<gnuplot>, [">= 0"])
84
- s.add_dependency(%q<latex>, [">= 0"])
85
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
86
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
87
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
88
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
89
- s.add_dependency(%q<rcov>, [">= 0"])
90
- end
91
- end
92
-