Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producing subscripts in R markdown

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.

like image 820
sebastian-c Avatar asked Apr 02 '13 06:04

sebastian-c


People also ask

How do you add a superscript in Markdown?

Use the <sup></sup> tag ( <sub></sub> is the equivalent for subscripts).

How do you add a footnote in R Markdown?

To create a footnote in R Markdown, you use the carrot ^ followed immediately by square brackets []. Put the text inside of the [] and it'll print that at the bottom of the page. Code for a footnote will look like this: ^[This sentence will be printed as a footnote.] .

How do you produce italicized text in R Markdown?

To write text in italic font, use a single underscore or asterix before and after the text. To write text in bold font, use a double asterix or underscores before and after the text.

How do you write formulas in R Markdown?

Math inside RMarkdownIn side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”. Do not leave a space between the $ and your mathematical notation. Example: $\sum_{n=1}^{10} n^2$ is rendered as ∑10n=1n2.


2 Answers

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

like image 60
Daniel Rust Avatar answered Sep 19 '22 19:09

Daniel Rust


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

like image 28
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 19 '22 19:09

A5C1D2H2I1M1N2O1R2T1