Coder Social home page Coder Social logo

array_to_latex's Introduction

Convert NumPy/SciPy arrays and Pandas Dataframes to formatted LaTeX arrays

image

image

PyPi Download stats

image

The module array_to_latex converts a NumPy/SciPy array or Pandas Numerical DataFrame to a LaTeX array or table using Python 3.x style formatting of the result or a format usable for plotting within LaTeX using pgfplots. Note that as of 0.83 it does add the column formatting arguments (for example, {ccc...}) when the user chooses to use array. I'm happy to have someone create a better solution but this at least makes a copied array immediately usable. I prefer bmatrix or similar. `

Play with it on mybinder.org!

A NumPy-focused re-envisioned converter based, in part, on this is numpyarray_to_latex. Also, available pip install. It incorporates more sophisticated "sub" markup capabilities. Check it out!

Recent updates follow, with a more complete list towards the end of this document. If you don't see the current version in this list it's likely because I (again) forgot to update it when pushing out a new version. Please see the readme on GitHub.

0.81: Bug fixes in requirements and to_clp
0.82: Raise ImportError exception when incorrect datatype used.
0.83: Now puts a default format in when returning as an array object.
0.90: Add coords format for use in plotting within LaTeX using pgfplots.
0.91: Improve the output for scientific e notation.

Install using pip install --user array_to_latex from your command prompt, not the Python prompt.

Please read the help. It explains all options. To try it, see the online mybinder.org demo. It documents illustrates application to numerical Pandas DataFrames.

import numpy as np
import array_to_latex as a2l
A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')

will print the LaTeX code to your output.

import numpy as np
import array_to_latex as a2l
A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
latex_code = a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'bmatrix', print_out=False)

will put the LaTeX code into variable latex_code.

import numpy as np
import array_to_latex as a2l
A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
a2l.to_clp(A, frmt = '{:6.2f}', arraytype = 'bmatrix')

will put the array onto your clipboard.

If you will be using the same conversion over and over, you can define your own by using a lambda function:

to_tex = lambda A : a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'bmatrix', mathform=True)
to_tex(A)

so you can now use your function to_tex repeatedly with your specified settings. More detailed information on usage is in the help.

import array_to_latex as a2l
help(a2l.to_ltx)

Interesting alternative approaches are np_array_to_latex and tab2latex (convert numpy array to longtable file).

Like this module, buy me a coffee!.

New in 0.37: Now handles complex arrays.
New in 0.38: Aligns columns neatly.
0.40: Critical upgrade- 0.37-0.38 formatted incorrectly.
0.41: Critical upgrade- 0.37-0.40 formatted incorrectly.
New in 0.43: Now handles 1-D Arrays. See new option row
New in 0.50: Now works with Pandas DataFrames
0.51: Bug fix- remove extra blank lines in DataFrame tabular output
0.52: A few documentation typos fixed. No code changed.
0.60: Now handles strings in Pandas Dataframes. Fixes bug in exponentials and handling of exponentials. Please report errors!
0.61: Minor documentation improvements. No code changed.
0.70: Added mathform. When set to True (default), returns 10 to superscript form.
0.71: Line breaks broke readme.rst on pypi. No code change.
0.72: Line breaks broke readme.rst on pypi. No code change.
0.73: pypi won't handle mathjax. It makes me sad. No code change.
0.74: Not released
0.75: output improvements (short-lived release)
0.76: Printing made better, allows outputs, added print_out
        boolean to turn off printing
0.80: Return to previous interface while still enabling returned
        LaTeX string.
0.81: Bug fixes in requirements and to_clp
0.82: Raise ImportError exception when incorrect datatype used.
0.83: Add formatting defaults when using array versus bmatrix, etc.
0.90: Add coords format for use in plotting within LaTeX using pgfplots.
0.91: Improve the output for scientific e notation.

array_to_latex's People

Contributors

benweishi avatar bfield1 avatar dbatten5 avatar dima74 avatar josephcslater avatar mkaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

array_to_latex's Issues

Missing \\

Hello,
Thank's a lot for your work.

When I try this code:

 import numpy as np
import array_to_latex as a2l

A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'pmatrix')

I obtain this 👍
image

The last backslashes are missing, and the latex code is wrong.

I use Python 3.8.6

Can you help me?

Best regards,

Do not see columns specifications

Let's consider sample from doc:

>>> import numpy as np
>>> import array_to_latex as a2l
>>> A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
>>> latex_code = a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')
\begin{array}
    1.23 &   23.46\\
  456.23 &    8.24
\end{array}

I do not see here any column specifications,
( https://latex-programming.fandom.com/wiki/Array_(LaTeX_environment) )
for me it works only when we specify columns

\begin{array}{|r|r|}
    1.23 &   23.46\\
  456.23 &    8.24
\end{array}

and dont work without it.

Am I doing something wrong?

Use 10^{###} instead of e### as an option

I'd like to make the output in more human readable form.

Steps are to write a function that is called after each numerical string is created, then parse out the parts if complex, then parse out the exponents. Good luck. I don't have i figured out yet either.

If you do this, please form the develop branch and work on it there.

Unexpected behavior when latex is returned to variable

Writing latex code into a variable doesn't work as specified in the readme because the keyword argument is print_out=True per default

behavior

In [118]: import numpy as np
     ...: import array_to_latex as a2l
     ...: A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
     ...: latex_code = a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')
     ...:
\begin{array}
    1.23 &   23.46\\
  456.23 &    8.24
\end{array}

In [119]: latex_code is None
Out[119]: True

expected behavior

In [118]: import numpy as np
     ...: import array_to_latex as a2l
     ...: A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
     ...: latex_code = a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')
     ...:

In [119]: latex_code 
Out[119]: r"\begin{array}
    1.23 &   23.46\\
  456.23 &    8.24
\end{array}"

Unhelpful exception when give wrong data type to to_ltx

In to_ltx, 'a' is required to be a numpy array or pandas dataframe. If 'a' is not an instance of either of those classes, then the function raises an UnboundLocalError, because it passes over the if statements where a variable would be assigned.

It would be more helpful if this situation could be detected (such as by using if-else statements) and a TypeError raised with a meaningful error message.

Improve the output of the scientific "e" notation

Currently if the mathform=False argument is added to the to_ltx method, then the output has the form -1.2e-01 which renders as

$$-1.2e-01$$

From reading some latex q&a online it seems like the \mathrm{e}{-01} form is preferred:

$$-1.2\mathrm{e}{-01}$$

Would it be possible to update this package with the above? I'm happy to implement it myself and raise a PR if that helps

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.