Producing Subscripts in R Markdown

Producing subscripts in R markdown

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:

Sample Image

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:

Sample Image

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

Sample Image


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

Scientific formats, subscripts and superscripts in RMarkdown table (docx output)

You can use tildes (~) to put in subscript and carets (^) for superscripts; and use sprintf to get the expected digit format:

---
title: "Table won't work"
author: "Exhausted student"
date: "2022/01/28"
output:
bookdown::word_document2
---

```{r table, echo=F, warning=F, message=F}
library(tidyverse)

expSup <- function(x, digits=3) {
sprintf(paste0("%05.", digits, "f x 10^%d^"), x/10^floor(log10(abs(x))), floor(log10(abs(x))))
}

a <- tibble(
constants = c("c", "N~A~", "h", "e", "H~2~0"),
values = expSup(c(2.998e8, 6.022e-23, 6.626e-34, -1.602e-19, 18.02))
)

knitr::kable(a)
```

Sample Image

R-Markdown subscript in [square brackets]

Wrapping each square bracket with backticks solved my problem:

$\mu$Biomass~(Total)~`[`CO~2~ @ 400ppm`]`

This now works perfectly!

Is it possible to generate superscript in HTML from a string variable using RMarkdown?

The markdown notation is ^.^ and ~.~ to write super and subscripts.

testdata <- mtcars[1:2]
names(testdata) <- c("", "acceleration (m/sec^2^)")
knitr::kable(testdata)

Sample Image

Knitr - stop superscript in R Markdown

You may insert HTML tags directly to Markdown documents. To toggle the "superscripting" mode, use sup.

MyTitle
========================================================

J.Smith<sup>1</sup>, K.John<sup>2</sup>, H.Gone<sup>*</sup>.

EDIT: You may also use round brackets to indicate exactly the part of text to be reformatted.

J.Smith^(1), K.John^(2), H.Gone^(*)

EDIT: This has changed for Markdown V2

2^10  
2^(10)
2^10^
2^(10)^

Sample Image

How can I subscript names in a table from kable()?

To add subscripts in a rmarkdown document, you can embed your text between two tilde: text~sub~.
When using function kable, any text in the table is recognized as markdown syntax. So that your rmarkdown code should be:

```{r}
A <- data.frame(round(replicate(3, runif(2)),2))
rownames(A) <- c("Hola~123~", "Hola~234~")
names(A) <- c("X~1~", "X~2~", "X~3~")
knitr::kable(A)
```


Related Topics



Leave a reply



Submit