Learn R Programming

R2wd (version 1.4)

wdEqn: Write formulas in Word

Description

This function allows writing formulas/equations into Word using the default formula editor in Word 2007 and 2010.

Usage

wdEqn(eqtext, bookmark = NULL, wdapp = .R2wd, paragraph = TRUE)

Arguments

eqtext
a text string containing the formula
bookmark
a text string containing the bookmark to use (otherwise default)
wdapp
the handle to the Word application
paragraph
whether to insert a paragraph after the equation

Value

  • none

Details

This function uses two tools to write a formula to Word. At first it uses the COM client to write a text into the range property of the selection and converts it to a formula. Then it uses the sendkeys method of the wsshell to add a carriage return. This causes Word to parse the formula and to substitute LaTeX like tokens such as \alpha by their corresponding formula equivalents (the greek letter alpha). In principle, it should be possible to achieve the parsing of the formula by invoking the BuildUp method of the omaths(1) object, but this does not work in the current versions of Word. The route via Sendkeys is fragile, so handle with caution.

Examples

Run this code
wdGet()
wdTitle("Try a formula")
## note that \ has to be used instead of \
## note that the , the following lines
## must be pasted into the console and run there.
## running them from an Emacs/ESS buffer won't work
wdEqn("\alpha \over ( \beta +3 ) ")
wdEqn("\alpha \over (( \beta +3 ))")
wdEqn("\matrix(\alpha &0@0&1 ) ")
## note: the handling of parentheses "(" is a bit peculiar in Word
## also experiment with the handling of empty spaces

Run the code above in your browser using DataLab