Search code examples
pythonlatexdocstring

Latex command \bar not working properly in Python docstring


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


Solution

  • 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}`,
        '''