JohnDoedel
JohnDoedel

Reputation: 141

How do I get superscript in tinytex data.table()

I am printing tables - in PDFs - by using data.table(). In these tables, I want some texts with superscript. I've tried a couples of things, but I only find solutions which are using kable() or tibble() - but I really want to use data.table() or solutions which are using expression() - which I cannot get working in PDFs.

Here is some simple example code.

library(data.table)

some_table <- data.table(
    element = c('Some text with <1> in superscript at the end',
                'Some more text')
    )

Thank you in advance!

Upvotes: 2

Views: 411

Answers (2)

s_baldur
s_baldur

Reputation: 33613

Two options with Rmarkdown + LaTeX:

---
title: "Untitled"
output: pdf_document
---

```{r}
library(kableExtra)
levels(iris$Species)[1] <- "setosa\\textsuperscript{*}"
names(iris)[5]          <- "Species$^x$"
kable(iris[1:2, ], format='latex', escape=FALSE)
```

enter image description here

Upvotes: 0

Quinten
Quinten

Reputation: 41593

You can use sup for superscript and sub for subscript. Maybe you want something like this:

library(DT)
some_table <- datatable(
  data.frame(c(1, 2), 
             row.names = c("Some text with A<sup>1</sup>", "Some more text")), rownames = T, escape = FALSE)

some_table

Output:

enter image description here

Not supported with data.table

It seems like that superscripts expressions are not supported with the data.table package. You get the following error when trying:

Error in `[.data.table`(x, i, , ) : Internal error: column type 'expression' not supported by data.table subset. All known types are supported so please report as bug.

When using this code:

library(data.table)

    my_string <- "'title'^2"
    my_title <- parse(text=my_string)
    
    some_table <- data.table(
        element = c('Some text with', my_title, 'in superscript at the end',
                    'Some more text')
        )
    
    some_table
    ```

It seems currently not available for `data.table`.

Upvotes: 1

Related Questions