Loading AMSmath explicitly in IPython notebook causes "[Math Processing Error]" - latex

I am a relatively new IPython Notebook user and I am using IPython 3.2.1. So upfront, my apologies if I did not read some documentation carefully!
I am trying to typeset some LaTeX in my Notebook intended for a presentation. I tried to utilize MathJax capabilities better than what's (supposedly) available by default and so tried incorporating other extensions that come with MathJax explicitly. In particular, I tried getting my IPython Notebook to load AMScd.js, AMSsymbols.js and unicode.js through the following sequence of steps:
Obtain the .js files for these extensions from the latest MathJax (v2.5) source
Add them to my ~/.ipython/nbextensions
Modify custom.js in ~/.ipython/profile_<mine>/static/custom by appending IPython.load_extensions("AMScd"), etc.
Now, all these work fine, and I am able to typeset commutative diagrams nicely in my IPython notebook. So, while at all this, I decided to also include/load AMSmath.js that comes along with MathJax and this is problematic. (To me, this seems like a natural thing to do given that other extensions work well!) However, attempting to include AMSmath.js via IPython.load_extensions("AMSmath") causes LaTeX math in my Notebook to display [Math Processing Error] everywhere. Here's my minimal example and the output with and without including IPython.load_extensions("AMSmath").
Markup:
## A minimal working example
\\[
\Delta u = f \text{ on } \Omega
\\]
(I would totally like to include images here, alas, my not being an active participant in the community here leaves me with not even 10 reputation!)
So, what am I missing here? Why is this inclusion of AMSmath problematic? Any help would be appreciated! Thanks!

It should work without extra configuration. Have a look here.
Your example in a markdown cell should be
(If you want the equation inline you can use $...$)
Inline equation $\Delta u = f \text{ on } \Omega$
(Or if you want the equation centered in their own line you can use $$...$$)
$$\Delta u = f \text{ on } \Omega$$
The result of both snippets in the same markdown cell is rendered as:
How it is loaded?
IPython/Jupyter notebook preloads some MathJax extensions including the extension you want to load. This extension is not an IPYthon/Jupyter extension so it shouldn't work if you try to load this MathJax extension as an IPython/Jupyter extension. See here to know more about how to load other MathJax extensions.

Related

latex \vec accent misplaced in jupyter markdown / mathjax

