Can I use \input{file.tex} or similar to efficiently bring content (not a whole document) from a LaTeX file into a Jupyter notebook? - include

I am new to Jupyter notebooks and Python and have started using these to make materials for a workshop so that I can also produce a handout. I have useful content in various LaTeX files that I would like to include in a notebook.
I would like to know whether there is a command that will allow me to efficiently bring my already modularized content into a notebook. I will be happy to take suggestions on any approach to my problem in case the LaTeX route is the wrong way to proceed.
As a particular case, suppose an external file fig.tex has only a stand-alone tikzpicture (that I have successfully included in another LaTeX document). If I start a notebook code cell with
%%itikz and follow with \input{fig} I obtain error messages.
I can remedy the problem if I add a preamble with \documentclass{}, many necessary \usepackage{} and \usetikzlibrary{} commands (which I have already included at the top of the notebook), and wrap the content with begin/end document commands.
There is more manual handling here than I would like. Is there a more efficient way to include the tikzfigure content?

So it turns out with itikz you have an --implicit-pic option that fills in the preamble for you.
In principle, with this option, your cell would look like the following:
%%itikz --implicit-pic
% my awesome figure
\input{path/to/fig}
This creates a tex file populated like so:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.0]
% my awesome figure
\input{path/to/fig}
\end{tikzpicture}
\end{document}
In addition when using an implicit-pic it is usefull to load tikz packages and set options . To quote from the Quickstart guide:
In an --implicit-pic, it's often useful to:
Set the \tikzpicture[scale=X] via --scale=<X> while iterating.
Set the \usepackage{X,Y,Z} via --tex-packages=<X,Y,Z>
Set the \usetizlibrary{X,Y,Z} via --tiz-libraries=<X,Y,Z>
For more info, see items 16-20 in the Quickstart notebook.

Related

How to add listings or images to the table of contents (TOC)

I have a couple of examples (all with titles) and I'd like to create an index/list out of them automatically.
An example can be seen in the chunked AsciiDoc User Guide table of contents (or beneath):
The asciidoc source of the AsciiDoc User Guide does not show anything specific to me for Asciidoc itself, I could find the following hint to Docbook:
DocBook toolchains will normally automatically number examples and generate a 'List of Examples' backmatter section.
I'm looking for the (asciidoctor?) standard html5 rendering, but I'm open for different suggestions.
Adding the :doctype: book attribute alone does not do it. So I merely hit dead ends not knowing if it is possible at all. Also I'm new to Asciidoc so I might just miss some pointers, too.
The Python Asciidoc repo includes the a2x tool, which is a wrapper around a DocBook toolchain. It is DocBook that is producing these entries in the table of contents. Neither Python Asciidoc, nor asciidoctor, can do this out of the box.
You would need to curate the lists manually, or create a macro that does the curation for you. This thread might prove helpful: https://github.com/asciidoctor/asciidoctor-extensions-lab/issues/111

How to experiment source code with `pdb`, `inspect`, `pprint`?

Problem
I want to understand source code of Kur (deep learning library)
I have no proper training in programming, I prefer to learn by experimenting without prior theoretical knowledge
I want an easy tool to help me dig into the detail workings of source code
debug tools are like pdb library seem to be a good choice to try
But what is the easiest way to get started in experimenting source with pdb?
I just want to write one line of code to dig into details, rather than writing a few lines as demonstrated in many examples when you google pdb
In other words, which function of pdb should I use? and How to use it effectively for experimenting source?
Toy Example
I want to explore the inner workings of kur dump mnist.yml
To make it simple, I want to just explore not beyond __main__.py and kurfile.py.
To be more specific, I want to explore dump() and parse_kurfile() in __main__.py, and Kurfile.__init__() in kurfile.py
Their relationship is as follows:
console: kur dump mnist.yml -->
python: __main__.py : main() --> dump() --> parse_kurfile() -->
python: kurfile.py : Kurfile class --> __init__() ...
python: ... the rest is not to be explored
Which function of pdb should I use to explore the execution flow from dump() to parse_kurfile() and to Kurfile.__init__() and back to dump() again?
Update
How to effectively explore Jupyter notebook using pdb?
pdb inside Jupyter can't even remember console history, not good
One possible solution
use pdb.set_trace only
set_trace is trace the details on the level of the current code block, it will not go deeper to the next inner function.
for example, when I put a single pdb.set_trace inside dump(), pdb will not help me trace into the function of parse_kurfile(), but stay on the current dump() block:
def dump(args):
""" Dumps the Kurfile to stdout as a JSON blob.
"""
pdb.set_trace()
### parse kurfile.yml into parts to be used in python code
spec = parse_kurfile(args.kurfile, args.engine)
If I want to go deeper into parse_kurfile in __main__.py and Kurfile.__init__ in kurfile.py, then I just need to put one pdb.set_trace in each of the two functions, like below:
Update
From my experience so far, there are two libraries inspect and pprint go well with pdb library.
Inside library inspect, I use the following functions the most:
inspect.getdoc: to see the doc of the function
inspect.getmodule: to find out where this function or object come from
inspect.getfullargspec: to find out all the inputs the func takes
inpsect.getsourceliens: to get the source code of the function
with these functions above, when I want to checkout other functions, I don't have to go to find the source code in editor, I can see them right where I am in the pdb.
From library pprint, you can guess, I use pprint.pprint to print out the source code, the doc in a more readable format right inside pdb.
More Update
A working station to explore and experiment source:
using atom to split window and see different source files at the same time;
use iterm2 to split window and use ipython to execute python or bash code
organise them in the following way:
More update
During exploring, I want to have all the attributes and methods of a module or class ready at hand.
To achieve it, I can use inspect.getmembers(module or class name) and use iterm2 split window to view it:
Update: How to change color of iterm2 for the eyes?
Go to iterm2 preferences, color, change to Tango Dark, to gray the foreground color to make white texts look soft
Change Kur logger color setting to:
## logcolor.py
# Color codes for each log-level.
COLORS = {
'DEBUG': BLUE,
'INFO': MAGENTA,
'WARNING': RED,
'ERROR': RED,
'CRITICAL': GREEN
}
How to effectively use pdb in Jupyter notebook?
One way to avoid the drawbacks of pdb in Jupyter:
download the notebook into py file
insert codes like import pdb and pdb.set_trace() into the python code
in console, run python your.py
Now you can explore this py file as you do in above answer

