59

I'm aware that R markdown can produce superscripts:

text^superscript

But is it possible to produce proper subscripts? Or is the only way to do so to cheat and use LaTeX math mode:

$\sf{text_{subscript}}$

The intended final output is HTML.

zx8754
  • 52,746
  • 12
  • 114
  • 209
sebastian-c
  • 15,057
  • 3
  • 47
  • 93
  • 5
    I don't think so - the `markdown` package (which R markdown uses) has a 'superscript' markdown extension enabled, but I didn't recall a 'subscript' one. Since the intended output is HTML you could embed the HTML directly (`textsuperscript`) but that's not ideal for a markdown document. There certainly exist extensions for subscripts in markdown, but I am not sure how the R `markdown` package loads them in (i.e. how you would add an existing extension in). – mathematical.coffee Apr 02 '13 at 06:55
  • 1
    @mathematical.coffee This time it's for an HTML document, so `` tags would work fine, but sometimes I use pandoc to convert a markdown document to multiple different file types. – sebastian-c Apr 02 '13 at 11:11
  • 1
    `text~subscript~` and `text^superscript^` as in H~2~O is a liquid. 2^10^ is 1024. – swihart Jun 15 '16 at 17:44
  • 1
    @swihart That's basically Roger's answer below – sebastian-c Jun 16 '16 at 10:40

4 Answers4

47

R Markdown subscript is working normally as it should.

Maybe this is an old post. I'm using RStudio Version 0.99.902 + R Version 3.4 on a Mac.

Subscript: H~2~O is a liquid.
Superscript: 2^10^ is 1024.

Same exemple

Daniel Rust
  • 514
  • 5
  • 4
  • 3
    yes, you can see it was asked in 2013 (prehistoric in RStudio-time). It's good to have the update. – Ben Bolker Jun 03 '17 at 16:34
  • 4
    I think this should be marked as accepted answer. Vaguely related: if you try to produce sub- and superscripts using `papaja` in R Studio, you have to add an extra sign at the end of the string. I.e. `H~2~` or `R^2^`. – Mikko Mar 15 '18 at 11:07
  • 2
    2020 update: This answer worked for me. To get a subscript use `H~2~` – nniloc Jun 16 '20 at 17:57
  • I don't understand. rmarkdown's internal function `pandoc_convert` would return `$\delta_{0}$`, but to do it manually, you would need to use the convention of `δ~0~`? – aiorr Aug 29 '23 at 20:24
33

Since you mention Pandoc in your comments, maybe it's not cheating to depend on Pandoc's extensions for subscript and superscript. From here, we can create a minimal example Rmd file:

Testing Subscript and Superscript
========================================================

This is an R Markdown document. 

Pandoc includes numerous extensions to markdown, and one 
of them is *subscript* and *superscript*.

Here's the example from the Pandoc help page 
(http://johnmacfarlane.net/pandoc/README.html#superscripts-and-subscripts): 
H~2~O is a liquid.  2^10^ is 1024.

For fun, here's an R code block with some code from @Spacedman:

```{r}
list.depth <- function(this, thisdepth = 0) {
# http://stackoverflow.com/a/13433689/1270695
  if(!is.list(this)) {
    return(thisdepth)
  } else {
    return(max(unlist(lapply(this, list.depth, thisdepth = thisdepth+1))))    
  }
}
```

Using Knitr results in an HTML file that renders like this:

enter image description here

That clearly doesn't work. But you can run pandoc on the resulting markdown file (which I've named "Subscripts.md"):

pandoc -o Subscripts.html Subscripts.md -s -S

and you'll get this:

enter image description here

The CSS is different, but perhaps you can call pandoc with a custom CSS argument to use the same CSS used by Knitr.

Subscripts in PDF files also work as expected with that markdown file:

pandoc -o Subscripts.pdf Subscripts.md

enter image description here


Edit

If you want the pandoc output to match the visual appearance of the output when you knit with RStudio, download the CSS file that RStudio uses here and make a reference to that file when you create your HTML file from pandoc. (The following assumes you have kept the name as markdown.css an it is in the same directory as your other files.)

pandoc -o Subscripts.html Subscripts.md -s -S --css=markdown.css

Yet Another User
  • 2,627
  • 3
  • 18
  • 27
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
8

I found that the X~j~ syntax for subscripts works fine in Rmarkdown when knitting in RStudio. However, it does not work if you embed knitting in a shiny app. In my app,

  knit2html("Steps.Rmd")
  browseURL("Steps.html")

works fine except for the subscripts. But vanilla HTML subscript syntax will work in your Rmd document for both RStudio and from within a shiny app: X<sub>j</sub> renders as Xj.

Roger
  • 422
  • 4
  • 12
2

For R version 4.0.2 (2020-06-22) this works for me:

Subscript H~2~O~
Superscript R^2^
MattG
  • 5,589
  • 5
  • 36
  • 52