【问题标题】:Math symbols in gridExtra table column headersgridExtra 表列标题中的数学符号
【发布时间】:2020-01-16 06:03:11
【问题描述】:

我需要在我的两个列标题上写入大于或等于数学符号并打印一个 gridExtra 表,但无法使其工作。下面是小的 Rmarkdown 文档,显示了我正在尝试做的事情。我仍在使用 gridExtra 0.9.1,因为我所有的表都可以很好地使用这个版本。

    ---
    title: "Math symbols in column headers"
    date: "January 15, 2020"
    output: pdf_document
    ---

    ```{r}
    library(gridExtra)

    a <- structure(list(MLE = c(0.0839, 0.2082, 0.4194, 0.8237, 1.6201
    ), MME = c(0.0839, 0.2082, 0.4194, 0.8234, 1.6147)), class = "data.frame", row.names = c(NA, 
    5L)) 

    colnames(a) <- c("Estimated abundance of\n White Sharks\n $\\\\geq$ 40 inches in total length","Percentage of 3 year old\n White shark in the population\n $\\\\geq{40}$ inches in total length")

      grid.table(a)

```

我尝试了不同的变体,但我无法做到正确。有人能指出我正确的方向吗?我也尝试过使用 kableExtra,但没有成功。这是我得到的,请注意我的列标题:

【问题讨论】:

  • 使用"... \u2265 40 inches ..."
  • @Roland,您是否使它适用于我的示例?它仍然无法正确打印到我这边。实际上,它起到了部分作用。 \u2265 用于“=”符号,但我需要“等于或大于”符号。有地方找这些吗?
  • 显然,我没有费心去创建一个markdown文档,但是我用你的R代码测试了它,它可以工作。如果它不适合您,您可能会遇到 markdown 的编码问题。可能,您需要设置一些选项以确保使用 UTF-8 作为编码。
  • 它似乎在 R 控制台中作为表达式工作,但在 Rmarkdown 文档中显示为“=”。
  • 它似乎对 html 文档按预期工作,但对 pdf 文档没有

标签: r grid latex r-markdown gridextra


【解决方案1】:

使用ttheme_default(colhead=list(fg_params = list(parse=TRUE))) 添加主题以在列标题中使用绘图符号。


Rmarkdown

---
title: "Math symbols in column headers"
date: "January 15, 2020"
output: pdf_document
---

```{r, echo = FALSE}
library(gridExtra)

a <- data.frame(
  MLE = c(0.0839, 0.2082, 0.4194, 0.8237, 1.6201),
  MME = c(0.0839, 0.2082, 0.4194, 0.8234, 1.6147)
)

colnames(a) <- c(bquote(atop("Estimated abundance of White Sharks", "" >= 40 ~ "inches in total length")), 
                 bquote(atop("Percentage of 3 year old White Sharks", "" >= 40 ~ "inches in total length"))) 

tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)))
grid.table(a, theme=tt)
```

注意:换行符现在由atop 指定,因为bquote 不会解释\n 换行符


PDF 输出

【讨论】:

  • 乔里斯周;谢谢你的建议。它适用于新版本的 gridExtra,但不幸的是它不适用于我正在使用的 gridExtra 版本(0.9.1)。我没有更新到新版本,因为我的所有报告都包含多个使用旧版本构建的表格,并且不适用于新版本。我仍在使用如下格式: tableGrob(format(x),gpar.corefill = gpar(fill = "lightblue", alpha = 0.3,col="grey90"), h.even.alpha = 0.2,show.rownames=FALSE ) 它似乎不适用于新版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 2014-02-05
  • 2014-08-23
相关资源
最近更新 更多