summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/hybrid-latex/examples/example-08.tex
blob: 86043fcc60de768a8cb23306bd2526fb848858cb (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
% based on
% https://github.com/sympy/sympy/wiki/Quick-examples

\documentclass[12pt]{pylatex}
\usepackage{examples}

\begin{document}

\section*{Visible Python code}

This is the first of two-part example. In this first part the Python code and tags are made visible using {\tt\small\verb|\PySetup{action=show}|} (this is the default). In the second part the Python code will be hidden using {\tt\small\verb|\PySetup{action=hide}|}. In both cases the Python output is identical (apart from the different tag names).

\vspace{12pt}

\PySetup{action=show}

\begin{python}
   from sympy import *

   x, y = symbols('x y')

   ans = limit((sin(x)-x)/x**3, x, 0)               # py(ans.101,ans)
   ans = (1/cos(x)).series(x, 0, 6)                 # py(ans.102,ans)
   ans = diff(cos(x**2)**2 / (1+x), x)              # py(ans.103,ans)
   ans = integrate(x**2 * cos(x), x)                # py(ans.104,ans)
   ans = integrate(x**2 * cos(x), (x, 0, pi/2))     # py(ans.105,ans)

   f = Function('f')

   eqn = Eq(Derivative(f(x),x,x) + 9*f(x), 1)       # py(ans.106,eqn)
   sol = dsolve(eqn, f(x))                          # py(ans.107,sol)

   eqn = Eq(f(x).diff(x, x) + 9*f(x), 1)            # py(ans.108,eqn)
   sol = dsolve(eqn, f(x))                          # py(ans.109,sol)

   my_int = Integral(cos(x),(x,0,2))                # py(ans.110,my_int)
   my_ans = my_int.doit()                           # py(ans.111,my_ans)
\end{python}

\clearpage

\section*{Visible Python code output}

Here is the output from the above Python code. This line of text is here for no other reason than to fill out the line so that we have two lines of text before the Python output. This makes it easier to compare the vertical spacing between this and the next example.
%
\begin{align*}
   &\py*{ans.101}\\
   &\py*{ans.102}\\
   &\py*{ans.103}\\
   &\py*{ans.104}\\
   &\py*{ans.105}\\
   &\py*{ans.106}\\
   &\py*{ans.107}\\
   &\py*{ans.108}\\
   &\py*{ans.109}\\
   &\py*{ans.110}\\
   &\py*{ans.111}
\end{align*}

\clearpage

\section*{Hidden Python code and output}

\PySetup{action=hide}

In this example the Python code is hidden and consumes no vertical space on the page. Compare the gap between this pair of lines and the following output text against that seen on the previous page.
%
\begin{python}
   from sympy import *

   x, y = symbols('x y')

   ans = limit((sin(x)-x)/x**3, x, 0)               # py(ans.201,ans)
   ans = (1/cos(x)).series(x, 0, 6)                 # py(ans.202,ans)
   ans = diff(cos(x**2)**2 / (1+x), x)              # py(ans.203,ans)
   ans = integrate(x**2 * cos(x), x)                # py(ans.204,ans)
   ans = integrate(x**2 * cos(x), (x, 0, pi/2))     # py(ans.205,ans)

   f = Function('f')

   eqn = Eq(Derivative(f(x),x,x) + 9*f(x), 1)       # py(ans.206,eqn)
   sol = dsolve(eqn, f(x))                          # py(ans.207,sol)

   eqn = Eq(f(x).diff(x, x) + 9*f(x), 1)            # py(ans.208,eqn)
   sol = dsolve(eqn, f(x))                          # py(ans.209,sol)

   my_int = Integral(cos(x),(x,0,2))                # py(ans.210,my_int)
   my_ans = my_int.doit()                           # py(ans.211,my_ans)
\end{python}
%
\begin{align*}
   &\py*{ans.201}\\
   &\py*{ans.202}\\
   &\py*{ans.203}\\
   &\py*{ans.204}\\
   &\py*{ans.205}\\
   &\py*{ans.206}\\
   &\py*{ans.207}\\
   &\py*{ans.208}\\
   &\py*{ans.209}\\
   &\py*{ans.210}\\
   &\py*{ans.211}
\end{align*}

\clearpage

\section*{Visible Python markup}

There is nothing special in this particular example. Its purpose is to provide a constart with the following example where the tags have been hidden from view.

\vspace{5pt}

\PySetup{action=show}

\begin{python}
   from sympy import *

   x = Symbol('x')

   ans = diff(cos(x)**2, x)                         # py(ans.301,ans)
   ans = integrate(2*sin(x)*exp(-x), x)             # py(ans.302,ans)
\end{python}

\begin{align*}
   &\py*{ans.301}\\
   &\py*{ans.302}
\end{align*}

\section*{Hidden Python markup}
This example creates the impression that there are no tags in the following block. But looks can be deceptive, the truth is revealed on the following page.

\vspace{5pt}

\PySetup{action=hide}

\begin{python}
   from sympy import *

   x = Symbol('x')

   ans = diff(cos(x)**2, x)                         # py(ans.401,ans)
   ans = integrate(2*sin(x)*exp(-x), x)             # py(ans.402,ans)
\end{python}

\PySetup{action=verbatim}

\begin{python}
   from sympy import *

   x = Symbol('x')

   ans = diff(cos(x)**2, x)
   ans = integrate(sin(x)*exp(-x), x)
\end{python}

\begin{align*}
   &\py*{ans.401}\\
   &\py*{ans.402}
\end{align*}

\clearpage

\section*{How to hide the tags}
The easiest way to hide the tags from view is to include two blocks, one containing the tags but hidden from view using {\tt\small\verb|\PySetup{action=hide}|}, the other block is a copy of the first but with all tags stripped out. That second block is made visible using {\tt\small\verb|\PySetup{action=verbatim}|}. This is a bit cumbersome and prone to errors (the second block must be a faithful copy of the first block). Better solutions could be devised but they all appear to require significant changes to the current preprocessors -- so much so that the this cloning trick was deemed a reasonable compromise.

Here is the LaTeX code from the previous example.

\vspace{-5pt}

\begin{minipage}[t]{0.65\textwidth}
\bgcolour{white}
\latexstyle
\begin{latex}
*  \PySetup{action=hide}
*
*  \begin{python}
\end{latex}
\pythonstyle
\begin{python}
*     from sympy import *
*
*     x = Symbol('x')
*
*     ans = diff(cos(x)**2, x)                         # py(ans.401,ans)
*     ans = integrate(2*sin(x)*exp(-x), x)             # py(ans.402,ans)
\end{python}
\latexstyle
\begin{latex}
*  \end{python}
*
*  \PySetup{action=verbatim}
*
*  \begin{python}
\end{latex}
\pythonstyle
\begin{python}
*     from sympy import *
*
*     x = Symbol('x')
*
*     ans = diff(cos(x)**2, x)
*     ans = integrate(sin(x)*exp(-x), x)
\end{python}
\latexstyle
\begin{latex}
*  \end{python}
*
*  \begin{align*}
*     &\py*{ans.401}\\
*     &\py*{ans.402}
*  \end{align*}
\end{latex}
\end{minipage}

\end{document}