1

I need to write the greater or equal than math symbol on two of my column headers and printing a gridExtra table but can't make it work. Below is small Rmarkdown document showing what I am trying to do. I am still using gridExtra 0.9.1 because all of my tables work nicely with this version.

    ---
    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)

```

I have tried different variations but I can't get it right. Can someone point me on the right direction? I also tried using kableExtra with no luck. This is what I get, notice my column headers:

enter image description here

Salvador
  • 1,229
  • 1
  • 11
  • 19
  • 1
    Use `"... \u2265 40 inches ..."`. – Roland Jan 16 '20 at 06:49
  • @Roland, did you make it work with my example? It still does not print correctly on my end. Actually, it worked partially. \u2265 is for the '=' sign but I need 'equal or greater than' sign. Is there a place to look for these? – Salvador Jan 16 '20 at 06:53
  • Obviously, I did not go to the trouble of creating a markdown document, but I tested it with your R code and it works. If it doesn't for you, you might have encoding issues with markdown. Possibly, you need to set some option to ensure UTF-8 is used as encoding. – Roland Jan 16 '20 at 06:55
  • it seems to work as expression in the R console but it shows as "=" inside the Rmarkdown document. – Salvador Jan 16 '20 at 07:38
  • It seems to work as expected with html documents but no with pdf docs – Salvador Jan 16 '20 at 08:01

1 Answers1

1

Add a theme with ttheme_default(colhead=list(fg_params = list(parse=TRUE))) to use plotmath notation in the column headers.


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)
```

Note: the line breaks are now specified by atop, since bquote does not interpret \n linebreaks


PDF output

screen

Joris C.
  • 5,721
  • 3
  • 12
  • 27
  • Joris Chau; Thanks for your suggestion. It works nicely with the new version of gridExtra but unfortunately it doesn't work with the version of gridExtra that I am using (0.9.1). I have no updated to the new version because all of my reports contain multiple tables that were built with the older version and don't work with the new version. I am still using formatting like: tableGrob(format(x),gpar.corefill = gpar(fill = "lightblue", alpha = 0.3,col="grey90"), h.even.alpha = 0.2,show.rownames=FALSE) which it doesn't appear to work with the new version. – Salvador Jan 16 '20 at 16:08