Class: FatTable::LaTeXFormatter

Inherits:
Formatter show all
Defined in:
lib/fat_table/formatters/latex_formatter.rb

Overview

A subclass of Formatter for rendering the table as a LaTeX table. It allows foreground colors through LaTeX's xcolor package but ignores background colors. You can see the valid color names with LaTeXFormatter.valid_colors.

Constant Summary

Constants inherited from Formatter

Formatter::LOCATIONS

Instance Attribute Summary

Attributes inherited from Formatter

#footers, #format_at, #gfooters, #options, #table

Instance Method Summary collapse

Methods inherited from Formatter

#avg_footer, #avg_gfooter, default_format, #foot, #footer, #format, #format_cell, #format_for, #gfoot, #gfooter, #max_footer, #max_gfooter, #min_footer, #min_gfooter, #output, #sum_footer, #sum_gfooter

Constructor Details

#initialize(table = Table.new, **options) ⇒ LaTeXFormatter

Return a new LaTeXFormatter for +table+. You can set the following +options+ with hash-like parameters:

document:: if set to true, include a document preamble and wrap the output in a LaTeX document environment so that the output can be compiled by a LaTeX processor such as +pdflatex+. By default, only the table environment is output.

environment:: set to a string, by default 'longtable' that indicates what kind of LaTeX tabular-like environment to use for the table. The default is good for tables that might continue over multiple pages since it repeats the header at the top of each continuation page.



22
23
24
25
26
# File 'lib/fat_table/formatters/latex_formatter.rb', line 22

def initialize(table = Table.new, **options)
  super
  @options[:document] = options.fetch(:document, false)
  @options[:environment] = options.fetch(:environment, 'longtable')
end

Instance Method Details

#decorate_string(str, istruct) ⇒ Object

Add LaTeX control sequences. Ignore background color, underline, and blink. Alignment needs to be done by LaTeX, so we have to take it into account unless it's the same as the body alignment, since that is the default.



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

def decorate_string(str, istruct)
  str = quote(str)
  result = istruct.italic ? "\\itshape{#{str}}" : str
  result = istruct.bold ? "\\bfseries{#{result}}" : result
  if istruct.color && istruct.color != 'none'
    result = "{\\textcolor{#{istruct.color}}{#{result}}}"
  end
  if istruct.bgcolor && istruct.bgcolor != 'none'
    result = "{\\cellcolor{#{istruct.bgcolor}}{#{result}}}"
  end
  if (istruct._h && format_at[:body][istruct._h] &&
     istruct.alignment != format_at[:body][istruct._h].alignment) ||
     (istruct._h.nil? && istruct.alignment.to_sym != :left)
    ac = alignment_code(istruct.alignment)
    result = "\\multicolumn{1}{#{ac}}{#{result}}"
  end
  result
end

#preambleObject

LaTeX commands to load the needed packages based on the :environement option. For now, just handles the default 'longtable' :environment. The preamble always includes a command to load the xcolor package.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fat_table/formatters/latex_formatter.rb', line 85

def preamble
  result = ''
  result +=
    case @options[:environment]
    when 'longtable'
      "\\usepackage{longtable}\n"
    else
      ''
    end
  result += "\\usepackage[pdftex,table,x11names]{xcolor}\n"
  result
end