How to force rstudio/knitr/rmarkdown to use alternative pandoc binary (scholdoc)

scholdoc (see scholarlymarkdown.com) is a fork of pandoc that has !FINALLY! easy referencing of figures/code blocks etc. build in - a central missing piece in pandoc.
Is there any straight forward way to force usage of scholdoc instead of the shipped pandoc binary when using knitr/rmarkdown in rstudio?
When I set in .Rprofile
options(
rstudio.markdownToHTML = function(inputFile, outputFile) {
system(
paste(
"~/.cabal/bin/scholdoc",
shQuote(inputFile),
"-o", shQuote(outputFile)))
})
as indicated here, this seems to work, but, as it is missing all manner of command line options used by the internal pandoc, produces HTML out of the box and will lead me down a painful way of getting all the CLI options right.
After studying some rmarkdown code, I have also tried to set the environment variable RSTUDIO_PANDOC to contain the path of scholdoc - to no avail.
Can anyone point out an easy way to do this with up-to-date rstudio/scholdoc installations?
I asked this long ago an thought that for completeness sake, I'd point out, that bookdown has stepped into the arena to provide cross referencing of figures etc. within rmarkdown documents.
after issuing install.packages('bookdown'), RStudio may be coerced to use it by adding the following to the YAML header of a document:
output:
bookdown::pdf_document2:

How to diff 2 notebooks at the source level?

Any one knows a tool to find difference between 2 notebooks at the source level?
The compare notebooks tool in workbench 2 seems to work at the internal data structure level which is not useful for me. I am looking for tool that looks at differences at the source level (what one sees when looking at a notebook, i.e. not the FullForm).
I am using V8 of Mathematica on windows.
EDIT1:
How I display the output/report from NotebookDiff in a more readable form?
This answer is based on discussion in the comments to other parts of this question.
It also could (and should) be automated if it's going to be used with any regularity.
This could be done by tagging the cells you want compared and using NotebookFind to find the cells for extraction and comparison.
A solution for comparing just a single large cell of code (as sometimes occurs when makeing demonstrations) is to copy the code in InputForm from both notebooks
and paste it into a simple diff tool such as Quick Diff Online
which will then display the standard diff for you:
The above code was taken from one of Nasser's demonstrations.
Another option is to use CellDiff from the AuthorTools package.
Needs["AuthorTools`"];
CellDiff[Cell["Some text.", "Text"],
Cell["Some different text.", "Text"]]
To use on your demonstrations you can copy the cell expressions from the two versions by right clicking on the cell brackets:
There is an undocumented package in the built-in add-ons (in $InstallationDirectory/AddOns/Applications) called AuthorTools. Once loaded, it exposes a NotebookDiff function which provides some basic diff features:
Needs["AuthorTools`"];
nb1 = NotebookPut[
Notebook[{Cell["Subsection heading", "Subsection"],
Cell["Some text.", "Text"]}]];
nb2 = NotebookPut[
Notebook[{Cell["Edited Subsection heading", "Subsection"],
Cell["Some different text.", "Text"]}]];
NotebookPut#NotebookDiff[nb1, nb2]
As this package is undocumented, please realize it is potentially buggy and is not considered a supported feature, but hopefully you still find it useful.
Note that you can also get handles to notebooks with e.g.:
nb1 = NotebookOpen["path/to/a/notebook.nb"]
and a list of notebooks currently open in the front end
Notebooks[]
If you must work with notebooks then NotebookDiff in AuthorTools is probably your best bet. If this is an important part of your workflow (due to version control or some other constraint) and you have some flexibility you may want to consider moving the code from the existing notebook (.nb) into a package file (.m), which will be saved as plain text. You can still open and edit package files in the Mathematica notebook front end, but you get the added benefit of being able to diff them using existing text diffing tools.

Generate line graph for any benchmark?

I had spent so many hours failing to find a line graph generator for my benchmark results that I just wanted to plug in. I tried quite a few like Google's chart API but it still seemed confusing or not graceful looking, I am clueless.
Examples of benchmark images I wished to make something like are this:
What specific applications /web services do you recommend for generating something even close to this? I want something "neat".
You can use python mathplotlib, which generates beautiful graphs like:
(Source code)
I use gnuplot. It is not a lib, but a separate executable file. You can output plotting data to one file, and plotting commands in another - script file, which refer to data file. Then call gnuplot with this script file.
Another way is to use qwt. It is a real library, but it depends on Qt. If you already use Qt in your project, it is very straigth way to plot graphs. If not, then just use gnuplot

Resources