Top Level Namespace

Includes:
Math

Defined Under Namespace

Modules: Enumerable Classes: Array, Hash, Symbol, TexlabFile

Instance Method Summary collapse

Instance Method Details

#column(title) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/texlab/table.rb', line 16

def column title
  @_table_columns << (@_table_columns.last[title] || {})

  result = (yield if block_given?)
  subcolumns = @_table_columns.pop

  if not subcolumns.empty?
    @_table_columns.last[title] = subcolumns
  elsif result
    if @_table_columns.last[title]
      @_table_columns.last[title] = *@_table_columns.last[title], *result
    else
      @_table_columns.last[title] = *result
    end
  else
    @_table_columns.last[title] ||= nil
  end

end

#data(*args) ⇒ Object



129
130
131
# File 'lib/texlab/plot.rb', line 129

def data *args
  datastring args
end

#dataset(*args) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/texlab/plot.rb', line 73

def dataset *args
  opts = args.extract_options!

  data = args.shift
  data_is_expr = !!data
  if data
    # simple function
    ds = Gnuplot::DataSet.new data
  else
    # data
    @_datastrings = []
    yield

    c = @_datastrings.first.count
    data = (0...c).map do |i|
      @_datastrings.map do |t|
        case value = t[i]
        when String
          "\"#{value.to_s.gsub(/([\\"])/, "\\\\\\1")}\""
        else
          value.to_s
        end
      end
    end

    ds = Gnuplot::DataSet.new data
  end
  
  # check args
  raise ArgumentError, "Unnecessary parameters: #{args}" unless args.empty?

  # tweak args (syntax candy)
  opts[:title] = nil unless data_is_expr or opts.has_key? :title
  if opts.key? :title and not opts[:title]
    opts.delete(:title)
    opts[:notitle] = true
  end

  # send options
  opts.each do |key, value|
    case value
    when true
      ds.send key
    else
      ds.send :"#{key}=", value
    end
  end

  @_datasets << ds

end

#datastring(arg) ⇒ Object



125
126
127
# File 'lib/texlab/plot.rb', line 125

def datastring arg
  @_datastrings << arg
end

#debug(*args) ⇒ Object

debug



3
4
5
6
# File 'lib/texlab/debug.rb', line 3

def debug *args
  args.each {|a| STDERR.puts a.inspect}
  return *args
end

#define_global_macro(key, value) ⇒ Object



35
36
37
# File 'lib/texlab/macro.rb', line 35

def define_global_macro key, value
  eval "$#{key} = '#{value.gsub("\\","\\\\").gsub("'", "\\'")}'"
end

#define_latex_macro(key, value) ⇒ Object



31
32
33
# File 'lib/texlab/macro.rb', line 31

def define_latex_macro key, value
  $_erbout << "\\def\\#{key}{#{value}}"
end

#define_symbol_macro(key, value) ⇒ Object



27
28
29
# File 'lib/texlab/macro.rb', line 27

def define_symbol_macro key, value
  $_macros[key] = value
end

#degrees(x) ⇒ Object

degree radian



5
6
7
# File 'lib/texlab/math.rb', line 5

def degrees x
  x * 180.0 / Math::PI
end

#documentHeader(*args) ⇒ Object



10
11
12
# File 'lib/texlab/latex.rb', line 10

def documentHeader *args
  $_latexfile.documentHeader *args
end

#env(*args) ⇒ Object



14
15
16
# File 'lib/texlab/latex.rb', line 14

def env *args
  $_latexfile.env(*args){yield}
end

#figure(*args) ⇒ Object

Figure generation



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/texlab/figure.rb', line 4

def figure *args
  opts = args.extract_options!
  caption = args.shift
  format = opts[:width] ? "width=#{opts[:width]}" : opts[:format]
  label = opts[:label]
  filename = opts[:filename]
  if @_figures
    @_figures << Figure.new(format, filename, caption, label)
  else
    raise "not implemented"
  end
end

#figures(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/texlab/figure.rb', line 17

def figures *args
  opts = args.extract_options!
  placement = opts.delete(:placement) || "htbp"
  label = opts.delete(:label)
  newPageThreshold = opts.delete(:newPageThreshold) || 29
  caption = args.shift

  @_figures = []
  yield

  $_latexfile.figures(caption, @_figures, newPageThreshold, placement, label)

  @_figures = nil
end

#fit(expr, opts = {}) ⇒ Object

fitting



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
# File 'lib/texlab/plot.rb', line 135

