ipython-contrib / jupyter_contrib_nbextensions

A collection of various notebook extensions for Jupyter
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest
Other
5.24k stars 807 forks source link

Python markdown does not work with Latex #514

Open Hugh-G opened 8 years ago

Hugh-G commented 8 years ago

I was hoping to use Jupyter to create verbose output for some repetitive calculations I have to do, but still have them in a nice format for a non-technical reviewer to understand.

To do this I was hoping to be able to insert python variables into latex expressions, however the python markdown extension does not seem to work like this.

Are there any plans to support this in the future? Are there other alternatives that I may not have come across?

juhasch commented 8 years ago

So you want to do something like $ \phi = {{ python_variable }} ^\circ $?. This is not possible, a workaround would be to split the Latex expression into two parts: $ \phi =$ {{ python_variable }} $^\circ $

Hugh-G commented 8 years ago

Your correct that is what I would like to do. I see that your work around is OK if everything is inline, however if I wanted to do $\frac{{{python_variable}}}{2}$ I don't think it would work.

juhasch commented 8 years ago

This could be done. The current implementation does not support this, because it tries to support different display types (i.e. text, HTML, Latex, images).

An extension that replaces the variables inside a Latex equation would need to be implemented differently. This is currently not on my near-term todo list, however.

To clarify: The difficulty is synchronizing the result coming from the Python interpreter with the Latex rendering. Both are asynchronous actions, so currently the rendering is happening separately. In your usecase, the rendering would need to be synchronized somehow.

GProtoZeroW commented 7 years ago

@juhasch I have found that if you utilize sympy's too latex and some string manipulation to get equations into markdown with Python Markdown exsample flows:

cell one 'code': from IPython.display import display, Math, Latex, Image from sympy import * init_printing() cell two 'code': x, m, b=symbols('x, m, b') y=Function('y') lin=latex(Eq(y(x), m*x+b)) lin linLine='$'+lin+'$' linEviorment=r"\begin{equation} \\"+lin+r"\\\end{equation}"

cell three 'markdown': `with a call to the string from the latex conversion: {{lin}}, just text

with a call to the "inline" sting manipulation: {{linLine}}, yep inline

And with a call to standalone one we get {{linEviorment}}`

@juhasch what I am seeing that is causing me issue (and I read what you were saying about the run sequencing with MathJax) is that if I try to pass in any table that already has '&' in the string that is being passed it tries to force the '&' to be 'amp' on the output. whatever part of the code that is doing this it naive seems to me that if the final string output to markdown has certain, let's say latex environments keywords in it then the conversion '&'-> 'amp' should be skipped.

The other issue, and I believe that it also has to do with the sequencing, is if I try to use python markdown inside a latex environment in markdown via Latex Enviorments the cell just returns the str object's memory location

ex markdown: \begin{equation} {{lin}} \end{equation} returns:

If these two nbextensions could get along it would greatly help with generating scientific reports via the Jupyter Notebook

juhasch commented 7 years ago

Hm, not sure I understand completely what is going on with the tables issue.

I tried this

t = """
\\left[
\\begin{array}{cc|c}
1&2&3 \\\\
4&5&6 \\\\
\\end{array} 
\\right]"""

Now in a markdown cell, I can see my table like this:

{{ Math(t) }}

I don't see where the ampersand gets converted there ?

GProtoZeroW commented 7 years ago

@juhasch , using your reply (thanks by the way) I was able to use the "Math" display function with your Python Markdown to display arrays just fine. so now my procedure for embedding numpy arrays in markdown is Numpy-> Sympy->LaTex A=np.zeros([5,5]) SA=sympy.Matrix(A) LSA=sympy.latex(SA) {{Math(LSA}}

GProtoZeroW commented 7 years ago

@juhasch I also tried modifying the above procedure to work with pandas to latex method by replacing the Math with Latex inside Python Markdown; but, gave me a strange result that when processed by nbconvert to latex would add the amp. I will work on getting a repo up this week to show what is going as part of me trying to streamline the notebook as a replacement to standalone TeX file reports

As for the issue of Python Markdown and @jfbercher 's jupyter_latex_envs getting along with that is still an issue

juhasch commented 7 years ago

The display system can get brittle at times. I appreciate your investigation into this. Adding LaTeX export makes it even more difficult.

At some point you may end up generating the outputs yourself rather than trying to juggle with all those pieces that are not made to play together.