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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It can happen that you need to include the results of your Sage computations and/or Sage code inside a LaTeX document. Luckily Sage provides some functions to translate its objects into LaTeX, and the listings package for LaTeX can be used to include any code (Sage, Python or any other language) in a LaTeX document.\n",
"\n",
"In this document we will describe some of these interactions between LaTeX and Sage."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The `show()` command\n",
"**Reference:** [[1](https://doc.sagemath.org/html/en/reference/repl/sage/repl/display/pretty_print.html)] (`show()` is just an alternative name for `pretty_print()`).\n",
"\n",
"With this command Sage will generate a picture displaying the object. The result depends on the object itself: most of them will be typeset in Latex, but for example graphics primitives (such as plots) will be displayed as pictures.\n",
"\n",
"You can see it as an alternative to `print()`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 + 1*x + 1/2*x^2 + 1/6*x^3 + Order(x^4)\n"
]
},
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}1 + 1 x + \\frac{1}{2} x^{2} + \\frac{1}{6} x^{3} + \\mathcal{O}\\left(x^{4}\\right)</script></html>"
],
"text/latex": [
"\\begin{math}\n",
"\\newcommand{\\Bold}[1]{\\mathbf{#1}}1 + 1 x + \\frac{1}{2} x^{2} + \\frac{1}{6} x^{3} + \\mathcal{O}\\left(x^{4}\\right)\n",
"\\end{math}"
],
"text/plain": [
"1 + 1*x + 1/2*x^2 + 1/6*x^3 + Order(x^4)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 2 3]\n",
"[ 4 5 6]\n",
"[ 8 9 10]\n"
]
},
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{rrr}\n",
"1 & 2 & 3 \\\\\n",
"4 & 5 & 6 \\\\\n",
"8 & 9 & 10\n",
"\\end{array}\\right)</script></html>"
],
"text/latex": [
"\\begin{math}\n",
"\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{rrr}\n",
"1 & 2 & 3 \\\\\n",
"4 & 5 & 6 \\\\\n",
"8 & 9 & 10\n",
"\\end{array}\\right)\n",
"\\end{math}"
],
"text/plain": [
"[ 1 2 3]\n",
"[ 4 5 6]\n",
"[ 8 9 10]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"pi\n"
]
},
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\pi</script></html>"
],
"text/latex": [
"\\begin{math}\n",
"\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\pi\n",
"\\end{math}"
],
"text/plain": [
"pi"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"s = (e^x).series(x==0, 4)\n",
"M = matrix([[1,2,3],[4,5,6],[8,9,10]])\n",
"print(s)\n",
"show(s)\n",
"print(M)\n",
"show(M)\n",
"print(pi)\n",
"show(pi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a Jupyter notebook, the results above are displayed using [MathJax](https://www.mathjax.org/).\n",
"\n",
"If you are running this code in an interactive console (terminal) instead of a Jupyter notebook, you will get the Latex source code for those objects. You can force this behavior by using the `latex()` command."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The `latex()` command\n",
"**Reference:** [[2](https://doc.sagemath.org/html/en/reference/misc/sage/misc/latex.html)]\n",
"\n",
"This command is potentially very useful if you need to include the results of Sage computations in a Latex file, especially with complex objects like matrices or very large polynomials.\n",
"\n",
"Technically, this is a function that returns a string, so you need to `print()` it to see the result."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 + 1 x + \\frac{1}{2} x^{2} + \\frac{1}{6} x^{3} + \\mathcal{O}\\left(x^{4}\\right)\n",
"\n",
"\n",
"\\left(\\begin{array}{rrr}\n",
"1 & 2 & 3 \\\\\n",
"4 & 5 & 6 \\\\\n",
"8 & 9 & 10\n",
"\\end{array}\\right)\n"
]
}
],
"source": [
"print(latex(s))\n",
"print(\"\\n\")\n",
"print(latex(M))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Interestingly, Sage can use matplotlib's PGF backend to generate Latex code for a plot. (PGF is the graphics language underlying TikZ, like TeX is the language underlying Latex)."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#latex(plot(x^2)) # The output is more than 20 pages long"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is probably easier to just generate the picture and include that in your Latex document with `\\includegraphics`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Latex name for your variables\n",
"**Reference:** [[3](https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/var.html)]\n",
"\n",
"Sometimes you might want to use variables and functions that have, for example, a Greek letter as a name. You can tell Sage that you want them displayed this way when you declare them:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"phi1(epsilon)\n"
]
},
{
"data": {
"text/html": [
"<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}e^{{\\epsilon}} + \\phi_1\\left({\\epsilon}\\right)</script></html>"
],
"text/latex": [
"\\begin{math}\n",
"\\newcommand{\\Bold}[1]{\\mathbf{#1}}e^{{\\epsilon}} + \\phi_1\\left({\\epsilon}\\right)\n",
"\\end{math}"
],
"text/plain": [
"e^epsilon + phi1(epsilon)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"e^{{\\epsilon}} + \\phi_1\\left({\\epsilon}\\right)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var('epsilon', latex_name=\"\\\\epsilon\")\n",
"function('phi1', latex_name=\"\\\\phi_1\")\n",
"\n",
"print(phi1(epsilon))\n",
"show(phi1(epsilon) + e^epsilon)\n",
"latex(phi1(epsilon) + e^epsilon)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Warning:** You need to use two backspaces `\\\\`. The reason is that in Python (like in many other programming languages) the backslash symbol inside a string is used to print special characters, such as a newline `\\n`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# From Jupyter to Latex\n",
"**Reference:** [[4](https://nbconvert.readthedocs.io/en/latest/)]\n",
"\n",
"From the Jupyter menu `File > Download as` you can choose to download your work in many formats, among which there are also Latex and pdf. Personally I prefer downloading the .tex file, so then I can change the title, add an author name and make any other change I like before compiling it into a pdf file.\n",
"\n",
"If you choose to download the pdf file, you might need to install some extra packages. For example I had to install [`pandoc`](https://pandoc.org/), `texlive-XeTeX` and `texlive-Xdvi`, but this depends on your operating system and Latex distribution."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SageTex\n",
"**Reference:** [[5](https://doc.sagemath.org/html/en/tutorial/sagetex.html)]\n",
"\n",
"With SageTex it is possible to run Sage commands directly inside Latex, using the `\\sage{}` command. In this way you don't need to run your Sage code first and then copy the results in Latex. It can be useful especially for short Sage commands.\n",
"\n",
"You might need to take some extra steps to make this work on your system, see the link above."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Latex `listings` package\n",
"**References:** [[6](https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings)] and [[7](https://ftp.snt.utwente.nl/pub/software/tex/macros/latex/contrib/listings/listings.pdf)]\n",
"\n",
"If you want to include some code (Sage, Python or anything else) in a Latex document you can use the listings package.\n",
"\n",
"```\n",
"\\usepackage{listings}\n",
"\n",
"...\n",
"\n",
"\\begin{lstlisting}[language=Python]\n",
"for i in range(0,100):\n",
" if i%5 == 0:\n",
" print(\"Multiple of 5!\")\n",
"\\end{lstlisting}\n",
"```\n",
"\n",
"You need to specify the language you are using with the `language=` option. This option can also be set at the beginning of the document using the `\\lstset{language=Python}` command.\n",
"\n",
"As an alternative, you can include a file directly without copying the code into the tex file, like you would do for a picture:\n",
"\n",
"```\n",
"\\lstinputlisting[language=Python]{file.py}\n",
"```\n",
"\n",
"It is technically possible to include Latex listings in a markdown cell of the Jupyter notebook using [this package](https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/latex_envs/README.html), but it does not make much sense. So we will move to a Latex editor for the examples."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.2",
"language": "sage",
"name": "sagemath"
},
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|