Class: Uniword::MathEquation

Inherits:
Element
  • Object
show all
Defined in:
lib/uniword/math_equation.rb

Overview

Represents a mathematical equation in a Word document Responsibility: Hold and manage Plurimath formula objects for math equations

Office Math Markup Language (OMML) is used in OOXML documents to represent mathematical equations. This class bridges between OMML and Plurimath, providing a model-based approach to math equation handling.

v4.0.0: Plurimath integration for math equation support

Examples:

Create a simple equation

equation = Uniword::MathEquation.new
equation.formula = Plurimath::Math::Formula.new([...])
equation.display_type = :inline

Create from OMML

equation = Uniword::MathEquation.from_omml(omml_xml)
puts equation.to_latex

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

abstract!, abstract?

Constructor Details

#initialize(**attributes) ⇒ MathEquation

Initialize a new MathEquation

Parameters:

  • attributes (Hash)

    Initial attributes

Options Hash (**attributes):

  • :formula (Plurimath::Math::Formula)

    The formula object

  • :display_type (Symbol)

    Display mode (:inline or :block)

  • :alignment (Symbol)

    Alignment for block equations

  • :break_enabled (Boolean)

    Enable line breaking



48
49
50
51
# File 'lib/uniword/math_equation.rb', line 48

def initialize(**attributes)
  super
  @formula = attributes[:formula]
end

Instance Attribute Details

#formulaPlurimath::Math::Formula?

The Plurimath formula object representing the equation

Returns:

  • (Plurimath::Math::Formula, nil)


27
28
29
# File 'lib/uniword/math_equation.rb', line 27

def formula
  @formula
end

Class Method Details

.from_omml(omml_xml) ⇒ MathEquation

Create a MathEquation from OMML XML

Examples:

xml = '<m:oMath>...</m:oMath>'
equation = MathEquation.from_omml(xml)

Parameters:

  • omml_xml (String, Nokogiri::XML::Node)

    OMML XML content

Returns:



61
62
63
# File 'lib/uniword/math_equation.rb', line 61

def self.from_omml(omml_xml)
  Math::PlurimathAdapter.from_omml(omml_xml)
end

Instance Method Details

#accept(visitor) ⇒ Object

Visitor pattern support

Parameters:

  • visitor (Object)

    The visitor object

Returns:

  • (Object)

    Result of visitor’s visit operation



134
135
136
# File 'lib/uniword/math_equation.rb', line 134

def accept(visitor)
  visitor.visit_math_equation(self)
end

#alignmentSymbol

Alignment for block equations

Returns:

  • (Symbol)

    :left, :center, or :right



35
# File 'lib/uniword/math_equation.rb', line 35

attribute :alignment, :string

#block?Boolean

Check if equation is block/display mode

Returns:

  • (Boolean)


126
127
128
# File 'lib/uniword/math_equation.rb', line 126

def block?
  display_type.to_s == "block"
end

#break_enabledBoolean

Whether equation should break across lines

Returns:

  • (Boolean)


39
# File 'lib/uniword/math_equation.rb', line 39

attribute :break_enabled, :boolean, default: -> { false }

#display_typeSymbol

Display type for the equation

Returns:

  • (Symbol)

    :inline or :block



31
# File 'lib/uniword/math_equation.rb', line 31

attribute :display_type, :string, default: -> { "inline" }

#inline?Boolean

Check if equation is inline (vs block/display)

Returns:

  • (Boolean)


119
120
121
# File 'lib/uniword/math_equation.rb', line 119

def inline?
  display_type.to_s == "inline"
end

#to_asciimathString

Convert equation to AsciiMath

Examples:

equation.to_asciimath
# => "x^2 + y^2 = z^2"

Returns:

  • (String)

    AsciiMath representation



110
111
112
113
114
# File 'lib/uniword/math_equation.rb', line 110

def to_asciimath
  return "" unless formula

  formula.to_asciimath
end

#to_latexString

Convert equation to LaTeX

Examples:

equation.to_latex
# => "x^2 + y^2 = z^2"

Returns:

  • (String)

    LaTeX representation



84
85
86
87
88
# File 'lib/uniword/math_equation.rb', line 84

def to_latex
  return "" unless formula

  formula.to_latex
end

#to_mathmlString

Convert equation to MathML

Examples:

equation.to_mathml
# => "<math>...</math>"

Returns:

  • (String)

    MathML representation



97
98
99
100
101
# File 'lib/uniword/math_equation.rb', line 97

def to_mathml
  return "" unless formula

  formula.to_mathml
end

#to_omml(options = {}) ⇒ String

Convert equation to OMML XML

Examples:

equation.to_omml
# => "<m:oMath>...</m:oMath>"

Parameters:

  • options (Hash) (defaults to: {})

    Serialization options

Returns:

  • (String)

    OMML XML string



73
74
75
# File 'lib/uniword/math_equation.rb', line 73

def to_omml(options = {})
  Math::PlurimathAdapter.to_omml(self, options)
end

#valid?Boolean

Validate the equation

Returns:

  • (Boolean)

    true if valid



141
142
143
# File 'lib/uniword/math_equation.rb', line 141

def valid?
  super && formula_valid?
end