question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert ndarray to Tex matrix string (Feature Suggestion)

See original GitHub issue

As a linear algebra student I find myself checking my answers and computing busy work with NumPy; I could imagine that thousands of others who work with linear algebra and write up their work in papers/articles/problem sets could also face this. One consistent source of pain has been manually typing out arrays I obtain within NumPy into my LaTeX write-ups. To automatically convert 2-dimensional NumPy ndarrays to a string that LaTeX would accept I wrote the following function

def np2tex(np_arr, latex_type='bmatrix'):
    """
    Convert a NumPy array to a LaTeX matrix string.
    """

    if len(np_arr.shape) != 2:
        raise ValueError("incorrect shape")

    inner_str = ' \\\\ '.join([' & '.join(str(row)[1:-1].split()) for row in np_arr])
    latex_out = '\\begin{' + latex_type + '} ' + inner_str + ' \\end{' + latex_type + '}'

    return latex_out

Is this something that could be added in some way? Please advise as to whether a PR could be possible (this would be very useful for me, as I wouldn’t have to edit my own NumPy source code or add a custom module to my own system). Maybe it could be rolled into array2string in some way with a parameter?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Feb 24, 2020

I think IPython let’s you define _repr_latex_ for types you don’t own - you might want to look into doing that.

0reactions
rodrigo-castelloncommented, Feb 24, 2020

Cool, thanks for being helpful!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert numpy array into tabular - TeX - LaTeX Stack Exchange
I am doing some data analysis in python, putting the results in form of a matrix stored into a numpy array ...
Read more >
Numpy 2d and 1d array to latex bmatrix - Stack Overflow
Another option is to use sympy: first convert the array to a sympy.Matrix and then use the sympy.
Read more >
josephcslater/array_to_latex - GitHub
Convert NumPy /SciPy arrays and Pandas Dataframes to formatted LaTeX arrays ... The module array_to_latex converts a NumPy/SciPy array or Pandas Numerical ...
Read more >
How to turn Text into Features - Towards Data Science
The techniques used to turn Text into features can be referred to as “Text ... And then encode a vector with the word...
Read more >
How to save a NumPy array to a text file? - GeeksforGeeks
Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found