Faceți căutări pe acest blog

luni, 3 august 2015

Using Automation to create Equations in Word

Starting with MS Word 2007, a new type of equations can be embeded.
This new type came from Latex. Latex syntax can be used, and is autocorrected by Word, by replacing Latex keywords into Unicode special characters.
Also, the graphic interface can be used to generate these new kind of equations.

But these new type of equations can be easy created using automation.
The steps
1 Create the Unicode string
2 Add the range to the oMaths object
3 Create the oMath object
4 Convert the equation to the professional format (BuildUp the equation)
Steps 2-4 are the similar for every equation

The Unicode chars can be passed to Word as a blob constant, or using the range.insertsymbol() method.
For example, the product symbol can be inserted in Word in two ways :
orange.insertsymbol(0x220F,,.T.)
or as a blob constant
orange.insertafter(0h0F22)
(note the reversed order of the two bytes)

Examples 
1) Here is a first and very simple sample (a fraction)
owrd=CREATEOBJECT("Word.Application")
odoc=owrd.documents.add()
owrd.visible=.T.
orange=odoc.range()

* fraction
orange.Move (6)  && ctrl+end

* 1 the string
orange.text="(1.4+3)/23"+CHR(13)
* 2 Add the range to the oMath object
objRange2 = orange.OMaths.Add(orange)
* 3 create the oMath object
objEq = objRange2.OMaths(1)
* 4 Convert the equation to the professional format
objEq.BuildUp


2) The next example is almost identical. The only difference is the first step (* 1 the string)
Permutation (simple)
Permutations are delimited by brackets. The row separator is ¦ chr(166)
owrd=CREATEOBJECT("Word.Application")
odoc=owrd.documents.add()
owrd.visible=.T.
orange=odoc.range()

orange.Move (6)  && ctrl+end

* 1 the string
orange.text="(n¦k)"+CHR(13)  &&&&&&& this is the difference &&&&
* 2 Add the range to the oMath object
objRange2 = orange.OMaths.Add(orange)
* 3 create the oMath object
objEq = objRange2.OMaths(1)
* 4 Convert the equation to the professional format
objEq.BuildUp


3) For a more complex permutation, the expressions from the upper / lower row are enclosed by brackets
The only difference is contained in this row
orange.text="((n+1)¦(k+1))"+CHR(13)

 4) Superscript
The character for superscript is ^
If needed, the expressions from the base and from the superscript row are enclosed by brackets
orange.text="(x+1)^(y+2)"+CHR(13)

 5) Subscript
The character for subscript is _ (underscore)
orange.text="x_1"+CHR(13) 

 6) Square root
The symbol for square root is Unicode 8730 (0x221A)
 * 1 the string
orange.Move (6)  && ctrl+end
orange0=odoc.range(orange.start,orange.start)
orange.insertsymbol(8730,,.T.) && radical
orange.Move (6)  && ctrl+end
orange.insertafter("(x^(2^3)+y^2)"+CHR(13)) && superscript is ^
* 2 Add the range to the oMath object
* etc.


 7) Radical of order n
 Is similar with square root. First is placed Unicode 8730 (0x221A), second the order of the radical then an ampersand (&) and finally the expression
* 1 the string
orange.Move (6)  && ctrl+end
orange0=odoc.range(orange.start,orange.start)
orange.insertsymbol(8730,,.T.) && radical
orange.Move (6)  && ctrl+end
orange.text="(n&u)"+CHR(13) && n is the order of the radical, u is the expression under radical
* 2 Add the range to the oMath object
* etc.


8) Sum, product, integral, reunion, intersection, or, and
First is placed the Unicode character specific for each function. (*)
Then is placed Unicode character 9618 (0x2592) (the placeholder for the expression affected by the function)
If a lower bound is present, is added like any subscript (_expression)
If a upper bound is present, is added like any superscript (^expression)
Finally the general term
* Characters
- product 0x220F
- product overturned 0x2210
- sum 0x2211
- and 0x22C0
- or 0x22C1
- intersection 0x22C2
- reunion 0x22C3
- integral 0x222B
- double integral 0x222C
- tripple 0x222D
- line integral 0x222E
- double line integral 0x222F
- tripple line integral 0x2230
Example for sum
* 1 the string
orange.Move (6)  && ctrl+end
orange0=odoc.range(orange.start,orange.start)
orange.insertsymbol(8721,,.T.) && sum
orange.Move (6)  && ctrl+end
orange.insertafter("_(i=1)^n ") && _ lower bound (like subscript) ^upper bound (like superscript)
orange.Move (6)  && ctrl+end
orange.insertsymbol(9618,,.T.) && placeholder
orange.Move (6)  && ctrl+end
orange.insertafter("i=n(n+1)/2"+CHR(13)) && terms (what is summed)
* 2 Add the range to the oMath object
* etc.


9) Matrix
Symbol for matrix is Unicode 9632 (0x25A0) followed by an open brackets
Column break is &
Row break is @
Finally is the closed brackets
The whole matrix can be enclosed by different types of brackets
Example Matrix enclosed by square brackets
* 1 the string
orange.Move (6)  && ctrl+end
orange0=odoc.range(orange.start,orange.start)
orange.insertafter("[")
orange.Move (6)  && ctrl+end
orange.insertsymbol(9632,,.T.) && simbol for matrix
orange.Move (6)  && ctrl+end
orange.insertafter("(1 & 2 & 3 @ 4 & 5 & 6)]"+CHR(13))
* 2 Add the range to the oMath object
* etc.


Here is a list with many Unicode characters used

Niciun comentariu:

Trimiteți un comentariu