How do I print latex from a code block in Google Colab? - jupyter-notebook

In Jupyter, I can write:
d=sqrt(2+3)
show(d)
And it will print the square root of 5 symbolically, using the symbol for square root.
How do I print symbolic LaTeX-formatted output from code blocks in Google Colab?

You could try using calls to the Math function like this.
from IPython.display import display, Math
latex = r'$\alpha=\Omega^4'
Math(latex)

I was using a Jupyter Notebook with SageMath. Which is apparently different to every other type of Jupyter Notebook. It seems the function "show()" belongs to the module "sage", which cannot be imported into Google Colab. Instead of Google Colab, I've had to use cocalc.com which allows me to use a sage kernel.

Related

Render docstring with Sphinx in Jupyter

Is there a way to make Jupyter notebooks render docstrings that contain Sphinx markup correctly in a Jupyter notebooks.
At the moment if I have a Foo class with Sphinx documentation and the user tries to get the docstring with
[1] Foo.bar?
They end up with the raw text. E.g.
Calls the :py:meth:`bar` method of the :py:class:`Foo` class.
Is there a way to make Jupyter automatically render the docstring correctly as Sphinx. Or anything that's easier to read than raw rst?
SageMath does this, I believe using its "sphinxify" code: https://github.com/sagemath/sage/blob/develop/src/sage/misc/sphinxify.py. That is not easy to use out of the box — it has various SageMath specific things in it — but maybe you can adapt it.

Is there a way to write down chemical equations in jupyter notebook by using external packages for Mathjax?

I can't seem to figure out how to write down chemical equations in Jupyter notebook. How do we install mhchem for Mathjax to use inside Jupyter notebook?
Add the following code on top of your jupyter notebook:
$$\require{mhchem}$$
here you can find the detailed explanation on how to use and write chemical equations in jupyter notebook
https://notebooks.azure.com/OUsefulInfo/projects/gettingstarted/html/3.1.1%20Chemical%20Equations.ipynb
and without any extension's you can do as well
from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))
display(Math(r'\ce{H2O}'))
try these in jupyter

ipywidget interactive plot in Html presentations ipython notebook

I would like to use the interactive function from ipywidget in an ipython notebook to make a presentation in notebook.
I have stored my data in a pickle file and what I want is to change the parameters interactively so that I can see my plot.
My code is like this.
def spin(model, power):
with open(path_cluster1+'SSHFS/Model'+str(model)+'/'+str(power)+'/spinpython2.pickle','rb') as f:
spin = pickle.load(f)
plt.plot(spin)
plt.title('Power'+str(power*0.1))
interactive(spin, model=(1,4,1), power=(70,101,1))
My collaborators are unfamiliar with python so in principle I would like to make the life easier for them to see my data just by changing a parameter in a html page. Maybe I have to save all the data in a pickle file but the question is if this can work in a html without running python.
Is something like this possible?
Have a look at https://www.nbinteract.com/. To quote from the website "nbinteract is a Python package that provides a command-line tool to generate interactive web pages from Jupyter notebooks."

Jupyter Notebook save to PDF without Code

This has been an ongoing issue for me, as I would love to use Jupyter Notebook to write my research reports, but have found it very difficult to export my Jupyter Notebooks to PDF without code and without large formatting errors.
I am able to download the notebooks as PDF, but have not found a way to hide the code, or have the PDF resemble the formatting of the notebook.
No solution I've found on SO has been sufficient for my issue, so it may be possible that this is not the intended functionality of Jupyter Notebook.
Have you tried this:
jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True
For more on the flags, you can refer to nbconvert config options
I have successfully converted .ipynb to .html exclude the code blocks
I followed the suggestion by 0xffff above but the PDF output did not behave as intended (code blocks were still included).
However, their suggestion inspired me to try converting to html first using the following call:
jupyter nbconvert path/to/your/ipynb --to=html --TemplateExporter.exclude_input=True
This behaved as intended, and from there, it was straightforward to print the output to PDF in a browser.
Regarding the issue with the --to=pdf flag, I've opened up an issue on the Jupyter Notebook git repo: https://github.com/jupyter/notebook/issues/3804. Will report back once I get a response.
nbconvert
nbconvert is an official package, and other answers have mentioned it. However, they did not mention there are two ways to convert to PDF.
The default option uses LaTeX, and it's often a pain to install:
jupyter nbconvert notebook.ipynb --to=pdf
A newer option is to use the webpdf converter, this does not require LaTeX
jupyter nbconvert notebook.ipynb --to=webpdf
Quarto
Quarto is the newest option. The defaults are prettier than nbconvert, and it has advanced features to customize the output PDF. However, it uses pandoc (and pandoc uses LaTeX), so you must install both first.
quarto render notebook.ipynb --to pdf
Quarto is a new project, and I often run into problems (I couldn't convert the sample notebook); however, the project is in active development and I'm sure it will get better.
Online converters
The last option is to use one of the many online services. The two main caveats are 1) You might not want to upload your notebooks to some random website, and 2) It's often unclear what engine they use to convert the notebooks. I've tried many of them with mostly negative results.
I created one online converter that uses nbconvert and deletes your notebook as soon as it's converted.
If you manually create the PDF, there is a simpler solution: add the following code block & execute it
from IPython.core.display import HTML
HTML('<style>.input, .jp-InputArea {display: none !important}</style>')
Code will disappear both in the "normal" UI and in print preview / export to HTML. Open print preview, generate the PDF - it looks great now, clear the output of this one cell to bring the code back, done

How to use inline code in markdown in Jupyter with R

How to use inline code in markdown in Jupyter with R?
CODE CELL
n <- 8
MARKDOWN CELL
The Number is {{n}}
What would be the correct syntax in Jupyter Markdown for R? Is it even possible?
In case you haven't done it already, you might want to give the python markdown extension a shot, that adds the markdown inline code functionality for Python to Jupyter notebooks. On their github they claim:
The Python Markdown extension allows displaying output produced by the current kernel in markdown cells. The extension is basically agnostic to the kernel language, however most testing has been done using Python.
Installation instructions are on the github page of the nbextensions. Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator.
Calling variables then should work inside a markdown cell with the {{var-name}} syntax that you've already given (described in the readme of the corresponding github page (linked in the wiki)).
If this doesn't help, you might want to join the discussion of the corresponding issues in the issue trackers for ipython and jupyter.

Resources