0

If I have a variable and want to use the string formatting, I would use:

print 'r_0 =%02f ' %(variable1)

which works fine. However I would like to use "math mode" inside print. i.e.

print r'$r_{0}=%02f \pm %02f$' %(variable1,variable2) 

The above doesn't seem to work. It just prints the values of variable1 and variable2 correctly, but it doesn't print r_0 and \pm as it should. i.e. it prints

$r_{0}=18.966 \pm 0.424698$

My question is, how to make the subscript (r_{0}) and \pm work!

user3397243
  • 567
  • 2
  • 10
  • 20
  • As far as I understand it seems that you believe that `print 'r_0 =%02f ' %(variable1)` will assign value `variable1` to variable `r_0` ? – mvelay Mar 07 '16 at 15:20
  • @massiou: No, the values are assigned correctly. That is not my question. My question is, how to make the subscripts and \pm work! – user3397243 Mar 07 '16 at 15:22
  • 1
    What do you mean by `make the subscript (r_{0}) and \pm work!` ?? – mvelay Mar 07 '16 at 15:26
  • @massiou: actually, in math mode ($$), `\pm` should give me `plus or minus` and `r_{0}` should give me an `r with 0 as the subscript` – user3397243 Mar 07 '16 at 15:27
  • @user3397243, that isn't a feature of Python string formatting. Can you describe in what context those features do exist? – Robᵩ Mar 07 '16 at 15:32
  • Possible duplicate of [Writing variables as subscripts in math mode](http://stackoverflow.com/questions/23276918/writing-variables-as-subscripts-in-math-mode) – nthall Mar 07 '16 at 15:33
  • I assume you mean ipython-notebook – as python itself has no "math mode" or any way to render MathJax/LaTeX-markup – fredrikhl Mar 07 '16 at 15:36
  • @Robᵩ: So how do I write subscripts and other LaTeX environments in `print`? – user3397243 Mar 07 '16 at 15:41

2 Answers2

0

From what I know, you seem to be confusing string formatting with LaTeX. You cannot use inline LaTeX with Python.

Rohit Kharsan
  • 145
  • 1
  • 8
0

If you're trying to render a LaTeX-formatted string in a code-cell in ipython-notebook, you'll have to do something like this:

from IPython.display import display, Math
display(Math(r'$r_{0}=%02f \pm %02f$' % (1, 2)))
fredrikhl
  • 159
  • 4