设置字体家庭和方程式的大小。

发布于 2025-01-20 04:00:23 字数 1266 浏览 6 评论 0原文

我正在寻找一个选项来设置 flextable 中方程的字体系列和大小。

一般来说,表格、行和列的字体系列和大小可以通过糖函数 flextable::fontflextable::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2025-01-27 04:00:23

要控制行高,您需要指定 hrule(ft, i = 1:3,rule = 'atleast') 以及通过 height_all 指定的高度(以英寸为单位)

ft <- flextable(df)
ft <- compose(
  x = ft, j = "formula",
  value = as_paragraph(as_equation(formula, width = 3, height = 2))
)
ft <- width(ft, j = 1:2, width = 2)
ft <- hrule(ft, i = 1:3, rule = 'atleast')
ft <- height_all(ft, height = 1)
ft <- fontsize(ft, size = 20, part = "all")

。 ,这不会改变方程的大小:

在此处输入图像描述

构成第二列的 mathjax 公式(包括文本字符)将呈现为 SVG 路径,并且它们的大小和字体系列都是固定的。

如果您深入研究 flextable 代码,您会发现当您执行

  1. print(ft) 时,它会调用
  2. flextable:::print.flextable ,而 flextable:::print.flextable 会调用
  3. htmltools_value(ft) ,它调用
  4. flextable:::html_str(ft),它调用
  5. flextable:::html_gen(ft),生成实际的 html。

公式字符串直接在 html_gen 内部传递到 equatags::transform_mathjax,它不采用任何大小或字体系列参数,只是输出默认的 mathjax svg。 svg 图像以固定大小合并到表格单元格中。

为了改变 svgs 的大小,你需要参与 svg hacking,这在简单缩放的情况下并不太困难:

html_format <- as.character(htmltools_value(ft))
html_format <- gsub('<svg ',
                    '<svg transform=\"scale(2)\" ',
                    html_format, fixed = TRUE)

html_format 对象只是 svg 的 html 字符串。灵活并且可以渲染
像这样:

dir <- tempfile()
dir.create(dir)
htmlFile <- file.path(dir, "index.html")
writeLines(html_format, con = htmlFile)
rstudioapi::viewer(htmlFile)

导致

在此处输入图像描述

当然,这一切都不是理想的,但这只是 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 via height_all

ft <- flextable(df)
ft <- compose(
  x = ft, j = "formula",
  value = as_paragraph(as_equation(formula, width = 3, height = 2))
)
ft <- width(ft, j = 1:2, width = 2)
ft <- hrule(ft, i = 1:3, rule = 'atleast')
ft <- height_all(ft, height = 1)
ft <- fontsize(ft, size = 20, part = "all")

Unfortunately, that doesn't change the size of the equations:

enter image description here

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

  1. print(ft) it calls
  2. flextable:::print.flextable which calls
  3. htmltools_value(ft) , which calls
  4. flextable:::html_str(ft), which calls
  5. flextable:::html_gen(ft), which generates the actual html.

The formula strings are passed directly inside html_gen to equatags::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:

html_format <- as.character(htmltools_value(ft))
html_format <- gsub('<svg ',
                    '<svg transform=\"scale(2)\" ',
                    html_format, fixed = TRUE)

The html_format object is just the html string of the flextable and can be rendered
like this:

dir <- tempfile()
dir.create(dir)
htmlFile <- file.path(dir, "index.html")
writeLines(html_format, con = htmlFile)
rstudioapi::viewer(htmlFile)

Resulting in

enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文