Skip to main content

A package to convert python objects into latex strings

Project description

latexify

Install with pip install latexifier

Use with from latexifier import latexify

This module aims to turn python objects into latex strings. For instance:

  • Integers: latexify(20) returns "20"
  • Python Fractions: latexify(Fraction(1,3)) returns "\frac{1}{3}"
  • Approximate Fractions: latexify(0.25) returns "\frac{1}{4}"
  • Approximate combination of radicals: latexify(0.5 - 3 * numpy.sqrt(5), numbertype='algebraic') returns "\frac{1}{2} - 3\sqrt{5}"
  • Complex numbers: latexify(2 + 3.5j) returns "2+\frac{7}{2}i"
  • numpy.arrays: latexify(numpy.eye(2)) returns "\begin{pmatrix} 1&0\\0&1 \end{pmatrix}"
  • tuples: latexify((1,2,3)) returns "\left(1,2,3\right)"
  • lists: latexify([1,2,3]) returns "1,2,3"
  • sympy polynomials: latexify(x**2 - y*z/3) returns "x^2 - \frac{yz}{3}"
  • All those objects can be combined and nested: list of arrays, arrays of polynomials, etc.

When given integer-based values (such as Integers or Fractions), latexify returns a string without any ambiguity. When given a float, latexify tries to approximate this float by means of fractions and radicals. To do so, the search is limited to "small" fractions and "small" radicals. What "small" means can be specified with some parameters (see below).

Options

The behavior of latexify can be changed on-the-fly by passing arguments to the function, like latexify(0.25, parameter_name=parameter_value), or can be set once-for-all with the function latexifier.parameters(parameter_name=parameter_value). Here are is a list of such parameters:

parameter_name default value purpose
style_fraction 'frac' Sets how fraction should be displayed. Given 0.5, the option 'frac' returns \frac{1}{2}. The option 'dfrac' returns \dfrac{1}{2}. The option 'inline' returns 1/2.
newline False Some Latex expressions usually contain newlines (for arrays for instance). By default latexify returns a string with no such newlines, but you can turn it on.
arraytype 'pmatrix' np.arrays can be converted in many latex flavours, of the form \begin{arraytype} ... \end{arraytype}
column True if the input is a 1D array, how should it be represented? 'True' means a column vector, 'False' means a row vector
mathmode 'raw' latexify returns by default the latex expression of a number or a matrix, etc. If you want to display it correctly in a document, this string must be placed in a math environment. You can optionally ask latexify to do it for you. With the option 'inline', the expression is returned in between $ $. With the option 'equation' the expression is returned inside a equation* environment. With the option 'display' the expression is returned in between \[ ... \].
denominator_max 10 Maximal denominator allowed to appear in fractions (in absolute value)
numbertype 'rational' Controls which kind of number you expect to have. By default latexify looks only for formal expression of rationals. With the option 'root' it will look for a rational multiplied with the square root of some integer (see root_max below). With the option 'algebraic' it will look for a rational linear combination of square roots. Be careful though, these two options are more time consuming.
root_max 7 The expression can contain sqrt(n) to show up, with n no greater than root_max
tol 1e-12 Any formal expression must satisfy \vert x_{formal} - x_{float} \vert < tol. If not, the float is rounded.
frmt '{:3.6f}' When no formal expression can be found, the floats will be rounded. This parameters specifies which rounding/formating rules must be applied (this is a pythonic syntax).
verbose False When calling latexify we can ask it to print the result. Can be 'True' (the string is printed), 'False' (nothing is printed), 'markdown' (in notebooks this interprets the latex contents of the string and renders the expression like in markdown)
value False if 'True', latexify returns a second variable which is the approximation of the input

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

latexifier-1.0.8.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

latexifier-1.0.8-py3.7.egg (28.3 kB view details)

Uploaded Source

File details

Details for the file latexifier-1.0.8.tar.gz.

File metadata

  • Download URL: latexifier-1.0.8.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.7.1 requests/2.25.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

File hashes

Hashes for latexifier-1.0.8.tar.gz
Algorithm Hash digest
SHA256 b70ef226d0beac9f658389b3714d4d3811a2069bb37fc773cdda6a15727201d2
MD5 fb74af8e7ffdb193774874c5a32ebb31
BLAKE2b-256 fada80630498c8172cbdcda4fc2cc1db851589f8fe474df39ceb489993ba0b6a

See more details on using hashes here.

File details

Details for the file latexifier-1.0.8-py3.7.egg.

File metadata

  • Download URL: latexifier-1.0.8-py3.7.egg
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.7.1 requests/2.25.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

File hashes

Hashes for latexifier-1.0.8-py3.7.egg
Algorithm Hash digest
SHA256 769228c850f8ba2b5f4b5468952ea526feb34a9ea17f5156fad403da258ead0c
MD5 9111b606101bb47d79e08c5ed518f665
BLAKE2b-256 4b64071bf14df19a38c133c5119fedfa07486da0d9f0f8f049b4faba62c5208f

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