Class: AsciiChem::Formatter::Latex

Inherits:
Base
  • Object
show all
Defined in:
lib/asciichem/formatter/latex.rb

Overview

Renders a Model tree as LaTeX, wrapped in mhchem's \ce{...} at the formula level only. Output requires mhchem in the LaTeX preamble:

\\usepackage[version=4]{mhchem}

Example: H_2O -> \ce{H2O}.

Instance Method Summary collapse

Methods inherited from Base

#render

Constructor Details

#initializeLatex

Returns a new instance of Latex.



13
14
15
# File 'lib/asciichem/formatter/latex.rb', line 13

def initialize
  @inside_ce = false
end

Instance Method Details

#visit_atom(atom) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asciichem/formatter/latex.rb', line 32

def visit_atom(atom)
  parts = []
  parts << (":" * atom.lone_pairs) if atom.lone_pairs
  parts << "^#{wrap(atom.isotope)}" if atom.isotope
  parts << wrap(atom.element)
  parts << mhchem_subscript(atom.subscript) if atom.subscript
  if atom.charge
    parts << "^#{wrap(atom.charge)}"
  elsif atom.oxidation_state
    parts << "^{(#{wrap(atom.oxidation_state)})}"
  elsif atom.superscript
    parts << "^#{wrap(atom.superscript)}"
  end
  parts << ("." * atom.radical_electrons) if atom.radical_electrons
  parts << atom.ring_closures.to_s if atom.ring_closures
  parts.join
end

#visit_bond(bond) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/asciichem/formatter/latex.rb', line 57

def visit_bond(bond)
  case bond.kind
  when :single then "-"
  when :double then "="
  when :triple then "\\\\equiv{}"
  else bond.ascii
  end
end

#visit_electron_configuration(ec) ⇒ Object



98
99
100
101
# File 'lib/asciichem/formatter/latex.rb', line 98

def visit_electron_configuration(ec)
  inner = ec.orbitals.map { |orb, occ| "#{wrap(orb)}^#{wrap(occ)}" }.join(" ")
  "\\ce{#{inner}}"
end

#visit_embedded_math(em) ⇒ Object



103
104
105
# File 'lib/asciichem/formatter/latex.rb', line 103

def visit_embedded_math(em)
  "$#{em.formula.to_latex}$"
end

#visit_formula(formula) ⇒ Object



17
18
19
# File 'lib/asciichem/formatter/latex.rb', line 17

def visit_formula(formula)
  within_ce { "\\ce{" + formula.nodes.map { |n| render_node(n) }.join + "}" }
end

#visit_group(group) ⇒ Object



50
51
52
53
54
55
# File 'lib/asciichem/formatter/latex.rb', line 50

def visit_group(group)
  body = group.nodes.map { |n| render_node(n) }.join
  open, close = bracket_chars(group.bracket)
  suffix = group.multiplicity ? mhchem_subscript(group.multiplicity) : ""
  "#{open}#{body}#{close}#{suffix}"
end

#visit_molecule(molecule) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/asciichem/formatter/latex.rb', line 21

def visit_molecule(molecule)
  prefix = blank?(molecule.coefficient) ? "" : molecule.coefficient.to_s
  stereo = molecule.stereo ? "(#{molecule.stereo_letter})-" : ""
  body = molecule.nodes.map { |n| render_node(n) }.join
  if @inside_ce
    "#{stereo}#{prefix}#{body}"
  else
    "\\ce{#{stereo}#{prefix}#{body}}"
  end
end

#visit_reaction(reaction) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/asciichem/formatter/latex.rb', line 66

def visit_reaction(reaction)
  left = reaction.reactants.map { |n| render_node(n) }.join(" + ")
  right = reaction.products.map { |n| render_node(n) }.join(" + ")
  arrow = arrow_for(reaction.arrow)
  conds = reaction.conditions
  return "#{left} #{arrow} #{right}" unless conds

  above = conditions_bracket(conds.above)
  below = conditions_bracket(conds.below)
  "#{left} #{arrow}#{above}#{below} #{right}"
end

#visit_reaction_cascade(cascade) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/asciichem/formatter/latex.rb', line 78

def visit_reaction_cascade(cascade)
  return "" if cascade.steps.empty?

  head = cascade.steps.first
  out = head.reactants.map { |n| render_node(n) }.join(" + ")
  cascade.steps.each do |step|
    arrow = arrow_for(step.arrow)
    conds = step.conditions
    if conds
      above = conditions_bracket(conds.above)
      below = conditions_bracket(conds.below)
      out += " #{arrow}#{above}#{below}"
    else
      out += " #{arrow}"
    end
    out += " " + step.products.map { |n| render_node(n) }.join(" + ")
  end
  out
end

#visit_text(text) ⇒ Object



107
108
109
# File 'lib/asciichem/formatter/latex.rb', line 107

def visit_text(text)
  text.content
end