pandas dataframes are displayed nicely within the ipython cell. How does it do it?
The regular ways of getting the console width for Python do not seem to work for ipython cells.
If you just want to see what is the size you can use this script:
from IPython.display import display, HTML
js = """<script>
alert($( ".cell").width())
</script>"""
display(HTML(js))
If you want to use in code you can assign it to a variable and use it in next cell:
from IPython.display import display, HTML
js = """<script>
IPython.notebook.kernel.execute("cell_width="+($( ".cell").width()))
</script>"""
display(HTML(js))
In the next cell:
print(cell_width)
I have learned how to display symbolic math in jupyter notebook using the display, Math, & Latex packages.
An example would be: display(Math('some math eqn')), which would result in clearly formatted symbolic math within jupyter notebook.
Now I am interested in exporting the display to an image file so I can use it elsewhere.
I tried using the code below, but it returned a NoneType object:
Image(display(Math('f(x) = x + 2C')), embed=True, format=PNG)
How would I go about doing this please?
Update:
I tried executing display(Math("\dfrac{5x}{3}")) via a cmd prompt and got this: <IPython.core.display.Math object>. I'm wondering if there's a way to convert this object type to an image?
I'm using latex to render text in matplotlib but I'm unable to save the figure if any text has latex environments in it.
Simplified code:
import matplotlib as mpl
mpl.use('PS')
import matplotlib.pyplot as plt
mpl.rcParams['text.usetex'] = True
s = (r"\begin{description}"
r"\item[someitem:] blah blah blah blah blah \\"
r"Some more text that I want to add to my plot"
r"\item[anotheritem:] This is even more text"
r"\end{description}")
plt.text(0,0,s)
plt.savefig('myfig.eps')
I get a generic latex error when I try to save it.
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
However, I don't think there's anything wrong with my latex. I tried compiling a latex document with just the above snippet in it and it worked fine.
Interestingly, the plot gets rendered if I use an interactive backend with ipython/jupyter-notebook but I am still unable to save the figure as ps or eps file. It also works if I remove the \begin{...} ... \end{...}
How do I save the figure as a ps/eps file if I use a latex environment in the text?
You have to be in vertical mode. You can wrap your text in a parbox:
import matplotlib as mpl
mpl.use('PS')
import matplotlib.pyplot as plt
mpl.rcParams['text.usetex'] = True
s = (r"\parbox{4in}{"
r"\begin{description}"
r"\item[someitem:] blah blah blah blah blah \\"
r"Some more text that I want to add to my plot"
r"\item[anotheritem:] This is even more text"
r"\end{description}"
r"}"
)
plt.text(0,0,s)
plt.savefig('myfig.eps')
I am converting a ipython notebook file (mynotebook.ipynb) to a PDF file for model documentation. It contains some LaTex-format mathematical equations, markdown texts, codes and graphs.
For example, the ipython notebook file looks like this:
$$c = \sqrt{a^2 + b^2}$$
TEXT: Companies must take risks if they are to survive and prosper.
import pandas as pd
%load_ext rmagic
%%R
plot(rnorm(1000))
I haven't got enough reputation to post the picture, so I hope you can understand how it looks like.
I have installed the complete versions of MikTeX and Pandoc.
I have tried several ways I found online:
1.
ipython nbconvert mynotebook.ipynb --to latex --post PDF
it gives me errors:
[NbConvertApp] Using existing profile dir: u'C:\\Users\\John Smith\\.ipython\\prof
ile_default'
[NbConvertApp] Converting notebook mynotebook.ipynb to latex
[NbConvertApp] Support files will be in mynotebook_files\
[NbConvertApp] Loaded template latex_article.tplx
[NbConvertApp] Writing 13601 bytes to mynotebook.tex
[NbConvertApp] Building PDF: ['pdflatex', 'mynotebook.tex']
[NbConvertApp] CRITICAL | PDF conversion failed: ['pdflatex', 'mynotebook.tex']
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (MiKTeX 2.9 64-bit)
entering extended mode
("C:\Users\John Smith\Anaconda\mynotebook.tex"
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, afrikaans, ancientgreek, ar
abic, armenian, assamese, basque, bengali, bokmal, bulgarian, catalan, coptic,
croatian, czech, danish, dutch, esperanto, estonian, farsi, finnish, french, ga
lician, german, german-x-2013-05-26, greek, gujarati, hindi, hungarian, iceland
ic, indonesian, interlingua, irish, italian, kannada, kurmanji, latin, latvian,
lithuanian, malayalam, marathi, mongolian, mongolianlmc, monogreek, ngerman, n
german-x-2013-05-26, nynorsk, oriya, panjabi, pinyin, polish, portuguese, roman
ian, russian, sanskrit, serbian, slovak, slovenian, spanish, swedish, swissgerm
an, tamil, telugu, turkish, turkmen, ukenglish, ukrainian, uppersorbian, usengl
ishmax, welsh, loaded.
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.11
?
! Emergency stop.
...
l.11
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on mynotebook.log.
2.
ipython nbconvert mynotebook.ipynb --to latex
pandoc mynotebook.tex -o mynotebook.pdf
Then the PDF file generated shows the markdown and latex formula perfectly; it shows graphs, but it doesn't show the R codes nicely.
3.
ipython nbconvert mynotebook.ipynb --to html
Then the html file shows the code, the markdown texts and the graph very well, but the latex formula is not shown in a good way.
===================================================
I didn't expect this difficulty, because everything looks so fancy in the ipython notebook when I edit them. iPython Notebook uses mathjax to show the formula, and shows other part perfectly too. But for some reason these techniques I have tried cannot convert all subjects into their proper formats.
I am wondering there is a solution with which I can convert all these things (markdown, code, graphs and LaTex Formula) to a PDF file nicely?
While trying to use LaTeX with IPython, I used the following code:
from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))
from How to write LaTeX in IPython Notebook?.
But, I got the following error.
<IPython.core.display.Math at 0x269a030>
What does this error mean? When do I get it? How can I get rid of it?
That is not an error. Run it with ipython notebook and you will see your integral displayed.
It is not an error, it is the output representation. Simply run the code inside the IPython notebook:
from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))