Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - 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
Area: {}m^2
Volume: {}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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...