| View previous topic :: View next topic |
| Author |
Message |
gromo Newbie

Joined: 21 Jun 2006 Posts: 3
|
Posted: Wed Jun 21, 2006 3:29 am Post subject: macros with math editor |
|
|
Hello!
I'm a mathematics teacher. I need to do OpenOffice documents with a lot of easy mathematical formulae like this
2x+1=3
3x-2=7
5x+4=7
(...10000 lines later ...)
3x-4=8
I want to programme the math editor (I need mathematical symbols!) with macros to automatize this task. Is it possible to do something like this:
for n=1 to 10000
insert Math equation with random number & "x +" random number &"=" & random number
next n
Do you know any internet addrees about this?
Thanks! |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
uros Super User


Joined: 22 May 2003 Posts: 601 Location: Slovenia
|
Posted: Wed Jun 21, 2006 10:40 pm Post subject: |
|
|
Hi gromo!
If linear equations are all you need, they can be made in calc as well:
| Code: | Sub RandomLinearEquations
oSheet = ThisComponent.Sheets(0)
Randomize
For i = 0 To 10000
A = Int((9 * Rnd)) + 1 ' A = 1 to 9
B = Int((10 * Rnd)) ' B = 0 to 9
C = Int((10 * Rnd)) ' C = 0 to 9
If A = 1 Then A = "" ' if you don't like 1x + ...
oSheet.getCellByPosition(0,i).String = A & "x + " & B & " = " & C
Next i
End Sub |
Hope it helps!
Uros |
|
| Back to top |
|
 |
gromo Newbie

Joined: 21 Jun 2006 Posts: 3
|
Posted: Thu Jun 22, 2006 7:08 am Post subject: Thanks! |
|
|
Thanks SergeM
Thanks Uros, but I need to use all the mathematical symbols, like square root, integral... then I have to use the math editor. |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
jrkrideau Super User

Joined: 08 Aug 2005 Posts: 6733 Location: Kingston ON Canada
|
Posted: Thu Jun 22, 2006 9:49 am Post subject: |
|
|
| uros wrote: | Hi gromo!
If linear equations are all you need, they can be made in calc as well:
| Code: | Sub RandomLinearEquations
oSheet = ThisComponent.Sheets(0)
Randomize
For i = 0 To 10000
A = Int((9 * Rnd)) + 1 ' A = 1 to 9
B = Int((10 * Rnd)) ' B = 0 to 9
C = Int((10 * Rnd)) ' C = 0 to 9
If A = 1 Then A = "" ' if you don't like 1x + ...
oSheet.getCellByPosition(0,i).String = A & "x + " & B & " = " & C
Next i
End Sub |
Hope it helps!
Uros |
uros,
If I am reading the OP correctly they want a load of formatted equations to print, probably to give to their students for practice or even on an exam. They probably don't want an answer just the text to torture (err, drill) the students. _________________ jrkrideau
Kingston ON Canada
Currently using Windows 7 & OOo 3.4.0 and Ubuntu 12.04 & LibreOffice 3.5.2.2 |
|
| Back to top |
|
 |
ChipperQ General User

