'Encode special characters in `text` & return.
Escaping with a backslash does not help with backslashes, ~ and ^.
< > are only available in math-mode or tt font. (really ?)
$ starts math- mode.
AND quotes:'

def encode(self, text):

    if self.verbatim:
        return text
    if (not self.__dict__.has_key('encode_re_braces')):
        self.encode_re_braces = self.re.compile('([{}])')
    text = self.encode_re_braces.sub('{\\\\\\1}', text)
    if (not self.__dict__.has_key('encode_re_bslash')):
        self.encode_re_bslash = self.re.compile('(?<!{)(\\\\)(?![{}]})')
    text = self.encode_re_bslash.sub('{\\\\textbackslash}', text)
    text = text.replace('$', '{\\$}')
    if (not (self.literal_block or self.literal or self.mathmode)):
        text = text.replace('|', '{\\textbar}')
        text = text.replace('<', '{\\textless}')
        text = text.replace('>', '{\\textgreater}')
    text = text.replace('&', '{\\&}')
    text = text.replace('^', '{\\textasciicircum}')
    text = text.replace('%', '{\\%}')
    text = text.replace('#', '{\\#}')
    text = text.replace('~', '{\\textasciitilde}')
    separate_chars = '-'
    if (self.literal_block or self.literal):
        separate_chars += ',`\'"<>'
        text = self.babel.double_quotes_in_tt(text)
        if (self.font_encoding == 'OT1'):
            text = text.replace('_', '{\\underline{    }}')
            text = text.replace('\\textbackslash', '\\reflectbox{/}')
        else:
            text = text.replace('_', '{\\_}')
    else:
        text = self.babel.quote_quotes(text)
        text = text.replace('_', '{\\_}')
    for char in (separate_chars * 2):
        text = text.replace((char + char), ((char + '{}') + char))
    if (self.insert_newline or self.literal_block):
        text = text.replace('\n', '~\\\\\n')
    elif self.mbox_newline:
        if self.literal_block:
            closings = ('}' * len(self.literal_block_stack))
            openings = ''.join(self.literal_block_stack)
        else:
            closings = ''
            openings = ''
        text = text.replace('\n', ('%s}\\\\\n\\mbox{%s' % (closings, openings)))
    text = text.replace('[', '{[}')
    if self.insert_none_breaking_blanks:
        text = text.replace('    ', '~')
    if (self.latex_encoding != 'utf8'):
        text = self.unicode_to_latex(text)
    return text
