You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarto渲染PDF时gt()函数cols_merge中±符号报错求助

解决gt包在Quarto PDF渲染中±符号显示问题

方案1:使用原生Unicode±字符

直接将±替换为Unicode的±字符,这种方式同时兼容HTML和PDF输出,无需额外判断:

reliabilitet2 |> 
  gt() |> 
  fmt_number(decimals = 2) |> 
  cols_merge(
    columns = c(m, s), 
    pattern = "{1} ± {2}"
  )

方案2:根据输出格式动态切换符号

借助Quarto的输出判断函数,针对HTML和PDF分别使用对应的符号语法,确保两种格式都正常渲染:

library(gt)
library(quarto)

reliabilitet2 |> 
  gt() |> 
  fmt_number(decimals = 2) |> 
  cols_merge(
    columns = c(m, s), 
    pattern = if (is_html_output()) {
      "{1} `±` {2}"
    } else {
      "{1} $\\pm$ {2}"
    }
  )

注意:需要提前加载quarto包,且LaTeX语法中的反斜杠需要用两个(\\)转义,因为R字符串会解析单个反斜杠为转义符。

方案3:使用text_transform自定义内容渲染

通过text_transform直接修改目标列内容,隐藏原s列,实现跨格式的±符号显示:

reliabilitet2 |> 
  gt() |> 
  fmt_number(decimals = 2) |> 
  text_transform(
    locations = cells_body(columns = m),
    fn = function(x) {
      s_vals = reliabilitet2$s
      if (is_html_output()) {
        paste0(x, " ± ", s_vals)
      } else {
        paste0(x, " $\\pm$ ", s_vals)
      }
    }
  ) |> 
  cols_hide(columns = s)

内容的提问来源于stack exchange,提问作者Leif

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:07:13