Joined: 30 Apr 2006 Posts: 9 Location: Venice, California
|
Posted: Fri Jun 23, 2006 9:24 am Post subject: Re: Thanks! |
|
|
| gromo wrote: | Thanks SergeM
Thanks Uros, but I need to use all the mathematical symbols, like square root, integral... then I have to use the math editor. |
Hi
Here are a couple seat-of-the-pants hacks trying to use edeval's method (pointed out by SergeM).
(Note: If you run these in Ooo1.1.3 they seem to come out okay, but if you run them in Ooo2.0.2 they look wrong until you select one and right-click on it and choose 'Edit'; this pops open the formula editor window; when you click back anywhere in the body of the document text (not in the formula text!), the GUI closes the formula editor and then you can see the embedded formula object expanded . Haven't figured out why that is, or how to correct it, yet. Thanks to edeval for the add-on example, and to Andrew Pitonyak for his macros document.)
The DoJustOneEqPerLine sub produces 30 lines of one simple equation per line (oops, in OOo 1.1.3 it comes out on the same line; I'm probably not implementing the InsertSimpleText example correctly), and the Main sub produces 30 lines of 7 equations per line, with a mix of fractions and the square root function:
Hopefully this is helpful; I've noticed in the literature that Basic (my favorite language) is considered 'goto ridden', understandably, and so I apologize for my use of that command in my example.
| Code: | REM ***** BASIC *****
Private oDoc as Object
Sub Main
Dim sPropName as String, aValue as Any, iCount as Integer
Dim iSeed as Integer, sFormula as String
Dim iBuildString as Integer, iF1 as Integer
Dim iA as integer, iB as Integer, iC as Integer, iD as Integer, iE as Integer
Dim sGap as String, sTemplate as String, iGapCount as Integer
iSeed = 0: sTemplate = "~~~~~~~~"
oDoc = StarDesktop.CurrentComponent
For iCount = 1 to 30
sFormula = "{"
For iBuildString = 1 to 7
If Rnd(iSeed)<0.333 Then
iA = Int(8*Rnd(iSeed)+2): iB = Int(8*Rnd(iSeed)+2): iC = Int(9*Rnd(iSeed)+1)
iD = Int(8*Rnd(iSeed)+2): iE = Int(9*Rnd(iSeed)+1): iF1 = Int(8*Rnd(iSeed)+2)
If Rnd(iSeed)<0.333 Then
sFormula = sFormula + "{{sqrt{" + iA + "x}} over " + iB + "} + {" + iC + " over " + iD
sFormula = sFormula + "}={" + iE + " over " + iF1 + "}" + "{~~~~~}"
Goto Around
End If
sFormula = sFormula + "{{" + iA + "x} over " + iB + "} + {" + iC + " over " + iD
sFormula = sFormula + "}={" + iE + " over " + iF1 + "}" + "{~~~~~}"
Goto Around
End If
iGapCount = 0
iA = Int(20*Rnd(iSeed)+1)
If iA < 10 Then
iGapCount = iGapCount + 1
End If
iB = Int(20*Rnd(iSeed)+1)
If iB < 10 Then
iGapCount= iGapCount + 1
End If
iC = Int(20*Rnd(iSeed)+1)
If iC < 10 Then
iGapCount = iGapCount + 1
End If
' sFormula = sFormula + Str(Int(20*Rnd(iSeed)+1)) + "x +" + Str(Int(20*Rnd(iSeed)+1))
' sFormula = sFormula + " =" + Str(Int(20*Rnd(iSeed)+1))
sFormula = sFormula + Str(iA) + "x +" + Str(iB) + " =" + iC
sGap = "{~~~~~"
If iGapCount > 0 Then
sFormula = sFormula + sGap + Left(sTemplate, iGapCount) +"}"
Else
sFormula = sFormula + sGap + "}"
End If
Around:
Next iBuildString
sFormula = sFormula + "}"
oViewCursor = ThisComponent.getCurrentController().getViewCursor()
oTextCursor=oViewCursor.Text.createTextCursorByRange(oViewCursor)
obj=ThisComponent.CreateInstance("com.sun.star.text.TextEmbeddedObject")
obj.CLSID="078B7ABA-54FC-457F-8551-6147e776a997"
obj.AnchorType=com.sun.star.text.TextContentAnchorType.AS_CHARACTER
ThisComponent.getCurrentController().select(oTextCursor)
oTextCursor.Text.insertTextContent(oTextCursor, obj, true)
obj.EmbeddedObject.formula= sFormula
' msgbox sFormula
call InsertSimpleText
Next iCount
' obj.EmbeddedObject.BaseFontHeight = 12 ' Changing this WORKS!, but only for text; the border doesn't scale with it
' obj.EmbeddedObject.XAccessibleParent(0).Height = 600 ' doesn't work with/without X
' obj.EmbeddedObject.setPropertyValue(sPropName, aValue) 'doesn't work
' msgbox obj.EmbeddedObject.Dbg_Properties
'obg.EmbeddedObject.setRelativeNumeratorHeight = 64 'Note: BaseFontHeight changes text size in frameEnd Sub
End Sub
'-----------------------------------------------------------
Sub DoJustOneEqPerLine
Dim sPropName as String, aValue as Any, iCount as Integer
Dim iSeed as Integer, sFormula as String
Dim iBuildString as Integer
Dim iA as integer, iB as Integer, iC as Integer
iSeed = 0
oDoc = StarDesktop.CurrentComponent
For iCount = 1 to 30
sFormula = "{"
iA = Int(20*Rnd(iSeed)+1)
iB = Int(20*Rnd(iSeed)+1)
iC = Int(20*Rnd(iSeed)+1)
sFormula = sFormula + Str(iA) + "x +" + Str(iB) + " =" + iC + "}"
oViewCursor = ThisComponent.getCurrentController().getViewCursor()
oTextCursor=oViewCursor.Text.createTextCursorByRange(oViewCursor)
obj=ThisComponent.CreateInstance("com.sun.star.text.TextEmbeddedObject")
obj.CLSID="078B7ABA-54FC-457F-8551-6147e776a997"
obj.AnchorType=com.sun.star.text.TextContentAnchorType.AS_CHARACTER
ThisComponent.getCurrentController().select(oTextCursor)
oTextCursor.Text.insertTextContent(oTextCursor, obj, true)
obj.EmbeddedObject.formula= sFormula
' msgbox sFormula
call InsertSimpleText
Next iCount
End Sub
'-------------------------------------------------------------
Sub InsertSimpleText
Dim oDocument As Object
Dim oText As Object
Dim oViewCursor2 As Object
Dim oTextCursor2 As Object
'oDocument = ThisComponent
oText = oDoc.Text
oViewCursor2 = oDoc.CurrentController.getViewCursor()
oTextCursor2 = oText.createTextCursorByRange(oViewCursor2.getEnd())
' Place the text to insert here
oText.insertString(oTextCursor2, " "+Chr(13)+Chr(13), FALSE)
End Sub
'------------------------------------------------------------- |
|
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Fri Jun 23, 2006 11:44 am Post subject: |
|
|
I made some modifications and cleaned certain "issues" such as using the current controller at some locations and using ThisComponent at others. Also, it properly inserts new lines now.
It will, however, dose not expand the equations when they are inserted. I am not certain why that is.
| Code: | Option Explicit
Sub Main
'InsertNewLines(ThisComponent, 4)
'InsertSimpleEquations(ThisComponent, 1)
'InsertFormulas(ThisComponent, 3)
End Sub
Sub InsertFormulas(oDoc, iNumToInsert As Integer)
Dim sPropName As String
Dim aValue
Dim iCount As Integer
Dim iNumPerLine As Integer
Dim iSeed As Integer
Dim sFormula As String
Dim iBuildString As Integer
Dim iF1 As Integer
Dim iA As integer
Dim iB As Integer
Dim iC As Integer
Dim iD As Integer
Dim iE As Integer
Dim sGap As String
Dim sTemplate As String
Dim iGapCount As Integer
Dim oVC
Dim obj
iNumPerLine = 3
iSeed = 0
sTemplate = "~~~~~~~~"
oVC = oDoc.CurrentController.getViewCursor()
oVC.gotoRange(oVC.getEnd(), False)
REM Insert equations
For iCount = 1 to iNumToInsert
sFormula = "{"
For iBuildString = 1 to iNumPerLine
If Rnd(iSeed)<0.333 Then
iA = Int(8*Rnd(iSeed)+2): iB = Int(8*Rnd(iSeed)+2): iC = Int(9*Rnd(iSeed)+1)
iD = Int(8*Rnd(iSeed)+2): iE = Int(9*Rnd(iSeed)+1): iF1 = Int(8*Rnd(iSeed)+2)
If Rnd(iSeed)<0.333 Then
sFormula = sFormula + "{{sqrt{" + iA + "x}} over " + iB + "} + {" + iC + " over " + iD
sFormula = sFormula + "}={" + iE + " over " + iF1 + "}" + "{~~~~~}"
Else
sFormula = sFormula + "{{" + iA + "x} over " + iB + "} + {" + iC + " over " + iD
sFormula = sFormula + "}={" + iE + " over " + iF1 + "}" + "{~~~~~}"
End If
Else
iGapCount = 0
iA = Int(20*Rnd(iSeed)+1)
If iA < 10 Then
iGapCount = iGapCount + 1
End If
iB = Int(20*Rnd(iSeed)+1)
If iB < 10 Then
iGapCount= iGapCount + 1
End If
iC = Int(20*Rnd(iSeed)+1)
If iC < 10 Then
iGapCount = iGapCount + 1
End If
' sFormula = sFormula + Str(Int(20*Rnd(iSeed)+1)) + "x +" + Str(Int(20*Rnd(iSeed)+1))
' sFormula = sFormula + " =" + Str(Int(20*Rnd(iSeed)+1))
sFormula = sFormula + Str(iA) + "x +" + Str(iB) + " =" + iC
sGap = "{~~~~~"
If iGapCount > 0 Then
sFormula = sFormula + sGap + Left(sTemplate, iGapCount) +"}"
Else
sFormula = sFormula + sGap + "}"
End If
End If
Next iBuildString
sFormula = sFormula + "}"
InsertEquationAtViewCursor(oDoc, sFormula)
Call InsertNewLines(oDoc, 1)
Next iCount
' obj.EmbeddedObject.BaseFontHeight = 12 ' Changing this WORKS!, but only for text; the border doesn't scale with it
' obj.EmbeddedObject.XAccessibleParent(0).Height = 600 ' doesn't work with/without X
' obj.EmbeddedObject.setPropertyValue(sPropName, aValue) 'doesn't work
' msgbox obj.EmbeddedObject.Dbg_Properties
'obg.EmbeddedObject.setRelativeNumeratorHeight = 64 'Note: BaseFontHeight changes text size in frame
End Sub
'-----------------------------------------------------------
Sub InsertSimpleEquations(oDoc, iNumToInsert As Integer)
Dim sPropName As String
Dim aValue As Any
Dim iCount As Integer
Dim iSeed As Integer
Dim sFormula As String
Dim iBuildString As Integer
Dim iA As Integer
Dim iB As Integer
Dim iC As Integer
iSeed = 0
For iCount = 1 to iNumToInsert
sFormula = "{"
iA = Int(20*Rnd(iSeed)+1)
iB = Int(20*Rnd(iSeed)+1)
iC = Int(20*Rnd(iSeed)+1)
sFormula = sFormula + Str(iA) + "x +" + Str(iB) + " =" + iC + "}"
InsertEquationAtViewCursor(oDoc, sFormula)
InsertNewLines(oDoc, 1)
Next iCount
End Sub
Sub InsertEquationAtViewCursor(oDoc, sFormula$)
Dim oVC
Dim oObj
oVC = oDoc.CurrentController.getViewCursor()
oVC.gotoRange(oVC.getEnd(), False)
oObj = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
oObj.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
oVC.Text.insertTextContent(oVC, oObj, False)
oObj.EmbeddedObject.formula = sFormula
End Sub
'-------------------------------------------------------------
Sub InsertNewLines(oDoc, nNumLines As Integer)
Dim oText
Dim oViewCursor
Dim oCursor
Dim i As Integer
Dim oVC
REM Get the view cursor and move the cursor to the end
REM of the selected range in case a range is selected.
REM This is likely to fail if cells in a table are selected.
oVC = oDoc.CurrentController.getViewCursor()
oVC.gotoRange(oVC.getEnd(), False)
For i = 0 To nNumLines
oText = oVC.getText()
oText.insertControlCharacter(oVC, _
com.sun.star.text.ControlCharacter.LINE_BREAK, False)
Next
End Sub |
_________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
ChipperQ General User

Joined: 30 Apr 2006 Posts: 9 Location: Venice, California
|
Posted: Fri Jun 23, 2006 7:28 pm Post subject: |
|
|
Wow, thank you for cleaning that up! I first learned basic back when line numbers were mandatory, and there was no Else clause for the If Then statement. Seeing how you nested an additional Else statement has shown me how I can express my 'bad-habit' logic more properly. And thank you for fixing the line insertion routines. Also much easier to see how to make adjustments in equations/line, number of lines to do, etc., with the subroutines structured perfectly when called from Main. Spot on!
Since the OP expressed the desire to utilize additional mathematical functions, it makes sense to implement a standardized way to allow for generating problems all of one specific type, or including several types (say to generate quizzes/tests instead of homework). The example code presently displays just 3 types of equations, and does so from all of them randomly. To be a little more useful for a teacher, s/he should be able to specify types to use, add additional types without having to modify any routines, and express quantities to generate and format to use, from a simple input box. I'll do this as time permits, since there's obviously a demand for it (see Math Worksheets Online thread from mathforum.org, which I found in a quick, general google search, after looking around a bit in the OooMath forum)
I also found interactive objects, like Factor a binomial , a deployed Java applet at the Math.com site, if I'm applying the terms correctly, and I think it would be fun to do something similar, useable straight from the Office. I'll do this too, as time permits, and post them here. |
|
| Back to top |
|
 |
gromo Newbie

Joined: 21 Jun 2006 Posts: 3
|
Posted: Mon Jun 26, 2006 5:20 am Post subject: My first openoffice macro |
|
|
Wow! Thanks ! All you are great!
With your help I did my first openoffice macro, that makes a worksheet about factoring numbers.
I hope it wil be the first one of a lot. So far I used VBA to programme my math macros (you can see my web http://www.toomates.net/vba.htm ) but form now I want to learn and use OpenOffice. And It's would be nice to contact with people with the same interest.
What a pity I have to use the older 1.1 version of OpenOffice because It's not acceptable to me to "click" on any expression for any worksheet I do. Any idea about this?
Thanks a lot, Gerard.
| Code: |
Option Explicit
' Generates a work-sheet of factoring numbers
Dim Max_exponent as integer
Dim Max_prime as integer
Dim Max_factors as integer
Dim Max_number as Long
Dim Wording(1 to 10) as string
Dim Solution(1 to 10) as string
Sub Main
dim n as integer
Max_exponent=4
Max_prime=6
Max_factors=5
Max_number=10000
for n=1 to 10
Exercise(n,Max_exponent,Max_prime,Max_factors,Max_number)
next
InsertSimpleText(ThisComponent,"Exercises:")
InsertNewLines(ThisComponent,1)
for n=1 to 10
InsertSimpleText(ThisComponent,Str(n)&")"& " ")
InsertEquationAtViewCursor(ThisComponent,Wording(n))
InsertNewLines(ThisComponent,1)
next
InsertSimpleText(ThisComponent,"Solutions:")
InsertNewLines(ThisComponent,1)
for n=1 to 10
InsertSimpleText(ThisComponent,Str(n)&")"& " ")
InsertEquationAtViewCursor(ThisComponent,Solution(n))
InsertNewLines(ThisComponent,1)
next
End Sub
' --------------------------------------------------------------------
' Kernel Code --------------------------------------------------------
' --------------------------------------------------------------------
Sub InsertEquationAtViewCursor(oDoc, sFormula$)
'Author: pitonyak
Dim oVC
Dim oObj
oVC = oDoc.CurrentController.getViewCursor()
oVC.gotoRange(oVC.getEnd(), False)
oObj = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
oObj.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
oVC.Text.insertTextContent(oVC, oObj, False)
oObj.EmbeddedObject.formula = sFormula
End Sub
Sub InsertNewLines(oDoc, nNumLines As Integer)
'Author: pitonyak
Dim oText
Dim oViewCursor
Dim oCursor
Dim i As Integer
Dim oVC
REM Get the view cursor and move the cursor to the end
REM of the selected range in case a range is selected.
REM This is likely to fail if cells in a table are selected.
oVC = oDoc.CurrentController.getViewCursor()
oVC.gotoRange(oVC.getEnd(), False)
For i = 0 To nNumLines
oText = oVC.getText()
oText.insertControlCharacter(oVC, _
com.sun.star.text.ControlCharacter.LINE_BREAK, False)
Next
End Sub
Sub InsertSimpleText(oDoc, Texto as string )
'Author: ChipperQ
Dim oDocument As Object
Dim oText As Object
Dim oViewCursor2 As Object
Dim oTextCursor2 As Object
oText = oDoc.Text
oViewCursor2 = oDoc.CurrentController.getViewCursor()
oTextCursor2 = oText.createTextCursorByRange(oViewCursor2.getEnd())
' Place the text to insert here
'oText.insertString(oTextCursor2, " "+Chr(13)+Chr(13), FALSE)
oText.insertString(oTextCursor2, Texto, FALSE)
End Sub
'------------------------------------------------------------------------------
' My code ---------------------------------------------------------------------
'------------------------------------------------------------------------------
Function n_primer(n as integer) as integer
' 0 --> 2
' 1 --> 3
' 2 --> 5
' ...
' 43 --> 197
Dim primes ()
primes=Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,43,_
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,101, 103,_
107, 109, 113, 127, 131,137, 139, 149,151,157, 163, 167,_
173, 179, 181, 193, 197)
n_primer=primes(n)
End Function
Sub Exercise (n_esercise as integer, max_exp as integer, max_primer as integer,_
max_n_fact as integer,max_number as Long)
'max_exp = valor màxim d'un exponent (>=1)
'max_primer = primer màxim: 1--> 2, 2 --> 3, 3 --> 5, 4 --> 7, etc...
'max_n_fact = nombre màxim de factors (>=1)
'max_number = valor màxim (>=1)
Dim Matriu_exponents(0 to 43) as integer
Dim n as integer
Dim primer as integer
Dim exponent as integer
Dim text_primer as string
Dim text_exponent as string
Dim factoritzacio as String
Dim valor as long
Dim first_factor as boolean
for n=1 to max_n_fact
primer=Int(max_primer*Rnd())
exponent=Int(max_exp*Rnd()+1)
Matriu_exponents(primer)=exponent
Next n
valor=1
factoritzacio=""
first_factor=true
for n=0 to 43
if Matriu_exponents(n)<>0 then
primer=n_primer(n)
exponent=Matriu_exponents(n)
if (valor*(primer^exponent) > Max_number) then exit for
text_primer=Str(primer)
if exponent <> 1 then
text_exponent="^" & Str(exponent)
else
text_exponent=""
end if
if not first_factor then
factoritzacio=factoritzacio & " cdot " & text_primer & text_exponent
else
factoritzacio=text_primer & text_exponent
first_factor=false
end if
valor=valor*(primer^exponent)
end if
next n
Wording(n_esercise)=Str(valor)
Solution(n_esercise)=factoritzacio
End sub
|
|
|
| Back to top |
|
 |
allirpa General User

Joined: 21 Aug 2008 Posts: 13 Location: Philippines
|
Posted: Wed Mar 04, 2009 2:53 pm Post subject: java implementation |
|
|
is there a way to do this using Java?
i have the same problem in creating a math editor plugin to open office but I should do it in Java.
anyone know how? |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|