diff --git a/docs/source/examples/Notebook/Typesetting Equations.ipynb b/docs/source/examples/Notebook/Typesetting Equations.ipynb index e9ee56918..057bf1556 100644 --- a/docs/source/examples/Notebook/Typesetting Equations.ipynb +++ b/docs/source/examples/Notebook/Typesetting Equations.ipynb @@ -7,6 +7,111 @@ "The Markdown parser included in the Jupyter Notebook is MathJax-aware. This means that you can freely mix in mathematical expressions using the [MathJax subset of Tex and LaTeX](http://docs.mathjax.org/en/latest/tex.html#tex-support). [Some examples from the MathJax site](http://www.mathjax.org/demos/tex-samples/) are reproduced below, as well as the Markdown+TeX source." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "\"\"\"Attribution to Willie Wong https://williewong.wordpress.com/2012/07/27/ipython-notebook-take-2/\"\"\"\n", + "\n", + "from IPython.core.display import *\n", + " \n", + "def add(x,y): return x+y\n", + "def MDPL(string): display(Math(string))\n", + "def comp_str(listofstrings): return reduce(add,listofstrings)\n", + " \n", + "class math_expr(object):\n", + " '''''Math Expression object'''''\n", + " \n", + " def __init__(self,atomslist, labeldefault = True, highlight = []):\n", + " '''''init takes arg: list of atoms, each atom being a compilable chunck of LaTeX expression'''''\n", + " self.listofatoms = atomslist\n", + " self.labels = labeldefault\n", + " MDPL(comp_str(self.__colouratoms(highlight)))\n", + " \n", + " def _repr_latex_(self):\n", + " '''''Returns a latex expression of the object. Will be parsed by MathJax'''''\n", + " self.__labelatoms()\n", + " latexstring = comp_str(self.labeledatoms) if self.labels else comp_str(self.listofatoms)\n", + " return \"$\" + latexstring + \"$\"\n", + " \n", + " @property\n", + " def labeled(self):\n", + " '''''Tells you whether labelling is turned on by default'''''\n", + " return self.labels\n", + " \n", + " @labeled.setter\n", + " def label(self,boolean):\n", + " '''''Sets the default labelling'''''\n", + " self.labels = boolean\n", + " \n", + " @property\n", + " def latex(self):\n", + " '''''Accesses the LaTeX code snip for the expression'''''\n", + " display(Latex(comp_str(self.listofatoms)))\n", + " \n", + " def __labelatoms(self):\n", + " '''''Label atoms by adding underbraces'''''\n", + " self.labeledatoms = [ r\"\\underbrace {\" + self.listofatoms[i] + \"}_{\" + str(i) + \"}\" for i in range(len(self.listofatoms)) ]\n", + " \n", + " def replace(self,pos,newstr):\n", + " '''''Replaces an atom with another atom'''''\n", + " MDPL(comp_str(self.__colouratoms([pos])))\n", + " newstrings = list(self.listofatoms)\n", + " newstrings[pos] = newstr\n", + " return math_expr(newstrings,self.labels,[pos])\n", + " \n", + " def merge(self,positions):\n", + " '''''Merges atoms: the input is a list of positions. The new atom is placed at the position of the foremost of the positions'''''\n", + " MDPL(comp_str(self.__colouratoms(positions)))\n", + " newstrings = list(self.listofatoms)\n", + " temp = [ newstrings[i] for i in positions ]\n", + " positions.sort()\n", + " positions.reverse()\n", + " for i in positions: del newstrings[i]\n", + " newstrings.insert(positions[-1],comp_str(temp))\n", + " return math_expr(newstrings,self.labels,[positions[-1]])\n", + " \n", + " def split(self,pos,newatoms):\n", + " '''''Splits atoms: replaces an atom in place with multiple sub atoms'''''\n", + " MDPL(comp_str(self.__colouratoms([pos])))\n", + " newstrings = list(self.listofatoms)\n", + " del newstrings[pos]\n", + " templen = len(newatoms)\n", + " while len(newatoms) > 0:\n", + " newstrings.insert(pos,newatoms.pop())\n", + " return math_expr(newstrings,self.labels,range(pos,pos+templen))\n", + " \n", + " def cancel(self,positions):\n", + " '''''Cancels a bunch of terms: input a list of positions'''''\n", + " MDPL(comp_str(self.__colouratoms(positions)))\n", + " positions.sort()\n", + " positions.reverse()\n", + " newstrings = list(self.listofatoms)\n", + " for i in positions: del newstrings[i]\n", + " return math_expr(newstrings,self.labels)\n", + " \n", + " def move(self,posini,posfin):\n", + " '''''Move atom at posini to posfin, pushing all others back'''''\n", + " MDPL(comp_str(self.__colouratoms([posini])))\n", + " newstrings = list(self.listofatoms)\n", + " temp = newstrings.pop(posini)\n", + " newstrings.insert(posfin if posfin < posini else posfin-1, temp)\n", + " return math_expr(newstrings,self.labels,[posfin if posfin < posini else posfin - 1])\n", + " \n", + " def __colouratoms(self,positions,labelled=False):\n", + " '''''Returns the list of atoms, but with selected terms coloured'''''\n", + " temp = list(self.listofatoms)\n", + " if labelled:\n", + " self.labelatoms()\n", + " temp = list(self.labeledatoms)\n", + " for i in positions: temp[i] = \"\\color{red}{\"+temp[i]+\"}\"\n", + " return temp\n" + ] + }, { "cell_type": "markdown", "metadata": {},