2

I have a function with a docstring as follows,

def func(x):

    '''Boring function

    Notes
    -----
    Let :math:`\bar{x}_k = \sum_{i=1}^{n_k}x_{ik}`,
    '''

    return x

But the \bar command in Latex renders as "ar",

enter image description here

NewNameStat
  • 2,474
  • 1
  • 19
  • 26

2 Answers2

4

Python is interpreting the backslashes. You need to pass the backslashes through so LaTeX can interpret them; you should use a raw string to do so:

def func(x):

    # Note the r!
    r'''Boring function

    Notes
    -----
    Let :math:`\bar{x}_k = \sum_{i=1}^{n_k}x_{ik}`,
    '''
Community
  • 1
  • 1
user2357112
  • 260,549
  • 28
  • 431
  • 505
2

Alternative answer for future visitors: use two backslashes instead of one. It's useful for formatting formulas as we saw above, but also for uses like:

print("/!\\ Error encountered.")
Guimoute
  • 4,407
  • 3
  • 12
  • 28