Skip to content

Instantly share code, notes, and snippets.

@BekeJ
Created November 8, 2012 17:55
Show Gist options
  • Save BekeJ/4040388 to your computer and use it in GitHub Desktop.
Save BekeJ/4040388 to your computer and use it in GitHub Desktop.
py2tex example
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Example_py2tex"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Py2tex extension example notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The py2tex extension can be found at <a href=\"https://gist.github.com/4032651\">https://gist.github.com/4032651</a>. The extension defines new magic functions to convert easy python formulas to a nice LaTeX representation. <br>\n",
"The extension supports the python <a href=\"https://bitbucket.org/kiv/unum\">unum class</a> to provide unit aware calculation. The combination of ipython notebook with unit aware calculation make it perfect for engineering calculation worksheets."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext py2tex"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from math import *"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Normal usage"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%tex a=1.0\n",
"b = -1.0\n",
"c = -1.0\n",
"x_1=(-b+sqrt(b**2-4*a*c))/(2*a)\n",
"x_2=(-b-sqrt(b**2-4*a*c))/(2*a)\n",
"varphi = x_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\mbox{a} = 1.0$$"
],
"output_type": "display_data",
"text": [
"a=1.0"
]
},
{
"latex": [
"$$\\mbox{b} = -1.0$$"
],
"output_type": "display_data",
"text": [
"b = -1.0"
]
},
{
"latex": [
"$$\\mbox{c} = -1.0$$"
],
"output_type": "display_data",
"text": [
"c = -1.0"
]
},
{
"latex": [
"$$\\mbox{x}_{1} = \\frac{- b + \\sqrt{b^{2} - 4 \\cdot a \\cdot c}}{2 \\cdot a} = 1.618$$"
],
"output_type": "display_data",
"text": [
"x_1=(-b+sqrt(b**2-4*a*c))/(2*a) = 1.618"
]
},
{
"latex": [
"$$\\mbox{x}_{2} = \\frac{- b - \\sqrt{b^{2} - 4 \\cdot a \\cdot c}}{2 \\cdot a} = -0.618$$"
],
"output_type": "display_data",
"text": [
"x_2=(-b-sqrt(b**2-4*a*c))/(2*a) = -0.618"
]
},
{
"latex": [
"$$\\varphi = x_{1} = 1.618$$"
],
"output_type": "display_data",
"text": [
"varphi = x_1 = 1.618"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%texnr x_1_2=x_1+x_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\mbox{x}_{1,2} = x_{1} + x_{2}$$"
],
"output_type": "display_data",
"text": [
"x_1_2=x_1+x_2"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%texformat %.3e"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%tex alpha = pi/3\n",
"cos(alpha)**2+sin(alpha)**2\n",
"sin(alpha)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\alpha = \\frac{\\pi}{3} = 1.047$$"
],
"output_type": "display_data",
"text": [
"alpha = pi/3 = 1.047"
]
},
{
"latex": [
"$$\\cos\\left(\\alpha\\right)^{2} + \\sin\\left(\\alpha\\right)^{2} = 1.000$$"
],
"output_type": "display_data",
"text": [
"cos(alpha)**2+sin(alpha)**2 = 1.000"
]
},
{
"latex": [
"$$\\sin\\left(\\alpha\\right) = 8.660 \\cdot 10^{-1} $$"
],
"output_type": "display_data",
"text": [
"sin(alpha) = 8.660 \\cdot 10^{-1} "
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Unit example"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from unum.units import *"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%texformat %.3f"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%texnr t = 3.0*s\n",
"a = 4.5*(m/s**2)\n",
"v_0 = 2*(m/s)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\mbox{t} = 3.0 \\cdot s$$"
],
"output_type": "display_data",
"text": [
"t = 3.0*s"
]
},
{
"latex": [
"$$\\mbox{a} = 4.5 \\cdot \\frac{m}{s^{2}}$$"
],
"output_type": "display_data",
"text": [
"a = 4.5*(m/s**2)"
]
},
{
"latex": [
"$$\\mbox{v}_{0} = 2 \\cdot \\frac{m}{s}$$"
],
"output_type": "display_data",
"text": [
"v_0 = 2*(m/s)"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%tex x = v_0*t+(a*t**2)/2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\mbox{x} = v_{0} \\cdot t + \\frac{a \\cdot t^{2}}{2} = 26.250\\quad \\mbox{m}$$"
],
"output_type": "display_data",
"text": [
"x = v_0*t+(a*t**2)/2 = 26.250\\quad \\mbox{m}"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment