Search code examples
rr-markdownkablekableextra

KableExtra math symbols


How can I insert 'equal or greater than' symbol in a kable? On the small rmarkdown doc below I tried two different ways based on some internet suggestions but neither work. I did try the argument 'escape=FALSE' but it does not work either. Thanks for any pointers...


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

```{r}
library(kableExtra)
library(knitr)

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("abundance of\n White Sharks\n $\\\\geq$ 40 inches","Percentage of \n White shark in 
the population\n $\\\\geq{40}$ inches")
```

```{r}
kable(a, "latex", booktabs = T)

```

This is what I get...

enter image description here


Solution

  • You need to end up with \geq in the Markdown file. To get that, you enter "\\geq" in the R string, and specify escape = FALSE in the call to kable(). That is,

    ---
    title: "Math symbols in column headers"
    date: "January 15, 2020"
    output: pdf_document
    ---
    
    ```{r}
    library(kableExtra)
    library(knitr)
    
    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("Abundance of\n White Sharks\n $\\geq 40$ inches","Percentage of \n White shark in 
    the population\n $\\geq 40$ inches")
    ```
    
    ```{r}
    kable(a, "latex", booktabs = T, escape = FALSE)
    
    ``` 
    

    This gives me

    enter image description here

    To respect the linebreaks, you need to use the linebreak function from kableExtra, i.e. something like this:

    colnames(a) <- linebreak(c("Abundance of\n White Sharks\n $\\geq 40$ inches",
                             "Percentage of \n White shark in the population\n $\\geq 40$ inches"))
    kable(a, "latex", booktabs = TRUE, escape = FALSE, align = "c") 
    

    which gives this output:

    screenshot 2