View source: R/ooxml_run_equation.R
| run_equation | R Documentation |
Create an equation written in LaTeX, to be used in a
paragraph made with fpar().
The equation is converted by the package 'equatags' into an 'Office'
equation ('Office Open XML Math'): it stays editable in 'Word' and
'PowerPoint'. Use it with body_add_fpar() in a 'Word' document, with
ph_with() in a 'PowerPoint' presentation and with sheet_add_drawing()
in an 'Excel' workbook, where equations are in a text box ('Excel' does
not accept equations in cells).
In HTML (to_html()), equations are drawn by 'KaTeX' (its stylesheet is
linked). In RTF documents (rtf_add()), they are images drawn by
'equatags' (R >= 4.3), placed on the baseline of the text; with an older
version of R, the LaTeX source is written as text, with a warning. 'Word'
does not leave room for the part of these images below the baseline: an
equation larger than the text, or with a fraction, can touch the next
line. A larger line spacing avoids it (fp_par(line_spacing = 1.3));
in the cells of a table, the margins of the cells leave this room.
run_equation(x, prop = NULL)
x |
the equation, a LaTeX string. The functions supported by 'KaTeX' can be used, see https://katex.org/docs/supported.html. |
prop |
formatting text properties returned by |
As in 'Word', the style of the equation depends on its paragraph:
an equation alone in its paragraph uses the display style (larger
fractions, limits above and below sums) and is aligned with the
alignment of the paragraph (argument text.align of fp_par());
an equation in a paragraph that contains something else, even a space, uses the inline style.
You can use this function in conjunction with fpar to create paragraphs consisting of differently formatted text parts. You can also use this function as an r chunk in an R Markdown document made with package officedown.
Other run functions for reporting:
external_img(),
floating_external_img(),
ftext(),
hyperlink_ftext(),
run_autonum(),
run_bookmark(),
run_columnbreak(),
run_comment(),
run_footnote(),
run_footnoteref(),
run_linebreak(),
run_pagebreak(),
run_reference(),
run_tab(),
run_word_field(),
run_wordtext()
library(officer)
quadratic <- "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
euler <- "e^{i\\pi} + 1 = 0"
doc <- read_docx()
# an equation alone in its paragraph: display style, centered
doc <- body_add_fpar(
doc,
fpar(run_equation(quadratic), fp_p = fp_par(text.align = "center"))
)
# an equation in a sentence: inline style, 14 points, in red
doc <- body_add_fpar(
doc,
fpar(
"Euler's identity: ",
run_equation(euler, prop = fp_text_lite(font.size = 14, color = "red")),
"."
)
)
print(doc, target = tempfile(fileext = ".docx"))
pres <- read_pptx()
pres <- add_slide(pres, layout = "Title and Content")
pres <- ph_with(
pres,
block_list(
fpar("The quadratic formula:"),
fpar(run_equation(quadratic), fp_p = fp_par(text.align = "center"))
),
location = ph_location_type("body")
)
print(pres, target = tempfile(fileext = ".pptx"))
wb <- read_xlsx()
wb <- sheet_add_drawing(
wb,
value = fpar(run_equation(quadratic)),
sheet = "Feuil1",
anchor = "C2"
)
print(wb, target = tempfile(fileext = ".xlsx"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.