How can I include cells from another notebook? - jupyter-notebook

I am looking for a way to include one notebook in another, similar to LaTeX's \include directive.
My goal is to split up one large notebook, that mixes Markdown and code cells, into smaller chunks for easier maintainability.
What I've tried
I tried using %run "other.ipynb" command, but that only shows output of code cells and does not include the Markdown cells. So if I have:
In [1]: %run "other.ipynb"
In [1]: # This is a markdown cell
In [2]: Print("Simple string")
Out[2]: Simple string
The output when running main.ipynb is only Simple string, while I expected it to show the Markdown cell as well.

Related

How to capture a cell's output for use in another cell?

In org-mode, I can name the output of a code block and include it elsewhere in the document.
Is it possible to do this (or something similar) in a colab .ipynb file, or within a Jupyter notebook in general?
For example, I can make a cell with some Python code that generates a plot:
import matplotlib.pyplot as plt
plt.plot([0,2,1,4,9])
After executing, the plot appears below the code cell. So far so good.
But how do I capture this plot and to use it elsewhere in the notebook?
My hope is there is some syntax so that I can include the plot in a markdown cell, for example:
# this is my title
As you can see, the numbers go up and down in the plot:
![][some_magic_link_here]
Here is some more text to explain the plot.
Does such a feature exist in colab?
Good news - embedding an image in another markdown cell is self-service. Here's an example:
Full notebook:
https://colab.research.google.com/drive/1PF-hT-m8eZN2CBzFkMp9Bvzj9pSBQVYn
The key bits are:
Using IPython.display.Markdown is order to programmatically construct the markdown.
In addition to rendering, save the matplotlib figure using savefig.
Include the image data in the markdown after base64 encoding.

Less space between letters and units in rmarkdown (pdf and docx output)

I cannot find any information about spacing between letters in R Markdown. All I found only were questions about vertical spacing. I basically have two issues:
In LaTeX I usually use the siunitx package to correctly typeset numbers and units. I can of course use this in R Markdown as well if I load the package with \usepackage{siunitx}. But this does not output to the docx format, only to pdf. That brings me to my other question...
So I tried using different spaces of LaTeX to display numbers and their units with at least less space, e.g. writing 40\,m^2 to display 40 m² (in LaTeX I would use \SI{40}{\square\meter}). However, apparently R Markdown does not handle the \, nor \; at all, not even in the pdf output.
Question: What is the correct way to add smaller spaces between letters in R Markdown? (irrespective of output format!) How do I replace the \, command?
And: Is there a way to handle units nicely using R Markdown? I have found this question on the R units package, and I could live with it. That is, if I want to write hardcoded numbers like 40 m² I would have to use something like`r format(set_units(40, m2))`,right?.
Have you tried adding thin spaces using Unicode chars? For example, this page http://jkorpela.fi/chars/spaces.html suggests that "\u2009" and "\u200A" should display as thin spaces.
When I try this with a PDF document (using latex_engine: xelatex to handle Unicode), this is what I see:
It also appears to work with HTML and Word output.
Edited to add:
To be clear, this needs to go through R. If you want it inline, use code like this:
This is standard spacing: 40 m²,
this is narrow `r knitr::asis_output("40\u2009m²")` spacing.
This produces this output in PDF:

Using Output In markdown Jupyter notebook

I am using Jupyter Notebook to write my report and it would be convenient to include an output in my markdown.
The question below is a simillar question.
Jupyter notebook output in markdown
For example, I have a code cell with the code
In[1]: import random
a = random.randint(1,4);a
and the output was
Out[1]: 2
in my report, I would realy like to include this output just like
'the chosen number was 2'
however, as the 2 is a random number, it would be very convenient to have a way to include the variation a in my markdown like;
'the chosen number was %a'
kind of way.
Is there any way to achieve this?
It's not possible in the main notebook yet (although there are discussions about it) but there is an extension which should suit your purposes:
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/python-markdown/readme.html
It's contained within the ipython-contrib-extensions package, for which the install instructions are here:
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html

Aligning XTable Output in R Markdown

I am putting an assignment together in R Markdown to PDF (through Latex) and been using xtable to nicely format my output. The below code generates a small table that I would like to align right in the document and have text wrap around it. Is there a way to achieve this?
summary.table <- xtable(ddply(aircon, ~when, summarise, Mean=mean(hours), StdDev=sd(hours)), caption="Mean and Standard Deviation (in Hours) Before and After Change")
print(summary.table, include.rownames=FALSE)
Given the update regarding using LaTeX to do the wrapping, how could I write this in R Markdown to take advantage of this? I have tried to print inline using r <code> but that didn't work.
I guess you could add the begin/end wraptable commands in the r-chunk that creates the table, like this:
cat("\\begin{wraptable}{r}{5.5cm} \n")
...
print(summary.table,...)
cat("\\end{wraptable} \n")
You can use the Includes mechanism documented here, to include a custom header.tex which would contain usepackages, etc.

How to insert a number from R to LaTeX?

I´m trying to insert a number directly from R into a LaTeX file. I have only been able to do it as a table and that doesn´t allow me to put it in the middle of a sentence.
I would like the output to look like this:
"The final number is [number directly from R]"
What should I do?
If you use the Sweave or knitr (assuming .Rnw file), use \Sexpr{foo}. Then run the file through R + Sweave or R + knitr and you'll have a LaTeX source with the value of foo inserted in place of \Sexpr{foo}.
Basically you need to markup your LaTeX source appropriately, run it through a system using R to identify the things you want to replace and insert the actual data into the source file, and output a LaTeX source which you can then compile.
There are other systems besides Sweave and Knitr so find a system you like that suits your workflows. For example, there is brew.

Resources