Encode Unicode to LaTeX

The latexencode module provides a single function, utf8tolatex() which allows you to convert a unicode string to LaTeX escape sequences.

pylatexenc.latexencode.utf8tolatex(s, non_ascii_only=False, brackets=True, substitute_bad_chars=False, fail_bad_chars=False)

Encode a UTF-8 string to a LaTeX snippet.

If non_ascii_only is set to True, then usual (ascii) characters such as #, {, } etc. will not be escaped. If set to False (the default), they are escaped to their respective LaTeX escape sequences.

If brackets is set to True (the default), then LaTeX macros are enclosed in brackets. For example, santé is replaced by sant{\'e} if brackets=True and by sant\'e if brackets=False.

Warning

Using brackets=False might give you an invalid LaTeX string, so avoid it! (for instance, maître will be replaced incorrectly by ma\^\itre resulting in an unknown macro \itre).

If substitute_bad_chars=True, then any non-ascii character for which no LaTeX escape sequence is known is replaced by a question mark in boldface. Otherwise (by default), the character is left as it is.

If fail_bad_chars=True, then a ValueError is raised if we cannot find a character substitution for any non-ascii character.

Changed in version 1.3: Added fail_bad_chars switch