-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.tex
110 lines (88 loc) · 3.58 KB
/
example.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
% Copyright (C) 2009-2014 by Tim Molteno <[email protected]>
%
% This file may be redistributed and/or
% modified under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version. To view a copy of this
% license, see http://www.gnu.org/licenses/ or send a letter to
% the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
% Boston, MA 02110-1301, USA.
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{sympytex}
\title{SympyTeX example}
\author{Tim Molteno\thanks{[email protected]}}
\begin{document}
\maketitle
\section{Important! Setting up Your \LaTeX environment}
To use SympyTeX, you have to run latex (or pdflatex) on your file, then python, and then latex again.
\begin{verbatim}
latex example.tex
python example.sympy
latex example.tex
\end{verbatim}
If you use Kile, or another \LaTeX environment, you can set it up to automatically do the
sympy python command as part of your build process.
\section{Introduction}
Using sympy within your \LaTeX document is as easy as $2+2=\sympy{2+2}$.
You can write a block, and then use the variables defined later in your code!
\begin{sympyblock}
x = sympy.Symbol('x')
h = sympy.integrate(1+x,x)
\end{sympyblock}
The variable $h$, how can be called using {\verb \sympy{h} }, and you will get $h = \sympy{h}$.
Similarly, the integral of $1+x^4$ is $\sympy{sympy.integrate(1+x**4,x)}$.
\section{sympyplain}
The sympyplain command places the sympy output as text (rather than LaTeX formatted math stuff).
This is really useful if the output is huge as formulae don't line wrap.
The integral of $1+x^4$ is also \sympyplain{sympy.integrate(1+x**4,x)}.
\begin{sympyblock}
from sympy import *
e = 1/cos(x)
\end{sympyblock}
The series expansion is $\sympy{e.series(x, 0, 10)}$.
\subsection{Invisibly sympy code}
Placing a {\tt sympysilent} block in your code, allows you do do things like define
symbols without making a mess.
\begin{sympysilent}
theta = sympy.Symbol('theta')
from sympy import *
def RotationMatrixZ(angle):
return Matrix([[cos(angle), -sin(angle), 0],[sin(angle), cos(angle),0],[0,0,1]])
\end{sympysilent}
The rotation matrix is through an angle $\theta$ around the $z$-axis is
\[ R(\theta) = \sympy{RotationMatrixZ(theta)}. \]
\section{Plotting}
Plotting works using the {\tt sympyplot} command. This takes a matplotlib figure as a parameter. Have a look at the example below.
\begin{sympyblock}
import matplotlib
import matplotlib.pyplot as plt
plt.matplotlib.rc('text', usetex = True)
import pylab
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
t = pylab.linspace(0,10,400)
ax.plot(t, np.sin(t*3), '-',
t, np.sin(0.3*t**2), '--',
t, np.cos(t), '-.')
ax.legend((r'$A^{\omega}$', r'$A^{2\omega}$', r'$A^{3\omega}$'),
shadow = False, loc = (0.75, 0.1))
ax.set_xlabel(r'$\gamma_1 + \gamma_2$', {'fontsize' : 20 })
ax.set_ylabel(r'$A^{n\omega}$ (dB)', {'fontsize' : 20 })
\end{sympyblock}
Here we're using matplotlib! So this sympytex package is really not quite named properly.
\begin{figure}
\sympyplot[width=\linewidth]{fig}
\caption{The graph generated from {\tt \textbackslash sympyplot[width=\textbackslash linewidth]\{fig\}} }
\end{figure}
The syntax is
{\verb! \sympyplot[<includegraphics opts>][fmt]{figure} !}
where:
\begin{itemize}
\item {\verb!fmt!} can be one of pdf,eps,png,jpg or many more. If it is omitted, then an appropriate choice will be made for you.
\item {\verb!<figure>!} is a matplotlib figure object
\end{itemize}
\end{document}