Latex plugins for Google Docs often have you provide Latex code between $$, for instance $$a+b$$.
Word now natively supports Latex code within it’s equation editor, which is plus. However, if you’ve prepared your document in elsewhere you need to tell Word that text between the $$ is Latex code for the equation editor. And deleting the $$ would be great to boot.
What’s the best way to write a Macro to do this?
For example consider the text:
When evaluating a product, there are two types of correct judgments: approving a product that meets standards (approve $$A$$, quality meets standards $$S$$) results in a payoff of $$v_h$$, while rejecting a product that does not meet standards (reject $$lnot A$$, quality does not meet standards $$lnot S$$) yields a payoff of $$v_c$$. Errors occur when a product is wrongly approved (approve $$A$$, quality does not meet standards $$lnot S$$), leading to a loss of $$v_f$$, or when a product that meets standards is wrongly rejected (reject $$lnot A$$, quality meets standards $$S$$), resulting in a loss of $$v_m$$. It is better to approve a product that meets standards than to miss it ($$v_h > v_m$$), but it is also better to reject a substandard product than to approve it unnecessarily ($$v_c > v_f$$). The likelihood that a product meets the required standards is given by the probability $$p = mathrm{P}(S)$$.
I tried this code, but it doesn’t work very well, and i’m extremely confused about how the scripting language works.
Sub ConvertToEquations()
Dim doc As Document
Dim rng As Range
Dim startPos As Long
Dim endPos As Long
Dim found As Boolean
Set doc = ActiveDocument
Set rng = doc.Content
' Move to the beginning of the document
rng.Collapse Direction:=wdCollapseStart
' Search for $$
Do While rng.Find.Execute(FindText:="$$", MatchWildcards:=False)
' Mark the position of the first $$
startPos = rng.Start + 2
' Move the range to the position after the first $$
rng.Start = startPos
' Search for the closing $$
found = rng.Find.Execute(FindText:="$$", MatchWildcards:=False)
If found Then
endPos = rng.Start
' Set the range for the text between $$
Set rng = doc.Range(startPos, endPos)
' Convert the text to an equation
rng.OMaths.Add rng
rng.OMaths.BuildUp
' Collapse the range to the end of the current range
rng.Collapse Direction:=wdCollapseEnd
' Move the range two characters forward
rng.MoveEnd Unit:=wdCharacter, Count:=2
rng.MoveStart Unit:=wdCharacter, Count:=2
Else
' If no closing $$ is found, exit the loop
Exit Do
End If
Loop
End Sub
CRTmonitor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.