How to convert Jupyter/IPython notebooks to LaTex? - jupyter-notebook

How can I export Jupyter notebooks to LaTeX? I can convert to PDF through LaTeX using the inbuilt menus but I'm unsure how to step in at the intermediate step and extract the actual LaTeX file.

You have to do this from the command line rather than the web interface with the following command:
jupyter nbconvert /path/to/mynotebook.ipynb --to latex

Anaconda Navigator provides JupyterLab. Use this to open jupyter notebook.
Under File menu you can find an item "Export Notebook As...".
This provides format such as Asciidoc, HTML, latex, pdf, Markdown, etc.
You can choose your desired format.

Related

How can I handle "/n" when exporting an .ipynb as a LaTeX file?

Whenever I export a python notebook as a LaTeX file any new line characters ("\n") present in the notebook cells show up in the tex file and break it. When compiled LaTeX throws "unidentified control sequence". Is there a way to have this issue fixed automatically so I do not need to alter the .Tex file that comes out manually?

Can nbconvert join the output of multiple files?

I have a bunch of ipython notebooks (.ipynb) that I want to convert to PDF.
I can use the following command to convert all the notebooks in a given directory.
jupyter nbconvert --to pdf *.ipynb
I was then wondering if I could output all the nbconverts to a single PDF, as if it was a book. What is the option to have a single output file? I could not find an answer in the docs.
nbconvert does not have a function to do that which I could find. There are other projects that are designed to do this. The ones I could find are:
bookbook: This is simple and does what you're talking about
jupyter-book: Many more features, allowing combining notebooks, markdown, and a few other things.
nbmerge: Really basic, combines several command line passed files into a single notebook that can then be exported using nbconvert.

With RMarkdown is there a way to use knitr syntax to call external R code from a Jupyter notebook?

I am using RMarkdown to write a journal article. For various reasons I'd prefer to have the R analysis script in a separate Jupyter notebook. Is there a nice way to call R code from MyAnalysis.ipynb in MyArticle.Rmd?
I know I can use knitr syntax to have the .Rmd file read and execute chunks of R code from a .R file like so. And that you can use knitr::purl to call code chunks from one rmarkdown doc in another like so.
But I would like to be able to "purl" the code from the .ipynb file. Is there any way to do this?

Hide all code when exporting notebook in Jupyter Lab to PDF

When I export a notebook to PDF in jupyter lab i cannot hide the code cells in the output.
(Like 'View > Collapse All Code')
Is there a built in functionality to hide code in the output PDF?
For example by using the cell Metadata?
This worked for me:
jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True

How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?

What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96?
My motivation is that I might want to run the same command when I'm in another text editing environment or I might want to combine the command in a larger makefile.
Basic Script
So now that the R markdown package has been released, here is some code to replicate the features of Knit to Html.
require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html
knit('test.rmd', 'test.md') # creates md file
markdownToHTML('test.md', 'test.html') # creates html file
browseURL(paste('file://', file.path(getwd(),'test.html'), sep='')) # open file in browser
where test.rmd is the name of your R markdown file.
Note that I'm not 100% confident about the browseURL line (hence my question here about opening files in a web browser).
markdownToHTML Options
The good thing about markdownToHTML is that there are heaps of options in how the HTML is created (see ?markdownHTMLOptions). So for example, if you want just a code fragment without all the header information, you could write:
markdownToHTML('test.md', 'test.html', options='fragment_only')
or if you don't like hard wrapping (i.e., inserting line breaks when there are single manual line breaks in the markdown source), you can omit the 'hard_wrap' option.
# The default options are 'hard_wrap', 'use_xhtml',
# 'smartypants', and 'base64_images'.
markdownToHTML('test.md', 'test.html',
options=c('use_xhtml', 'base64_images'))
Makefile
This could also all be added to a makefile perhaps using Rscript -e (e.g., something like this). Here's a basic example makefile I put together, where test indicates that the rmd file is called test.rmd.
RMDFILE=test
html :
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''))"
The makefile uses my preferred markdown options: i.e., options=c('use_xhtml', 'base64_images')
Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Basically they are
library(knitr); knit() to get the markdown file;
RStudio has internal functions to convert markdown to HTML;
The second step will be more transparent in the next version of the markdown package. Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you.
Update on 2019/09/17: The above answer applies to RStudio v0.96 (in the year 2012). Now R Markdown is compiled through rmarkdown::render(), which uses Pandoc instead of the retired R package markdown. See the post Relationship between R Markdown, Knitr, Pandoc, and Bookdown for more details.
Very easy command line method from knitr in a knutshell:
R -e "rmarkdown::render('knitr_example.Rmd')"
This requires rmarkdown to be installed with install.packages(rmarkdown) and that pandoc is installed (apparently it comes with Rstudio, see knitr in a knutshell for more details).
So far when I've used this it nicely puts all the plots in the HTML file rather than as images in a figure directory and cleans up any intermediate files, if any; just like compilation in RStudio does.
It seems you should call rmarkdown::render() instead of knitr::knit2html() because a.rmd appears to be an R Markdown v2 document.

Resources