# LaTeX Codec

The `latexcodec.codec` module contains all classes and functions for LaTeX code translation. For practical use, you should only ever need to import the `latexcodec` module, which will automatically register the codec so it can be used by `str.encode()`, `str.decode()`, and any of the functions defined in the `codecs` module such as `codecs.open()` and so on. The other functions and classes are exposed in case someone would want to extend them.

latexcodec.codec.register()

  Register the `find_latex()` codec search function.

  See also

  `codecs.register()`

latexcodec.codec.find_latex(encoding: str) → CodecInfo | None

  Return a `codecs.CodecInfo` instance for the requested LaTeX encoding, which must be equal to `latex`, or to `latex+<encoding>` where `<encoding>` describes another encoding.

class latexcodec.codec.LatexIncrementalEncoder(errors='strict')

  Bases: `LatexIncrementalEncoder`

  Translating incremental encoder for latex. Maintains a state to determine whether control spaces etc. need to be inserted.

  emptytoken = ('unknown', '')

    The empty token.

  get_latex_chars(unicode_: str, final: bool = False) → Iterator[str]

    Encode every character. Override to process the unicode in some other way (for example, for character translation).

  get_space_bytes(bytes_: str) → Tuple[str, str]

    Inserts space bytes in space eating mode.

  reset()

    Reset state.

  table = <latexcodec.codec.LatexUnicodeTable object>

    Translation table.

class latexcodec.codec.LatexIncrementalDecoder(errors='strict')

  Bases: `LatexIncrementalDecoder`

  Translating incremental decoder for LaTeX.

  get_unicode_tokens(chars: str, final: bool = False) → Iterator[str]

    Decode every token. Override to process the tokens in some other way (for example, for token translation).

  getstate() → Any

    Get state.

  regexp: Any = re.compile('(?P<control_symbol_x2>[\\\\][\\\\]|[\\\\]%)|(?P<comment>%[^\\n]*)|(?P<control_word>[\\\\][a-zA-Z]+)|(?P<control_symbol>[\\\\][~\'"` =^!.])|(?P<control_symbol_x>[\\\\][^a-zA-Z])|(?P<parameter>\\#[0-9], re.DOTALL)

    Compiled regular expression.

  reset()

    Reset state.

  setstate(state: Any) → None

    Set state. The state must correspond to the return value of a previous `getstate()` call.

  table = <latexcodec.codec.LatexUnicodeTable object>

    Translation table.

  token_buffer: List[Token]

    The token buffer of this decoder.

class latexcodec.codec.LatexCodec

  Bases: `Codec`

  decode(bytes_: bytes | str, errors='strict') → Tuple[str, int]

    Convert LaTeX bytes to unicode string.

  encode(unicode_: str, errors='strict') → Tuple[bytes | str, int]

    Convert unicode string to LaTeX bytes.

class latexcodec.codec.LatexUnicodeTable(lexer_)

  Tabulates a translation between LaTeX and unicode.

  register(trans: UnicodeLatexTranslation)

    Register a correspondence between unicode_text and latex_text.

    Parameters:

      trans (UnicodeLatexTranslation) – Description of translation.

  register_all()

    Register all symbols and their LaTeX equivalents (called by constructor).