r"""Escape characters of given text.

    This function takes the given text and escapes characters
    that have a special meaning in LaTeX: # $ % ^ & _ { } ~ \

def escape_latex(text):
    r"""Escape characters of given text.

    This function takes the given text and escapes characters
    that have a special meaning in LaTeX: # $ % ^ & _ { } ~ \
    """
    text = unicode(text.decode('utf-8'))
    CHARS = {
        '&': r'\&',
        '%': r'\%',
        '$': r'\$',
        '#': r'\#',
        '_': r'\_',
        '{': r'\{',
        '}': r'\}',
        '~': r'\~{}',
        '^': r'\^{}',
        '\\': r'\textbackslash{}',
    }
    escaped = "".join([CHARS.get(char, char) for char in text])
    return escaped.encode('utf-8')