def ensure_latex(string):
    """Inserts $'s at the beginning and end of the string, if necessary."""
    if string.strip()[0] != '$':
        string = '$' + string
    if string.strip()[-1] != '$':
        string = string + '$'
    return string
