Superscript in markdown (Github flavored)?
Asked Answered
T

4

605

Following this lead, I tried this in a Github README.md:

<span style="vertical-align: baseline; position: relative;top: -0.5em;>text in superscript</span>

Does not work, the text appears as normal. Help?

Thickleaf answered 1/3, 2013 at 10:42 Comment(1)
For those who end up here but are using pandoc, try ^superscript^ (pandoc.org/MANUAL.html#superscripts-and-subscripts).Lamrert
F
929

Use the <sup></sup> tag (<sub></sub> is the equivalent for subscripts). See this gist for an example.

Fateful answered 1/3, 2013 at 11:25 Comment(5)
So to write O(n^2) sensibly, I would just type O(n<sup>2</sup>), and it would appear as O(n<sup>2</sup>). Nice. Now why didn't that work on SO markdown?Juratory
@phonetagger: No tags allowed in comments, IIRC.Measured
And how would you do the LaTeX logo? Does CSS in a style attribute work on <sup> and <sub>?Wien
@PeterH.Boling Stack Overflow comments.Schwerin
For asterisk (), use "*" directly to avoid "" being recognized as a function.Rejoice
S
131

You have a few options for this. The answer depends on exactly what you're trying to do, how readable you want the content to be when viewed as Markdown and where your content will be rendered:

HTML Tags

As others have said, <sup> and <sub> tags work well for arbitrary text. Embedding HTML in a Markdown document like this is well supported so this approach should work with most tools that render Markdown.

Personally, I find HTML impairs the readable of Markdown somewhat, when working with it "bare" (eg. in a text editor) but small tags like this aren't too bad.

LaTeX (New!)

As of May 2022, GitHub supports embedding LaTeX expressions in Markdown docs directly. This gives us new way to render arbitrary text as superscript or subscript in GitHub flavoured Markdown, and it works quite well.

LaTeX expressions are delineated by $$ for blocks or $ for inline expressions. In LaTeX you indicate superscript with the ^ and subscript with _. Curly braces ({ and }) can be used to group characters. You also need to escape spaces with a backslash. The GitHub implementation uses MathJax so see their docs for what else is possible.

You can use super or subscript for mathematical expressions that require it, eg:

$$e^{-\frac{t}{RC}}$$

Which renders as..

Superscript maths example

Or render arbitrary text as super or subscript inline, eg:

And so it was indeed: she was now only $_{ten\ inches\ high}$, and her face brightened up at the thought that she was now the right size for going through the little door into that lovely garden.

Which renders as..

Subscript text example

I've put a few other examples here in a Gist.

Unicode

If the superscript (or subscript) you need is of a mathematical nature, Unicode may well have you covered.

I've compiled a list of all the Unicode super and subscript characters I could identify in this gist. Some of the more common/useful ones are:

  • SUPERSCRIPT ZERO (U+2070)
  • ¹ SUPERSCRIPT ONE (U+00B9)
  • ² SUPERSCRIPT TWO (U+00B2)
  • ³ SUPERSCRIPT THREE (U+00B3)
  • SUPERSCRIPT LATIN SMALL LETTER N (U+207F)

People also often reach for <sup> and <sub> tags in an attempt to render specific symbols like these:

  • TRADE MARK SIGN (U+2122)
  • ® REGISTERED SIGN (U+00AE)
  • SERVICE MARK (U+2120)

Assuming your editor supports Unicode, you can copy and paste the characters above directly into your document or find them in your systems emoji and symbols picker.

On MacOS, simultaneously press the Command ⌘ + Control + Space keys to open the emoji picker. You can browse or search, or click the small icon in the top right to open the more advanced Character Viewer.

On Windows, you can a emoji and symbol picker by pressing ⊞ Windows + ..

Alternatively, if you're putting these characters in an HTML document, you could use the hex values above in an HTML character escape. Eg, &#x00B2; instead of ². This works with GitHub (and should work anywhere else your Markdown is rendered to HTML) but is less readable when presented as raw text.

Images

If your requirements are especially unusual, you can always just inline an image. The GitHub supported syntax is:

![Alt text goes here, if you'd like](path/to/image.png) 

You can use a full path (eg. starting with https:// or http://) but it's often easier to use a relative path, which will load the image from the repo, relative to the Markdown document.

If you happen to know LaTeX (or want to learn it) you could do just about any text manipulation imaginable and render it to an image. Sites like Quicklatex make this quite easy. Of course, if you know your document will be rendered on GitHub, you can use the new (2022) embedded LaTeX syntax discussed earlier)

Stevens answered 17/8, 2018 at 9:17 Comment(0)
S
22

Comments about previous answers

The universal solution is using the HTML tag <sup>, as suggested in the main answer.
However, the idea behind Markdown is precisely to avoid the use of such tags:
The document should look nice as plain text, not only when rendered.

Another answer proposes using Unicode characters, which makes the document look nice as a plain text document but could reduce compatibility.

Finally, I would like to remember the simplest solution for some documents: the character ^.
Some Markdown implementation (e.g. MacDown in macOS) interprets the caret as an instruction for superscript.

Ex.
Sin^2 + Cos^2 = 1
Clearly, Stack Overflow does not interpret the caret as a superscript instruction. However, the text is comprehensible, and this is what really matters when using Markdown.

Skiles answered 21/11, 2020 at 20:24 Comment(1)
Well, GitHub doesn't, and I guess that was the whole point of those answers. It would be nice if it supported ^ for superscript, but it's not part of the GitHub specs for GitHub-Flavoured Markdown (GFM).Jorgensen
L
8

If you only need superscript numbers, you can use pure Unicode. It provides all numbers plus several additional characters as superscripts:

x⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ⁿⁱ

However, it might be that the chosen font does not support them, so be sure to check the rendered output.

In fact, there are even quite a few superscript letters, however, their intended use might not be for superscript, and font support might be even worse. Use your own judgement.

Leprous answered 12/1, 2022 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.