import re


def fix_inline_math(f):
    """
    In the code, I prefer to use inline math like        $\int \bm x \mathrm dx$,
    so we have to convert it to rst inline math    :math:`\int \boldsymbol x \mathrm dx` 

    This is done by using a regex to find groups of $something$ and 
    replace the second one with `.
    Afterwards, we replace the remaining $ with :math:`
    """
    f = re.sub(r"\\bm", r"\\boldsymbol", f)
    f = re.sub(r"(\$[^\$]*)\$", r"\1`", f)
    return re.sub(r"\$", r":math:`", f)