Class: RubyPost::File

Inherits:
Object
  • Object
show all
Defined in:
lib/objects.rb

Overview

metapost file A metapost file can contain many figures. Notes: Filenames cannot contain underscores for view to work!
compile_to_str has a dodgy backspace handler.

Constant Summary collapse

@@start_of_file =
<<END_OF_STRING
prologues := 2;
filenametemplate "%j-%c.mps";
verbatimtex
%&latex
\\documentclass{minimal}
\\begin{document}
etex
END_OF_STRING

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname = nil) ⇒ File

input ‘sarith’ so that metapost can read exponential notation



91
92
93
94
95
96
# File 'lib/objects.rb', line 91

def initialize(fname = nil)
  @figures = Array.new
  @fname = fname
  @dvi_viewer = 'yap'
  @metapost_options = '-interaction=nonstopmode'
end

Instance Attribute Details

#dvi_viewer=(value) ⇒ Object (writeonly)

Sets the attribute dvi_viewer

Parameters:

  • value

    the value to set the attribute dvi_viewer to.



74
75
76
# File 'lib/objects.rb', line 74

def dvi_viewer=(value)
  @dvi_viewer = value
end

#fname=(value) ⇒ Object (writeonly)

Sets the attribute fname

Parameters:

  • value

    the value to set the attribute fname to.



74
75
76
# File 'lib/objects.rb', line 74

def fname=(value)
  @fname = value
end

#metapost_options=(value) ⇒ Object (writeonly)

Sets the attribute metapost_options

Parameters:

  • value

    the value to set the attribute metapost_options to.



74
75
76
# File 'lib/objects.rb', line 74

def metapost_options=(value)
  @metapost_options = value
end

Instance Method Details

#add_figure(f) ⇒ Object

add a new figure to this mpost file



99
100
101
# File 'lib/objects.rb', line 99

def add_figure(f)
  @figures.push(f)
end

#compile(fname = @fname) ⇒ Object

calls compile_to_file and writes the and copmiles the metapost commands if mpost is in the path



129
130
131
132
# File 'lib/objects.rb', line 129

def compile(fname=@fname)
  compile_to_file(fname)
  system('mpost ' + @metapost_options + ' ' + fname + '.mp')
end

#compile_to_file(fname = @fname) ⇒ Object

writes the string of metapost commands to a file named ‘fname.mp’



122
123
124
125
# File 'lib/objects.rb', line 122

def compile_to_file(fname=@fname)
  @fname = fname
  IO::File.open(fname + '.mp','w') { |f| f.puts self.compile_to_string }
end

#compile_to_stringObject

returns the mp file as a str



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/objects.rb', line 104

def compile_to_string
  str = @@start_of_file + @@Inputs.compile
  #save the original metapost picture
  str = str + @@picture_precompiler.compile
  @figures.each_index do
    |i| str = str + 'beginfig(' + (i+1).to_s + ");\n" + @figures[i].compile + "\n"
  end
  str = str + "end;\n"
  #remove the backspaces
  strback = str.gsub(/.[\b]/, '')
  if (strback==nil) 
    return str
  else 
    return strback 
  end
end

#viewObject

default view command is view_dvi



135
136
137
# File 'lib/objects.rb', line 135

def view
  view_dvi
end

#view_dviObject

assumes that the file has already been compiled by metapost. ie compile_to_ps has already been called. This assumes that the yap viewer and tex is in your path. Install miktex to get these by default. <p> Notes: Filenames cannot contain underscores for view_dvi to work. The “tex mproof” will not work with underscores



144
145
146
147
148
149
# File 'lib/objects.rb', line 144

def view_dvi
  str = 'tex mproof'
  @figures.each_index { |i| str = str + ' ' + @fname + '.' + (i+1).to_s }
  system(str)
  system(@dvi_viewer + ' mproof.dvi')
end