I'm a long-time latex user just learning to use jupyter notebooks. I've noticed that whenever I try to create latex-formatted equations in a markdown cell, the vector accents (created with "\vec{...}") are badly misplaced -- they show up way to the right of the character they are supposed to be accenting.
This seems like it must be a known/solved issue (probably an issue with MathJax, which I guess is what jupyter is using to process the latex?) but I couldn't find an answer anywhere. Indeed, searching around for an explanation/fix, I found a number of pages that explain how to create latex-formatted equations in mathjax/markdown that show (without identifying that it is a problem!) this misplacement, e.g.:
https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference (see the "\vec{x}" under accents near the bottom of the page)
http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Typesetting%20Equations.html (see Maxwell's equations)
http://data-blog.udacity.com/posts/2016/10/latex-primer/ (see again the "\vec{x}" under accents)
Why are the vector accents mis-placed like this, and how can this be fixed?
Any help appreciated!

How can I use Latex in Markdown?

I want to write mathematical formulas in my jupyter notebook and it seems that latex is a simpler method to make them in markdown. I went through several links, but all of them provided me with different information. Please post any detailed links or cheat sheets which can help achieve the above.
An example of what I want to write:
Jupyter notebooks support LaTeX in Markdown cells out of the box (via MathJax, a LaTeX equation renderer built in JavaScript). Simply delimit inline LaTeX expressions with $ and block expressions with $$, e.g.
Let $\theta^A_i$ and $\theta^B_i$ denote

How to handle citations in Ipython Notebook?

What is the best way to take care of citations in Ipython Notebook? Ideally, I would like to have a bibtex file, and then, as in latex, have a list of shorthands in Ipython markdown cells, with the full references at the end of the notebook.
The relevant material I found is this: http://nbviewer.ipython.org/github/ipython/nbconvert-examples/blob/master/citations/Tutorial.ipynb
But I couldn't follow the documentation very well. Can anyone explain it? Thanks so much!!
Summary
This solution is largely based on Sylvain Deville's excellent blog post. It allows you to simply write [#citation_key] in markdown cells. The references will be formatted after document conversion. The only requirements are LaTeX and pandoc, which are both widely supported. While there is never a guarantee, this approach should therefore still work in many years time.
Step-by-Step Guide
In addition to a working installation of jupyter you need:
LaTeX (installation guide).
Pandoc (installation guide).
A citation style language. Download a citation style, e.g., APA. Save the .csl file (e.g., apa.csl) into the same folder as your jupyter notebook (or specify the path to the .csl file later).
A .bib file with your references. I am using a sample bib file list.bib. Save to the same folder as your jupyter notebook (or specify the path to the .bib file later).
Once you completed these steps, the rest is easy:
Use markdown syntax for references in markdown cells in your jupyter notebook. E.g., [#Sh:1] where the syntax works like this: ([#citationkey_in_bib_file]). I much prefer this syntax over other solutions because it is so fast to type [#something].
At the end of your ipython notebook, create a code cell with the following syntax to automatically convert your document (note that this is R code, use an equivalent command to system() for python):
#automatic document conversion to markdown and then to word
#first convert the ipython notebook paper.ipynb to markdown
system("jupyter nbconvert --to markdown paper.ipynb")
#next convert markdown to ms word
conversion <- paste0("pandoc -s paper.md -t docx -o paper.docx",
" --filter pandoc-citeproc",
" --bibliography="listb.bib",
" --csl="apa.csl")
system(conversion)
Run this cell (or simply run all cells). Note that the 2nd system call is simply pandoc -s paper.md -t docx -o paper.docx --filter pandoc-citeproc --bibliography=listb.bib --csl=apa.csl. I merely used paste0() to be able to spread this over multiple lines and make it nicer to read.
The output is a word document. If you prefer another document, check out this guide for alternative syntax.
#Extras
If you do not like that your converted document includes the syntax for the document conversion, insert a markdown cell above and below the code cell with the syntax for the conversion. In the cell above, enter <!-- and in the cell below enter -->. This is a regular HTML command for a comment, so the syntax will in between these two cells will be evaluated but not printed.
You can also include a yaml header in your first cell. E.g.,
---
title: This is a great title.
author: Author Name
abstract: This is a great abstract
---
You can use the Document Tools of the Calico suite, which can be installed separately with:
sudo ipython install-nbextension https://bitbucket.org/ipre/calico/downloads/calico-document-tools-1.0.zip
Read the tutorial and watch the YouTube video for more details.
Warning: only the cited references are processed. Therefore, if you fail to cite an article, it won't appear in the References section. As a little working example, copy the following in a Markdown cell and press the "book" icon.
<!--bibtex
#Article{PER-GRA:2007,
Author = {P\'erez, Fernando and Granger, Brian E.},
Title = {{IP}ython: a System for Interactive Scientific Computing},
Journal = {Computing in Science and Engineering},
Volume = {9},
Number = {3},
Pages = {21--29},
month = may,
year = 2007,
url = "http://ipython.org",
ISSN = "1521-9615",
doi = {10.1109/MCSE.2007.53},
publisher = {IEEE Computer Society},
}
#article{Papa2007,
author = {Papa, David A. and Markov, Igor L.},
journal = {Approximation algorithms and metaheuristics},
pages = {1--38},
title = {{Hypergraph partitioning and clustering}},
url = {http://www.podload.org/pubs/book/part\_survey.pdf},
year = {2007}
}
-->
Examples of citations: [CITE](#cite-PER-GRA:2007) or [CITE](#cite-Papa2007).
This should result in the following added Markdown cell:
References
^ PĂ©rez, Fernando and Granger, Brian E.. 2007. IPython: a System for Interactive Scientific Computing. URL
^ Papa, David A. and Markov, Igor L.. 2007. Hypergraph partitioning and clustering. URL
I was able to run it with the following approach:
Insert the html citation as in the tutorial you mentioned.
Create ipython.bib in the "standard" bibtex format. It goes into the same file as your *.ipynb notebook file.
Create the template file as in the tutorial, also in the same directory or else in the (distribution dependent) directory with the other templates. On my system, that's /usr/local/lib/python2.7/dist-packages/IPython/nbconvert/templates/latex.
The tutorial has the template extend latex_article.tplx. On my distribution, it's article.tplx (without latex_).
Run nbconvert with --to latex; that generates an .aux file among other things. Latex will complain about missing references.
Run bibtex yournotebook.aux; this generates yournotebook.bbl. You only need to re-run this if you change references.
Re-run nbconvert either with --to latex or with --to pdf. This generates a .tex file, or else runs all the way to a .pdf.
If you want html output, you can use pandoc to assemble the references into a tidy citation page. This may require some hand-editing to make an html page you can reference from your main document.
If you know that you will be converting your notebook to latex anyway, consider simply adding a "Raw" cell (Ctrl+M R) to the end of the document, containing the bibliography just as you would put it in pure LaTeX.
For example, when I need to reference a couple of external links, I would not even care to do a proper BibTeX thing and simply have a "Raw" cell at the end of the notebook like that:
\begin{thebibliography}{1}
\bibitem{post1}
Holography in Simple Terms. K.Tretyakov (blog post), 2015.\\
\url{http://fouryears.eu/2015/07/24/holography-in-simple-terms/}
\bibtem{book1}
The Importance of Citations. J. Smith. 2010.
\end{thebibliography}
The items can be cited in other Markdown cells using the usual <cite data-cite="post1">(KT, 2015)</cite>
Of course, you can also use proper BibTeX as well. Just add the corresponding Raw cell, e.g:
\bibliographystyle{unsrt}
\bibliography{papers}
This way you do not have to bother editing a separate template file (at the price of cluttering the notebook's HTML export with raw Latex, though).
You should have a look at the latex_envs extension in https://github.com/ipython-contrib/IPython-notebook-extensions (install from this repo, it is the most recent version). This extension contains a way to integrate bibliography using bibtex files and standard latex notation, and generates a bibliography section at the end of the notebook. Style of citations can be (to some extent) customized. Some documentation here https://rawgit.com/jfbercher/latex_envs/master/doc/latex_env_doc.html

Physics bra-ket symbols in IPython

I am trying to have the ket symbol which is usually written in latex as \ket{\psi}. However, this doesn't work when written within the $$ ... $$. This also doesn't work when written using IPython.display.
$ latex
** \documentclass{article}\usepackage{amsmath}\begin{document}
... chatter chatter ...
* \show\ket
> \ket=undefined.
So your \ket macro is coming from some package or other which I don't know what it is. This doesn't explain why it doesn't work in $$ ... $$ -- you have to work at it to get things to not work in display math -- but it does explain why it doesn't work in IPython: whichever package this is isn't being loaded in IPython's canned TeX environment (or) IPython's emulation of TeX math input language does not include this macro.
Here's a reasonable definition in terms of primitive math symbols, which should work fine in display mode. I don't know if you can put this into IPython verbatim, but you should at least be able to write out the expansion longhand when needed (e.g. $$ \left| \psi \right\rangle $$)
\newcommand{\ket}[1]{\left|{#1}\right\rangle}
\newcommand{\bra}[1]{\left\langle{#1}\right|}
EDIT: define macros using LaTeX preferred interface, rather than primitive \def.
I was able to extend #zwol 's answer to create a \braket command as well for use in a Jupyter notebook:
$$\newcommand{\braket}[2]{\left\langle{#1}\middle|{#2}\right\rangle}$$
$$\braket{\Psi^*}{\Psi}$$
$$\braket{\frac{\Psi^*}{2}}{\Psi}$$
Gives:
IPython uses MathJax to render LaTeX in the notebook. MathJax is great but only supports a subset of LaTeX hence there are some limitations. See MathJax homepage for details.
A new command like proposed by #Zack will work fine though. Be aware that such custom commands will likely lead to some issues when converting to LaTeX (nbconvert).

Latex, TikZ and separate compilation of chapters and figures

I have fairly large Latex document with a lot of TikZ figures inside. I have a habit of frequent recompilation and it takes forever to compile it using pdflatex. Figures in TikZ take most of the time.
My question is what is the best way to split the document into separate tex files (figures/chapters) to achieve separate compilation of figures and chapters, separate chapter pdfs, and a whole document pdf file ?
Have you tried compiling each picture on its own and then including them in your tex file as pdf rather than the tikz code? You can use the package standalone so that the picture will be the exact size you need. So :
\documentclass{standalone}
\usepackage{tikz,pgf} %and any other packages or tikzlibraries your picture needs
\begin{document}
\begin{tikzpicture}
%your tikz code here
\end{tikzpicture}
\end{document}
The good thing about this is that you can either include the compile this document directly to get a pdf figure to include in your document, or you can use the command \input to include it in your main document as a tikz code by adding
\usepackage{standalone}
in your main document (together with the tikz packages and libraries), and then
\begin{figure}
\input{tikzfile.tex}
\end{figure}
There is a possibly better way (imho) to cache tikz-pictures. Add the following lines in your
preamble:
\usetikzlibrary{external}
\tikzexternalize[prefix=i/]
After a pdflatex-run you'll see all pictures in the subdirectory ./i .
If you update the code of a tikz-picture simply throw away its corresponding pdf-file and it will be regenerated. For more info see the manual of PFG/TikZ section 32.4 Externalizing Graphics
and possibly 32.5 Using External Graphics Without pgf Installed.
How about putting each chapter in a separate file and then using \include to put them into some master file? Then you can use \includeonly to only compile the chapter you're currently working on. That should save some time at least.
I expect some sort of makefile based solution would be even better than this, but I don't know anything about makefiles...
The way I generally do this is to apply Latex to just part of the file: Emacs and several other Latex editors allow you to compiler regions: with Auctex, you can run TeX-pin-region to specify the current chapter, and then TeX-command-region to run Latex on the selected region.
The traditional way to do this is cut parts of the big file into smaller parts that are \included, and then either comment out parts you don't want to work on, or put some macrology at the beginning and end of each file that allows them to be compiled separately.

Resources