Skip to content

Instantly share code, notes, and snippets.

@StanczakDominik
Last active December 9, 2017 11:31
Show Gist options
  • Save StanczakDominik/7707de45cbfcd623ebc2202a89d0e878 to your computer and use it in GitHub Desktop.
Save StanczakDominik/7707de45cbfcd623ebc2202a89d0e878 to your computer and use it in GitHub Desktop.
Latex printing in SymPy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAFcAAAAOBAMAAACoZ51gAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAiXaZRCLdEO9Uu81m\nqzIdlvb2AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABbElEQVQoFXWSv0tCURiGHxM7pmmmQ9EvI2kM\nDBoMh+7ekBC0NzREQ3dqaWhJIoK4f0BgEFQEQVBbBNeWGgyDlqLF/gKLogIJ+z6zkshv+s73Pvc9\n7znnwr8VzZd0boZiFsGL8Dh4hgb+JSHomDmVvHCHv1qNYE5IN4EvoVelLVgm2D8MgSQHTeBDyNmi\n9cASIYWmMk1Q+BDVUWSPvi94v85GE9afr8yzcIMyDFXzDqFCokTloUbNWCZr1Kb3VOtYOt8TlBUm\nXrEJ2L4XU3GZl/Us7LRowN/yiXM5Iuu2scVVHe+aKqRsvG+Q9vyC2v3EuKLlXX0mMq9QtPBvQ3eX\nIg0lB8xpsjXZwbmXNNaCwC4rsl1chXrmM23lSoviaB7ljO6GONu3NecOV5wVaKzvR8nKa2SSsEDZ\nJiUvek17PNxISt/qmE1Wjpi0OWea9iyhiFkXobPgeqN/YBMdKdF6TfhGfiTf6I1EivWX+AQuKl3N\ncVOyJAAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$\\alpha = 0.856$$"
],
"text/plain": [
"α = 0.856"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sympy as sp\n",
"sp.init_printing()\n",
"alpha = sp.symbols('alpha')\n",
"eq = sp.Eq(alpha, 0.856)\n",
"eq"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\alpha = 0.856\n"
]
}
],
"source": [
"print(sp.latex(eq))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that you can't just do the following, as it returns two \\ backslashes due to the need to escape one of them:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\\\alpha = 0.856'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sp.latex(eq)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You could, however, output them into a file:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\alpha = 0.856\n"
]
}
],
"source": [
"with open(\"equations.tex\", \"w\") as f:\n",
" f.write(sp.latex(eq))\n",
"with open(\"equations.tex\", \"r\") as f:\n",
" print(f.read()) "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\alpha = 0.856"
]
}
],
"source": [
"cat equations.tex"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I just stumbled upon these options and they're pretty cool:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"$\\alpha = 0.856$\n",
"\\begin{equation}\\alpha = 0.856\\end{equation}\n",
"\\begin{equation*}\\alpha = 0.856\\end{equation*}\n"
]
}
],
"source": [
"print(sp.latex(eq, mode='inline'))\n",
"print(sp.latex(eq, mode='equation'))\n",
"print(sp.latex(eq, mode='equation*'))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"alpha_i = 0.856\n"
]
}
],
"source": [
"print(sp.latex(eq, symbol_names={alpha: \"alpha_i\"}))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:3point6]",
"language": "python",
"name": "conda-env-3point6-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment