Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add math formulas in odt document? #22

Closed
zimakim opened this issue Sep 23, 2015 · 7 comments
Closed

How to add math formulas in odt document? #22

zimakim opened this issue Sep 23, 2015 · 7 comments

Comments

@zimakim
Copy link
Contributor

zimakim commented Sep 23, 2015

I need to add mathematical formulas in document. I have not found an example of how to do it

@zimakim
Copy link
Contributor Author

zimakim commented Oct 6, 2015

I've solved this problem by writing a few functions. Is it possible to include these functions in opfpy in the next release?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from xml.dom.minidom import parseString
from xml.dom import Node
import odf
import odf.opendocument
import odf.text
from odf.element import Element
from namespaces import MATHNS

math_templ = u'\
<math xmlns="http://www.w3.org/1998/Math/MathML">\
<semantics>\
<annotation encoding="StarMath 5.0">%s</annotation>\
</semantics></math>'

def gen_odf_math_(parent):
    elem = Element(qname = (MATHNS,parent.tagName))
    if parent.attributes:
        for attr, value in parent.attributes.items():
            elem.setAttribute((MATHNS,attr), value, check_grammar=False)

    for child in parent.childNodes:
        if child.nodeType == Node.TEXT_NODE:
            text = child.nodeValue
            elem.addText(text, check_grammar=False)
        else:
            elem.addElement(gen_odf_math_(child), check_grammar=False)
    return elem

def gen_odf_math(starmath_string):
    u'''
    Generating odf.math.Math element
    '''
    mathml = math_templ % (starmath_string)
    math_ = parseString(mathml.encode('utf-8'))
    math_ = math_.documentElement
    odf_math = gen_odf_math_(math_)
    return odf_math


def main():
    doc = odf.opendocument.OpenDocumentText()
    p = odf.text.P(text=u'text')
    df = odf.draw.Frame( zindex=0, anchortype='as-char')
    p.addElement(df)
    doc.text.addElement(p)

    formula = 'c = sqrt(a^2+b_2) + %ialpha over %ibeta'
    math = gen_odf_math(formula)
    do = odf.draw.Object()
    do.addElement(math)
    df.addElement(do)

    outputfile = u'result'
    doc.save(outputfile, True)

if __name__ == '__main__':
    main()

@zimakim
Copy link
Contributor Author

zimakim commented Oct 8, 2015

A more convenient way

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import odf
from odf.opendocument import OpenDocumentText
from odf.element import Element
from odf.text import P
from odf.math import Math
from namespaces import MATHNS


def main():
    doc = OpenDocumentText()
    p = P(text=u'text')
    df = odf.draw.Frame( zindex=0, anchortype='as-char')
    p.addElement(df)
    doc.text.addElement(p)

    formula =u'c=sqrt(a^2+b^2)'
    math = Math()
    annot = Element(qname = (MATHNS,u'annotation'))
    annot.addText(formula, check_grammar=False)
    annot.setAttribute((MATHNS,'encoding'), 'StarMath 5.0', check_grammar=False)
    math.addElement(annot)
    do = odf.draw.Object()
    do.addElement(math)
    df.addElement(do)

    outputfile = u'result'
    doc.save(outputfile, True)

if __name__ == '__main__':
    main()

@zimakim zimakim closed this as completed Oct 8, 2015
@ntala
Copy link

ntala commented Dec 3, 2016

I tried the solutions given but a problem remains : in the document "result.odt", the formula is a little frame and its content is invisible. I must edit it by double-clicking on it to see it.
Is the behavior the same with you ?

@scizers
Copy link

scizers commented Mar 21, 2017

Hello @ntala ,
Its showing me the same problem, did you solve it ?

@zimakim
Copy link
Contributor Author

zimakim commented Mar 24, 2017

@scizers
Copy link

scizers commented Mar 31, 2017

@zimakim
I guess the only was to handle it is to give height and width mannualy while defining the frame

df = odf.draw.Frame( zindex=0, anchortype='as-char' , width=100 , height=50)

@zimakim
Copy link
Contributor Author

zimakim commented Apr 23, 2017

@scizers
I use this macro in openoffice for force update math formulas

sub ForceUpdate
dim oCurrentController as variant: oCurrentController = ThisComponent.getCurrentController()
dim oDoc as variant: oDoc=ThisComponent
if not(oCurrentController.supportsService("com.sun.star.text.TextDocumentView")) then
'	msgbox("Macro works only in text documents.")
	exit sub
end if
dim oModelTextDocument as variant: oModelTextDocument = oCurrentController.Model
dim oEmbeddedObjects as variant: oEmbeddedObjects = oModelTextDocument.EmbeddedObjects
dim nIndex as long
dim nEndIndex as long: nEndIndex = oEmbeddedObjects.Count-1
dim oEmbeddedObject as variant: rem like green handle status
dim oModel as variant: rem like edit status
dim oXCOEO as variant: rem oExtendedControlOverEmbeddedObject
for nIndex=0 to nEndIndex
	oEmbeddedObject = oEmbeddedObjects.getByIndex(nIndex)
	oModel = oEmbeddedObject.Model: rem might be empty; css.comp.math.FormulaDocument
	if Not(isEmpty(oModel)) then
		if oModel.supportsService("com.sun.star.formula.FormulaProperties") then
			rem It is a formula object.
			oXCOEO = oEmbeddedObject.ExtendedControlOverEmbeddedObject
			oXCOEO.update()
		end if	
	end if
next nIndex
end sub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants