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",
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",
 
    
    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}`,
    '''
 
    
     
    
    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.")