def fit expr, opts={}
  raise "via: is a necessary parameter" if not opts[:via]
  via = opts[:via]
  vars = via.split(",")

  using = opts[:using]

  @_datasets = []

  dataset do 
    yield
  end

  ds = @_datasets.first

  errlines = []
  result = {}

  gnuplot = Gnuplot.gnuplot(true) or raise "gnuplot not found"
  Open3.popen3(gnuplot) do |gp, out, err, external|
    vars.each do |var|
      gp << "#{var} = #{rand}\n"
    end
    gp <<<<-GP
      fit #{expr} '-' #{"using " + using if using} via #{via}
    GP
    gp << ds.to_gplot
    gp.close
    
    errlines = err.readlines
    errlines.each do |l|
      if l =~ /\A([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*([0-9eE.+-]+)\s*\+\/-\s*([0-9eE.+-]+)/
        result[$1.to_sym] = $2.to_f.pm($3.to_f)
      end
    end
  end

  vars.map do |v|
    result[v.to_sym] or raise "could not fit:\n" + errlines.join("\n")
  end
end

#macro(hash) ⇒ Object

a macro is available in tex (asdf) and in ruby (:asdf.to_latex or $asdf). It will go to math mode



7
8
9
10
11
12
13
14
# File 'lib/texlab/macro.rb', line 7

def macro hash
  hash.each do |key, value|
    value = value.latex!.to_latex_math
    define_latex_macro key, value
    define_symbol_macro key, value
    define_global_macro key, value
  end
end

#plot(*args) ⇒ Object



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
# File 'lib/texlab/plot.rb', line 13

def plot *args
  opts = args.extract_options!
  title = args[0]

  placement = opts.delete(:placement) || (opts[:label] ? "htbp" : "H")
  width   = opts.delete(:width) || "17cm"
  height = opts.delete(:height) || "10cm"
  cmd    = opts.delete(:cmd) || "plot"
  label = opts.delete(:label)
  debug = opts.delete(:debug)

  env :figure, "[#{placement}]" do
    env :center do
      @_datasets = []
      yield
      @_datasets

      gnuplot = Gnuplot.gnuplot(true) or raise "gnuplot not found"
      Open3.popen3(gnuplot) do |gp, out, err, external|
        gp = STDERR if debug
        gp <<<<-GP
          set terminal latex size #{width}, #{height}
        GP

        Gnuplot::Plot.new(gp, cmd) do |plot|
          opts.each do |key, value|
            case value
            when true
              plot.send key
            when false
              gp << "unset #{key}\n"
            else
              plot.send key, value.to_s.gsub(/([\\])/, "\\\\\\1")
            end
          end

          @_datasets.each do |ds|
            plot.data << ds
          end
        end
        gp.close
        $_erbout << out.readlines.join("\n")

        if not external.value.success?
          errlines = err.readlines
          raise "could not plot:\n" + errlines.join("\n")
        end
      end
    end
    $_erbout << "\\caption{#{title}}" if title
    $_erbout << "\\label{#{label}}" if label
  end
end

#puts(string) ⇒ Object



18
19
20
# File 'lib/texlab/latex.rb', line 18

def puts string
  $_erbout << string.to_latex << "\n"
end

#radians(x) ⇒ Object



9
10
11
# File 'lib/texlab/math.rb', line 9

def radians x
  x / 180.0 * Math::PI
end

#row(title) ⇒ Object



36
37
38
39
40
# File 'lib/texlab/table.rb', line 36

def row title
  @_table_columns = [@_table[title] || {}]
  yield
  @_table[title] = @_table_columns.first
end

#splot(*args, &block) ⇒ Object



67
68
69
70
71
# File 'lib/texlab/plot.rb', line 67

def splot *args, &block
  opts = args.extract_options!
  opts.cmd = "splot"
  plot *args, opts, &block
end

#table(title, settings = {}) ⇒ Object

table generation



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/texlab/table.rb', line 4

def table title, settings={}
  @_table = {}
  yield
  entries = @_table

  unless @_tables
    $_latexfile.table(entries, title, settings)
  else
    @_tables << [entries, title, settings]
  end
end

#tables(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/texlab/table.rb', line 42

def tables *args
  opts = args.extract_options!
  caption = args.shift

  @_tables = []
  yield

  $_latexfile.tables(@_tables, caption, opts)

  @_tables = nil
end

#text_macro(hash) ⇒ Object

a text macro is available in tex (asdf) and in ruby (:asdf, $asdf). It will not be forced to math mode



18
19
20
21
22
23
24
25
# File 'lib/texlab/macro.rb', line 18

def text_macro hash
  hash.each do |key, value|
    value = "\\text{#{value}}".latex!
    define_latex_macro key, value
    define_symbol_macro key, value
    define_global_macro key, value
  end
end

#tikz(*libs) ⇒ Object

tikz



2
3
4
5
# File 'lib/texlab/tikz.rb', line 2

def tikz *libs
  $_erbout << "\\usepackage{tikz}\n"
  $_erbout << "\\usetikzlibrary{#{libs.join(",")}}\n" if libs.any?
end

#unit(hash) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/texlab/macro.rb', line 81

def unit hash
  # allow array keys to be DRY
  hash.each do |ks,v|
    [*ks].each do |k|
      $_units[k] = v
    end
  end
end