def texsafe(text):
    """Escape text such that it is tex-safe."""
    conv = {'&': '\\&', '%': '\\%', '$': '\\$', '#': '\\#', '_': '\\_', '{':
        '\\{', '}': '\\}', '~': '\\textasciitilde{}', '^': '\\^{}', '\\':
        '\\textbackslash{}', '<': '\\textless', '>': '\\textgreater'}
    escaped = []
    for char in text:
        if char in conv:
            char = conv[char]
        escaped.append(char)
    return ''.join(escaped)
