Producing subscripts in R markdown

RMarkdownKnitrSubscriptR Markdown

R Problem Overview


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.

R Solutions


Solution 1 - R

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: H2O is a liquid.
Superscript: 2^10^ is 1024.

Same exemple

Solution 2 - R

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

Solution 3 - R

I found that the Xj 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.

Solution 4 - R

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

Subscript H~2~O~
Superscript R^2^

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsebastian-cView Question on Stackoverflow
Solution 1 - RDaniel RustView Answer on Stackoverflow
Solution 2 - RA5C1D2H2I1M1N2O1R2T1View Answer on Stackoverflow
Solution 3 - RRogerView Answer on Stackoverflow
Solution 4 - RMattGView Answer on Stackoverflow