How to use Jupyter to print "latex into code" for math

I am using Jupyter to calculate matrix and I get the results printed out. Now I want to write the resulted matrix into latex. Since it is a large matrix, it would be good and time-saving if Jupyter could print out the “latex input code”, then I could just copy in the Jupyter and paste the code in my latex.
Is there a way of doing such a thing?

It sounds probably doable. However, you’ve got to help us help you.

I suspect you are using Python to calculate the matrix? Probably using some packages?
Jupyter notebook is code agnostic. You could be using a Jupyter notebook backed with a Julia kernel or an R kernel. This is important to understand because it probably will help you down the line as you search for solutions to hurdles you encounter such as this one.
However, there may be some IPython/Jupyter tricks that could help you in this specific case, and so I’m not going to tell you that you should seek help elsewhere just yet. However, you need to provide more to go on if you want some actionable advice. As discussed in the post
Getting good answers to your questions
, could you make a minimal example (a tiny matrix of ,say six values or two rows?) of your issue and share the notebook via Github, gist, or other code snippet sharing means? And honestly, if it’s really minimal you may just be able to just include it directly in reply here as blocks of code. To keep the pasted code snippets in a form they are useable by others trying to help you, see specifically the section ‘Block code formatting’ here for how you can post code blocks here by using 3 backticks (```) on their own line above and below your code. What you share just has to look like your actual data. Doesn’t have to be any of it actually.

And then with the minimal example set up, tell us what you’d expect to get back so you can paste it into your latex.

In case it helps in thinking about this, here are some posts & threads with code examples and resources for latex use in Jupyter:

Those links and the content therein may not be pertinent now to your specific issue but may give you a sense of what’s possible in Jupyter, and how it could relate to getting it to the latex you are wishing to use out of Jupyter.

I have a couple of examples of rendering latex equations here and here.

I hope to add more examples at some point eg relating to the display of matrices or simple markdown tables and displaying outputs from other language kernels (eg R).

1 Like

I just spotted GitHub - benmaier/numpyarray_to_latex: Format numpy arrays as LaTeX arrays which looks handy - demo added to my subject matter notebook examples here. The Python sympy package can also render matrices in a pretty way.

1 Like

Hi fomightez and psychemedia:
Thank you so much for the help. I am new and just come back to the forum, Sorry for the late reply. I will visit the forum more. Yes I am just doing matrix multiplication, and would like the screen to output some latex code

A = np.array([[1,2],[3,4]])
B = np.array([[1,2],[5,6]])
np.matmul(A,B)

, instead of output

array([[11, 14],
       [23, 30]])

I want some output like:

\begin{bmatrix}
11 & 12 \\
23 & 30 
\end{bmatrix}

. In this way, I can just copy the jupyte notebook output to latex. I figure out the solution via one classmate, kinda similar to psychemedia "format NumPy " method. And thanks fomightez for the advice and how to post questions better.

Zhong Chen

If it is solved then, could you mark it ‘Solved’?