Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to show `print` output as LaTeX in jupyter notebook?

I was writing a very simple script to count ellipsoid area and volume and some other things. I was presenting my output printing it out like this:

print('Dims: {}x{}m\nArea: {}m^2\nVolume: {}m^3'.format(a, round(b,2), P, V)) 

What, of course, gave this output (with sample data):

Dims: 13.49x2.25m Area: 302.99m^2 Volume: 90.92m^3 

As I wrote earlier, I am using jupyter notebook, so I can use $ operators in markdown cells to create LaTeX formulas.

My question is, is it possible to generate output using Python code in a way that it will be understood as LaTeX formula and printed in such a way, that:

Latex_example

Thanks for all replies.

like image 963
Sqoshu Avatar asked Jan 24 '18 12:01

Sqoshu


People also ask

How do I display LaTeX in Jupyter?

One of the most popular is Jupyter Notebook that uses MathJax to render the Latex syntax inside the markdown/HTML. To use LaTeX in the Jupyter notebook, put the Latex math content inside the '$ … $' double '$$ … $$' symbols.

Can you put LaTeX in Jupyter Notebook?

The Jupyter Notebook uses MathJax to render LaTeX inside HTML / Markdown. Just put your LaTeX math inside $ $ . Or enter in display math mode by writing between $$ $$ . The [n] is optional.

How do I show output in Jupyter?

Adjust the view of Output:Jupyter Notebook can print the output of each cell just below the cell. When you have a lot of output you can reduce the amount of space it takes up by clicking on the left side panel of the output. This will turn the output into a scrolling window.


2 Answers

Use IPython.display's display function with a Math object:

from IPython.display import display, Math display(Math(r'Dims: {}x{}m \\ Area: {}m^2 \\ Volume: {}m^3'.format(a, round(b,2), P, V))) 

Note the use of Latex-style \\ newlines, and the r'' string, which will take the backslashes as literal backslashes and not see them as escape characters.

Found the solution here.

like image 167
sebas Avatar answered Oct 07 '22 04:10

sebas


Here's another solution that let's you include text and math a little easier: Use Markdown with r (so backslashed don't become escape chars) and f string for value insertion.

from IPython.display import display, Markdown  a = 13.49 b = 2.2544223 P = 302.99 V = 90.02  display(Markdown(    rf""" Dims: ${a}m \times{b:5.2}m$  Area: ${P}m^2$  Volume: ${V}m^3$ """)) 
like image 35
JMann Avatar answered Oct 07 '22 04:10

JMann