设置字体家庭和方程式的大小。
我正在寻找一个选项来设置 flextable
中方程的字体系列和大小。
一般来说,表格、行和列的字体系列和大小可以通过糖函数 flextable::font
和 flextable::fontsize
设置。但是,无论是在 HTML 输出中还是在导出到 docx 时,两者都不会影响字体系列和方程的大小。
运行下面的 reprex 将为 text 列提供正确的字体系列和大小,但不会为 formula 列提供正确的字体系列和大小。
library(flextable)
# Note: Running the reprex requires the `equatags` package.
# Also equatags::mathjax_install() must be executed
# to install necessary dependencies. See ?flextable::as_equation.
eqs <- c(
"(ax^2 + bx + c = 0)",
"a \\ne 0",
"x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}"
)
text = LETTERS[1:3]
df <- data.frame(text = text, formula = eqs)
df
#> text formula
#> 1 A (ax^2 + bx + c = 0)
#> 2 B a \\ne 0
#> 3 C x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}
ft <- flextable(df)
ft <- compose(
x = ft, j = "formula",
value = as_paragraph(as_equation(formula, width = 2))
)
ft <- width(ft, j = 2, width = 2)
ft <- fontsize(ft, size = 20, part = "all")
fn <- tempfile(fileext = ".docx")
save_as_docx(ft, path = fn)
if (FALSE) fs::file_show(fn) # Set to TRUE to show file
I'm looking for an option to set the the font family and size of equations in a flextable
.
In general the font family and size of the table, rows and columns could be set via the sugar functions flextable::font
and flextable::fontsize
. However, both have no effect on the font family and size of equations neither in the HTML output nor when exporting to docx.
Running the reprex below gives the correct font family and size for the text column but not for the formula column.
library(flextable)
# Note: Running the reprex requires the `equatags` package.
# Also equatags::mathjax_install() must be executed
# to install necessary dependencies. See ?flextable::as_equation.
eqs <- c(
"(ax^2 + bx + c = 0)",
"a \\ne 0",
"x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}"
)
text = LETTERS[1:3]
df <- data.frame(text = text, formula = eqs)
df
#> text formula
#> 1 A (ax^2 + bx + c = 0)
#> 2 B a \\ne 0
#> 3 C x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}
ft <- flextable(df)
ft <- compose(
x = ft, j = "formula",
value = as_paragraph(as_equation(formula, width = 2))
)
ft <- width(ft, j = 2, width = 2)
ft <- fontsize(ft, size = 20, part = "all")
fn <- tempfile(fileext = ".docx")
save_as_docx(ft, path = fn)
if (FALSE) fs::file_show(fn) # Set to TRUE to show file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要控制行高,您需要指定
hrule(ft, i = 1:3,rule = 'atleast')
以及通过height_all
指定的高度(以英寸为单位)。 ,这不会改变方程的大小:
构成第二列的 mathjax 公式(包括文本字符)将呈现为 SVG 路径,并且它们的大小和字体系列都是固定的。
如果您深入研究 flextable 代码,您会发现当您执行
print(ft)
时,它会调用flextable:::print.flextable
,而flextable:::print.flextable
会调用htmltools_value(ft)
,它调用flextable:::html_str(ft)
,它调用flextable:::html_gen(ft)
,生成实际的 html。公式字符串直接在
html_gen
内部传递到equatags::transform_mathjax
,它不采用任何大小或字体系列参数,只是输出默认的 mathjax svg。 svg 图像以固定大小合并到表格单元格中。为了改变 svgs 的大小,你需要参与 svg hacking,这在简单缩放的情况下并不太困难:
html_format
对象只是 svg 的 html 字符串。灵活并且可以渲染像这样:
导致
当然,这一切都不是理想的,但这只是 flextable 通过 equatags 呈现公式的方式的限制。
不幸的是,Mathjax 不允许使用任意字体,所以这会更难实现。
To control the row heights, you need to specify
hrule(ft, i = 1:3, rule = 'atleast')
as well as the height in inches viaheight_all
Unfortunately, that doesn't change the size of the equations:
The mathjax formulas that make up your second column (including the text characters) are rendered as SVG paths, and both their size and font family are fixed.
If you dig into the flextable code you will see that when you do
print(ft)
it callsflextable:::print.flextable
which callshtmltools_value(ft)
, which callsflextable:::html_str(ft)
, which callsflextable:::html_gen(ft)
, which generates the actual html.The formula strings are passed directly inside
html_gen
toequatags::transform_mathjax
, which doesn't take any size or font family parameters, and just spits out default mathjax svg. The svg images are incorporated into the table cells at a fixed size.In order to change the size of the svgs, you would need to get involved in svg hacking, which in the case of simple scaling isn't too difficult:
The
html_format
object is just the html string of the flextable and can be renderedlike this:
Resulting in
None of this is ideal, or course, but it just a limitation of how flextable renders formulas though equatags.
Unfortunately, Mathjax does not allow for arbitrary fonts to be used, so that would be even harder to achieve.