How to do superscripts and subscripts in Jupyter Notebook?
Asked Answered
H

5

53

I want to to use numbers to indicate references in footnotes, so I was wondering inside of Jupyter Notebook how can I use superscripts and subscripts?

Herbalist answered 2/9, 2017 at 8:8 Comment(1)
Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here?Chuch
C
65

You can do this inside of a markdown cell. A markdown cell can be created by selecting a cell then pressing the esc key followed by the M key. You can tell when you have converted a cell to markdown when In [ ]: seen to the right of the default code cell is gone. Then you can input the following code that uses latex with markdown to represent sub/super-scripts:

Latex subscript:

$x_{2}$

Latex superscript:

$x^{2}$

You can find more detailed examples here.

Please comment below if you are still having difficulty.

Chuch answered 2/9, 2017 at 15:5 Comment(2)
Note that the text must be left-justified with a blank line separating it from surrounding text. This drove me mad not working because I had indented my text by four spaces, accidentally telling markdown it was pre-formatted code.Youthen
Doesn't work in headings unfortunately :(. At least not in Jupyter.Rumor
P
38

<sup>superscript text </sup> also works, and might be better because latex formatting changes the whole line etc.

Pierrette answered 5/1, 2018 at 19:57 Comment(2)
This is much better answer. Btw the subscript tag is <sub>.Glimp
Doesn't work in headings unfortunately :(. At least not in Jupyter.Rumor
T
6

What a meta question! One needs to use Markdown to answer Markdown syntax questions. Markdown does have the <sup></sup> and <sub></sub> tags that will adjust text to super- or sub- script, respectively in the typeface of the current block. If you are using the scripts for mathematical statements like this the LaTeX transformation makes sense. If you are using the scripts for footnotes or perhaps for something like chemical formulas (e.g. H2O) it might be preferable to use the first method rather than LaTeX. Mixing fonts is generally not considered a good graphics/typography practice!

Tipperary answered 9/5, 2018 at 15:55 Comment(0)
N
5

Let's say, you want to write this:
enter image description here

Then, here is what you need to write in your Markdown:

$\hat{Y} = a + b_1 X_1 + b_2 X_2 + b_3 X_1 X_2 + b_4 X_1^2 + b_5 X_2^2$

Another example:

$\hat{Y} = \hat{\beta}_{0} + \sum \limits _{j=1} ^{p} X_{j}\hat{\beta}_{j}$

enter image description here

Nolita answered 30/10, 2022 at 0:57 Comment(3)
This is the most useful onelinerPerigee
This answer should really be marked down as it doesn't work in Jupyter. Did you test it in Jupyter notebook or Jupyter Lab. It didn't work for me.Parishioner
You would need to have a $ on both sides. I updated it.Nolita
M
0

You can use HTML ' superscript '

http://wikimarkup.wikia.com/wiki/How_to_superscript_text

You can also use Latex $foo^{superscript}$, but the font used to render the text will change and you might not like how it looks.

Meyers answered 6/12, 2017 at 15:29 Comment(1)
While this link may assist in your answer to the question, you can improve this answer by taking vital parts of the link and putting it into your answer, this makes sure your answer is still an answer if the link gets changed or removed :)Dignitary

© 2022 - 2024 — McMap. All rights reserved.