Skip to main content

A lexer and codec to work with LaTeX code in Python.

Project description

The codec provides a convenient way of going between text written in LaTeX and unicode. Since it is not a LaTeX compiler, it is more appropriate for short chunks of text, such as a paragraph or the values of a BibTeX entry, and it is not appropriate for a full LaTeX document. In particular, its behavior on the LaTeX commands that do not simply select characters is intended to allow the unicode representation to be understandable by a human reader, but is not canonical and may require hand tuning to produce the desired effect.

The encoder does a best effort to replace unicode characters outside of the range used as LaTeX input (ascii by default) with a LaTeX command that selects the character. More technically, the unicode code point is replaced by a LaTeX command that selects a glyph that reasonably represents the code point. Unicode characters with special uses in LaTeX are replaced by their LaTeX equivalents. For example,

original text

encoded LaTeX

¥

\yen

ü

\"u

\N{NO-BREAK SPACE}

~

~

\textasciitilde

%

\%

#

\#

\textbf{x}

\textbf{x}

The decoder does a best effort to replace LaTeX commands that select characters with the unicode for the character they are selecting. For example,

original LaTeX

decoded unicode

\yen

¥

\"u

ü

~

\N{NO-BREAK SPACE}

\textasciitilde

~

\%

%

\#

#

\textbf{x}

\textbf {x}

#

#

In addition, comments are dropped (including the final newline that marks the end of a comment), paragraphs are canonicalized into double newlines, and other newlines are left as is. Spacing after LaTeX commands is also canonicalized.

For example,

hi % bye
there\par world
\textbf     {awesome}

is decoded as

hi there

world
\textbf {awesome}

When decoding, LaTeX commands not directly selecting characters (for example, macros and formatting commands) are passed through unchanged. The same happens for LaTeX commands that select characters but are not yet recognized by the codec. Either case can result in a hybrid unicode string in which some characters are understood as literally the character and others as parts of unexpanded commands. Consequently, at times, backslashes will be left intact for denoting the start of a potentially unrecognized control sequence.

Given the numerous and changing packages providing such LaTeX commands, the codec will never be complete, and new translations of unrecognized unicode or unrecognized LaTeX symbols are always welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

latexcodec-2.0.0.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

latexcodec-2.0.0-py2.py3-none-any.whl (17.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file latexcodec-2.0.0.tar.gz.

File metadata

  • Download URL: latexcodec-2.0.0.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.19.4 CPython/2.7.17

File hashes

Hashes for latexcodec-2.0.0.tar.gz
Algorithm Hash digest
SHA256 cd3f649d489169ed593f9466ef6ba485e694f6871d9696601e78307b5b84df5f
MD5 bd79c0f5cd016048ff68c985f8ccd33b
BLAKE2b-256 bccfead5e88a8cf2e66c673b1871ee78012d8b2ea9ce5ac85e441ef66790c418

See more details on using hashes here.

File details

Details for the file latexcodec-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: latexcodec-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.19.4 CPython/2.7.17

File hashes

Hashes for latexcodec-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 05d9ba4b2340bc9e71487719ca31c48db3b6685524322590448b8b96bcc98af6
MD5 977db8a2160af717bcb1a9fdd12eab8d
BLAKE2b-256 8e4f6e7353dce0cfde419995117705035c4a0433a08aa7c49219ee589767766a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page