I am trying to write a Word macro to expand the Christoffel symbol automatically. The expansion is simple:$$
\Gamma_{\mu\nu}^\sigma=\frac{1}{2}g^{\sigma\rho}\left(\partial_\mu g_{\nu\rho}+\partial_\nu g_{\rho\mu}-\partial_\rho g_{\mu\nu}\right)
$$One replaces the Christoffel symbol ##\Gamma_{\mu\nu}^\sigma## by a half times the inverse metric ##g^{\sigma\rho}## times the sum of slightly different index combinations of the partial derivative ##\partial_\mu## of the metric ##g_{\nu\rho}##. A new dummy index, ## \rho## in this case, is introduced (it is summed over) and the original indices ## \sigma,\mu,\nu## are placed carefully in the expansion. The Christoffel symbol occurs frequently in General Relativity and once I have done this a few times with different indices my eyes start to pop out, thus the motivation to write a macro to save said eyes.
The macro should be fairly straightforward. We want it to replace something like$$
A\Gamma_{\mu\nu}^\sigma X
$$by$$
A\frac{1}{2}g^{\sigma\rho}\left(\partial_\mu g_{\nu\rho}+\partial_\nu g_{\rho\mu}-\partial_\rho g_{\mu\nu}\right)X
$$An equation is an OMath object which consists of OMathFunction objects. So each of ## A,\Gamma_{\mu\nu}^\sigma,X## in the first equation is an OMathFunction object. In theory it should be easy to replace the OMathFunction object ##\Gamma_{\mu\nu}^\sigma## by a bunch of new OMathFunction objects ##\frac{1}{2},g^{\sigma\rho},+\ etc##. Dream on!
When you want to add a new OMathFunction object, you need to call Add method of an OMathFunctions object. The OMathFunctions object is the list of OMathFunction objects in the equation. The second parameter of this Add method is the type of new OMathFunction to add. (This not well described in the documentation.) The list of types is here. So the fraction ##\frac{1}{2}## is an wdOMathFunctionFrac, the inverse metric which has superscripts ##g^{\sigma\rho}## is a wdOMathFunctionScrSup and ##+## is a wdOMathFunctionText. You can see all these in the debugger if you have a look at the OMathFunctions object. This Add method crashes with error 6219 if the type parameter is wdOMathFunctionText, so it is very difficult to get the ##+,-## signs into expansion. The only way I could discover was by using
Selection.TypeText ("+")
I could then insert ##g_{\mu\nu}+g^{\mu\nu}## at the selection point in an equation with this code:
Sub ExampleWriteExpression()
'insertion point should be in equation. Metric + inverse metric inserted
Dim Equation As OMath
Dim MathTerm As OMathFunction
If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
Set Equation = Selection.OMaths(1)
Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionScrSub)
MathTerm.ScrSub.E.Range = "g"
MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
Selection.TypeText ("+")
'Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionText) still gets error
Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionScrSup)
MathTerm.ScrSup.E.Range = "g"
MathTerm.ScrSup.Sup.Range = ChrW(&H3BC) & ChrW(&H3BD)
End Sub
There was quite a bit of difficulty in getting the selection in the right place and I kept getting things like$$
\frac{1}{2}g^{\sigma\rho}\left(\partial_\mu g_{\nu\rho}\partial_\nu g_{\rho\mu}\partial_\rho g_{\mu\nu}\right)+-
$$It is necessary align the selection range with the equation range and this is not the simple matter of subtraction that you might expect! It now works properly and replaces multiple Christoffel symbols in an equation correctly. Click Read more below if you would like a look or copy. Covariant derivatives, Riemann tensors, using, metric and coordinates coming soon!
jpl on msofficeforums corrected me and showed me how to avoid using the Selection which is a bodge. I further refined his technique. It is now very beautiful.😅
\Gamma_{\mu\nu}^\sigma=\frac{1}{2}g^{\sigma\rho}\left(\partial_\mu g_{\nu\rho}+\partial_\nu g_{\rho\mu}-\partial_\rho g_{\mu\nu}\right)
$$One replaces the Christoffel symbol ##\Gamma_{\mu\nu}^\sigma## by a half times the inverse metric ##g^{\sigma\rho}## times the sum of slightly different index combinations of the partial derivative ##\partial_\mu## of the metric ##g_{\nu\rho}##. A new dummy index, ## \rho## in this case, is introduced (it is summed over) and the original indices ## \sigma,\mu,\nu## are placed carefully in the expansion. The Christoffel symbol occurs frequently in General Relativity and once I have done this a few times with different indices my eyes start to pop out, thus the motivation to write a macro to save said eyes.
The macro should be fairly straightforward. We want it to replace something like$$
A\Gamma_{\mu\nu}^\sigma X
$$by$$
A\frac{1}{2}g^{\sigma\rho}\left(\partial_\mu g_{\nu\rho}+\partial_\nu g_{\rho\mu}-\partial_\rho g_{\mu\nu}\right)X
$$An equation is an OMath object which consists of OMathFunction objects. So each of ## A,\Gamma_{\mu\nu}^\sigma,X## in the first equation is an OMathFunction object. In theory it should be easy to replace the OMathFunction object ##\Gamma_{\mu\nu}^\sigma## by a bunch of new OMathFunction objects ##\frac{1}{2},g^{\sigma\rho},+\ etc##. Dream on!
When you want to add a new OMathFunction object, you need to call Add method of an OMathFunctions object. The OMathFunctions object is the list of OMathFunction objects in the equation. The second parameter of this Add method is the type of new OMathFunction to add. (This not well described in the documentation.) The list of types is here. So the fraction ##\frac{1}{2}## is an wdOMathFunctionFrac, the inverse metric which has superscripts ##g^{\sigma\rho}## is a wdOMathFunctionScrSup and ##+## is a wdOMathFunctionText. You can see all these in the debugger if you have a look at the OMathFunctions object. This Add method crashes with error 6219 if the type parameter is wdOMathFunctionText, so it is very difficult to get the ##+,-## signs into expansion. The only way I could discover was by using
Selection.TypeText ("+")
I could then insert ##g_{\mu\nu}+g^{\mu\nu}## at the selection point in an equation with this code:
Sub ExampleWriteExpression()
'insertion point should be in equation. Metric + inverse metric inserted
Dim Equation As OMath
Dim MathTerm As OMathFunction
If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
Set Equation = Selection.OMaths(1)
Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionScrSub)
MathTerm.ScrSub.E.Range = "g"
MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
Selection.TypeText ("+")
'Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionText) still gets error
Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionScrSup)
MathTerm.ScrSup.E.Range = "g"
MathTerm.ScrSup.Sup.Range = ChrW(&H3BC) & ChrW(&H3BD)
End Sub
There was quite a bit of difficulty in getting the selection in the right place and I kept getting things like$$
\frac{1}{2}g^{\sigma\rho}\left(\partial_\mu g_{\nu\rho}\partial_\nu g_{\rho\mu}\partial_\rho g_{\mu\nu}\right)+-
$$It is necessary align the selection range with the equation range and this is not the simple matter of subtraction that you might expect! It now works properly and replaces multiple Christoffel symbols in an equation correctly. Click Read more below if you would like a look or copy. Covariant derivatives, Riemann tensors, using, metric and coordinates coming soon!
jpl on msofficeforums corrected me and showed me how to avoid using the Selection which is a bodge. I further refined his technique. It is now very beautiful.😅