Class: Latex::Tabular
- Inherits:
-
Object
- Object
- Latex::Tabular
- Defined in:
- lib/ruby-latex.rb
Instance Method Summary collapse
- #add_row(row) ⇒ Object (also: #<<)
- #add_separator(type = "\\midrule") ⇒ Object
- #align_string ⇒ Object
- #auto_align(col) ⇒ Object
- #column_sizes ⇒ Object
- #content_style(&block) ⇒ Object
- #header=(row) ⇒ Object
- #header_style(&block) ⇒ Object
-
#initialize(cols) ⇒ Tabular
constructor
A new instance of Tabular.
- #to_tex ⇒ Object
Constructor Details
#initialize(cols) ⇒ Tabular
Returns a new instance of Tabular.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby-latex.rb', line 58 def initialize(cols) @header = nil if cols.is_a? Array @columns = cols.map { |e| e.to_sym } @columns_info = cols.map { |e| [e, Column.new] }.to_h elsif cols.is_a? Hash keys = cols.keys @columns_info = cols.clone @columns = keys.map { |k| k.to_sym } if @columns_info.values.any? { |c| c.header } headers_row = @columns.map { |c| [c, @columns_info[c].header.to_s] }.to_h @header = HeaderRowFamily.new(@columns_info, Row.new(headers_row)) end else raise "Invalid argument: expected Array or Hash" end @rows = RowFamily.new(@columns_info) end |
Instance Method Details
#add_row(row) ⇒ Object Also known as: <<
101 102 103 |
# File 'lib/ruby-latex.rb', line 101 def add_row(row) @rows << Row.new(row) end |
#add_separator(type = "\\midrule") ⇒ Object
106 107 108 |
# File 'lib/ruby-latex.rb', line 106 def add_separator(type="\\midrule") @rows << RowSeparator.new(type) end |
#align_string ⇒ Object
133 134 135 |
# File 'lib/ruby-latex.rb', line 133 def align_string @columns.map { |col| @columns_info[col].is_a?(Column) ? @columns_info[col].align : auto_align(col) }.join(" ") end |
#auto_align(col) ⇒ Object
137 138 139 |
# File 'lib/ruby-latex.rb', line 137 def auto_align(col) @rows.rows.map { |row| row[col] }.all? { |e| e.is_a?(Numeric) } ? 'r' : 'l' end |
#column_sizes ⇒ Object
127 128 129 130 131 |
# File 'lib/ruby-latex.rb', line 127 def column_sizes header_sizes = @header ? [ @header.row.sizes ] : [] sizes = @rows.rows.map { |row| row.sizes } + header_sizes @columns.map { |col| [col, (sizes.map { |e| e[col] }).max] }.to_h end |
#content_style(&block) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/ruby-latex.rb', line 90 def content_style(&block) raise "Block expected" unless block_given? @rows.styler = block return self end |
#header=(row) ⇒ Object
97 98 99 |
# File 'lib/ruby-latex.rb', line 97 def header=(row) @header = HeaderRowFamily.new(@columns_info, Row.new(row)) end |
#header_style(&block) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/ruby-latex.rb', line 82 def header_style(&block) raise "Block expected" unless block_given? raise "No header to style" unless @header @header.styler = block return self end |
#to_tex ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ruby-latex.rb', line 110 def to_tex sizes = self.column_sizes out = [] out << " \\begin{tabular}{#{self.align_string}}" out << " \\toprule" if @header out << " " + @header.row.to_tex(sizes) out << " \\midrule" end out += @rows.rows.map { |row| " " + row.to_tex(sizes) } out << " \\bottomrule" out << " \\end{tabular}" return out.join("\n") end |