Debian Codegraph @

texmf-dist/doc/latex/pynotebook/pynotebook-doc.tex

raw
1 % !TeX TXS-program:compile = txs:///arara
2 % arara: pdflatex: {shell: no, synctex: no, interaction: batchmode}
3 % arara: pdflatex: {shell: no, synctex: no, interaction: batchmode} if found('log', '(undefined references|Please rerun|Rerun to get)')
4
5 \documentclass[english,11pt,a4paper]{article}
6 \usepackage[utf8]{inputenc}
7 \usepackage[T1]{fontenc}
8 \usepackage{pynotebook}
9 \usepackage{enumitem}
10 \usepackage{soul}
11 \usepackage{codehigh}
12 \usepackage{multicol}
13 \usepackage{fontawesome5}
14 \usepackage{fancyvrb}
15 \usepackage{fancyhdr}
16 \fancyhf{}
17 \renewcommand{\headrulewidth}{0pt}
18 %\rhead{\sffamily\small\affloetalab[Legende]}
19 \lfoot{\sffamily\small [pynotebook]}
20 \cfoot{\sffamily\small - \thepage{} -}
21 \rfoot{\hyperlink{matoc}{\small\faArrowAltCircleUp[regular]}}
22 \usepackage{hologo}
23 \usepackage{xspace}
24 \providecommand\tikzlogo{Ti\textit{k}Z}
25 \providecommand\TeXLive{\TeX{}Live\xspace}
26 \providecommand\PSTricks{\textsf{PSTricks}\xspace}
27 \let\pstricks\PSTricks
28 \let\TikZ\tikzlogo
29
30 \usepackage{hyperref}
31 \urlstyle{same}
32 \hypersetup{pdfborder=0 0 0}
33 \usepackage[margin=2cm]{geometry}
34 \setlength{\parindent}{0pt}
35
36 \def\TPversion{0.1.6}
37 \def\TPdate{20/04/2026}
38
39 \sethlcolor{lightgray!25}
40 \NewDocumentCommand\ShowCode{ m }{%
41 \hl{\vphantom{\texttt{pf}}\texttt{#1}}%
42 }
43
44 \usepackage{babel}
45
46 \begin{document}
47
48 \pagestyle{fancy}
49
50 \thispagestyle{empty}
51
52 \begin{center}
53 \begin{minipage}{0.88\linewidth}
54 \begin{tcolorbox}[colframe=yellow,colback=yellow!15]
55 \begin{center}
56 \begin{tabular}{c}
57 {\Huge \texttt{pynotebook}}\\
58 \\
59 {\LARGE Present a Jupyter notebook, with} \\
60 {\LARGE tcolorbox, and listings or piton/pyluatex.} \\
61 \\
62 {\small \texttt{Version \TPversion{} -- \TPdate}}
63 \end{tabular}
64 \end{center}
65 \end{tcolorbox}
66 \end{minipage}
67 \end{center}
68
69 \begin{center}
70 \begin{tabular}{c}
71 \texttt{Cédric Pierquet}\\
72 {\ttfamily c pierquet -- at -- outlook . fr}\\
73 \texttt{\url{https://forge.apps.education.fr/pierquetcedric/packages-latex}}
74 \end{tabular}
75 \end{center}
76
77 \hrule
78
79 \phantomsection
80
81 \hypertarget{matoc}{}
82
83 \tableofcontents
84
85 \vspace*{5mm}
86
87 \hrule
88
89 \vspace*{5mm}
90
91 \vfill~
92
93 \textit{\footnotesize Thanks to F. Pantigny for his package \textsf{piton} and his help.}\dotfill{\footnotesize\ttfamily\url{https://ctan.org/pkg/piton}}
94
95 \textit{\footnotesize And thanks to Tobias Enderle for his package \textsf{pyluatex}.}\dotfill{\footnotesize\ttfamily\url{https://ctan.org/pkg/pyluatex}}
96
97 \pagebreak
98
99 \section{Samples, with listings}
100
101 \begin{NotebookMarkdown}{\linewidth}
102 {\Large\bfseries This is a test for a \textsf{Markdown} block.}
103
104 It's possible to use \LaTeX{} formulas, like %
105 \[
106 \left\lbrace\begin{array}{l}
107 F_0 = 0\\
108 F_1 = 1 \\
109 F_{n+2} = F_{n+1} + F_n
110 \end{array}\right.
111 \]
112 \end{NotebookMarkdown}
113
114 \begin{NotebookRaw}{\linewidth}
115 This is a sample block, with RAW output.
116
117 Just to use all capacities of Jupyter notebook ;-)
118 \end{NotebookRaw}
119
120 \begin{NotebookIn}{\linewidth}
121 def fibonacci_aux(n,a,b):
122 if n == 0 :
123 return a
124 elif n == 1 :
125 return b
126 else:
127 return fibonacci_aux(n-1,b,a+b)
128
129 def fibonacci_of(n):
130 return fibonacci_aux(n,0,1)
131
132 print([fibonacci_of(n) for n in range(10)])
133 \end{NotebookIn}
134
135 \begin{NotebookOut}{\linewidth}
136 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
137 \end{NotebookOut}
138
139 \begin{NotebookRaw}{\linewidth}
140 Now we’re going to work with lists.
141
142 Just a little example with a prime numbers, in french.
143 \end{NotebookRaw}
144
145 \begin{NotebookIn}{\linewidth}
146 def proc_exec() :
147 choix = "o"
148 while choix == "o" :
149 n = -1
150 while n <= 1 :
151 n = int(input("Saisir un entier n, supérieur à 2 : "))
152 if estpremier(n) == True :
153 print(f"{n} est premier.")
154 else :
155 print(f"{n} n'est pas premier.")
156 listeres = listenombrepremiers(n)
157 print(f"La liste des entiers premiers <= à {n} est {listeres}.")
158 print(f"Il y a donc {len(listeres)} entiers premiers <= à {n}.")
159 choix = input("Recommencer [o/n] ? ")
160 \end{NotebookIn}
161
162 \begin{NotebookIn}{\linewidth}
163 proc_exec()
164 \end{NotebookIn}
165
166 \begin{NotebookConsole}{\linewidth}
167 Saisir un entier n, supérieur à 2 : 14
168 14 n'est pas premier.
169 La liste des entiers premiers <= à 14 est [2, 3, 5, 7, 11, 13].
170 Il y a donc 6 entiers premiers <= à 14.
171 Recommencer [o/n] ? o
172 Saisir un entier n, supérieur à 2 : 1
173 Saisir un entier n, supérieur à 2 : -3
174 Saisir un entier n, supérieur à 2 : 25
175 25 n'est pas premier.
176 La liste des entiers premiers <= à 25 est [2, 3, 5, 7, 11, 13, 17, 19, 23].
177 Il y a donc 9 entiers premiers <= à 25.
178 Recommencer [o/n] ? n
179 \end{NotebookConsole}
180 %\vspace*{5mm}
181 %
182 %\begin{center}
183 % With \ShowCode{piton} and \ShowCode{pyluatex} (with code execution, but with \hologo{LuaLaTeX} and \textsf{--shell-escape}) :
184 %\end{center}
185 %
186 %\includegraphics[clip]{pynotebook-samples-pitonpyluatex.pdf}
187
188 \pagebreak
189
190 \section{History}
191
192 \verb|v0.1.6|~:~~~~Bugfix wih pyluatex (\texttt{>0.7.0})
193
194 \verb|v0.1.5|~:~~~~Enhancements with \textsf{piton} (margins)
195
196 \verb|v0.1.4|~:~~~~Enhancements with \textsf{piton}
197
198 \verb|v0.1.3|~:~~~~Modification with \texttt{gobble} (for \textsf{piton})
199
200 \verb|v0.1.2|~:~~~~Enhancements with \texttt{gobble} (for \textsf{piton})
201
202 \verb|v0.1.1|~:~~~~New block \textsf{In/Out} with \textsf{piton/pyluatex} (tks to F. Pantigny)
203
204 \verb|v0.1.0|~:~~~~Initial version
205
206 \vspace*{15mm}
207
208 \pagebreak
209
210 \section{The package pynotebook}
211
212 \subsection{Ideas}
213
214 The idea is to provide environments to reproduce a Jupyter notebook :
215
216 \begin{itemize}
217 \item with \textit{blocks} for \textsf{RAW} or \textsf{Markdown} ;
218 \item with \ShowCode{listings} and no limitation with compiler, but without code execution ;
219 \item with \ShowCode{piton} and \ShowCode{pyluatex} with \hologo{LuaLaTeX} and \textsf{--shell-escape}.
220 \end{itemize}
221
222 The documentation use \hologo{pdfLaTeX}, but examples with \hologo{LuaLaTeX} are given in an other doc.
223
224 \subsection{Loading}
225
226 The package loads within the preamble, with \ShowCode{\textbackslash usepackage\{pynotebook\}}.
227
228 The loaded packages are \ShowCode{tcolorbox} (with \ShowCode{skins,breakable,listings}), \ShowCode{calc}, \ShowCode{xstring} and \ShowCode{iftex}.
229
230 If \hologo{LuaLaTeX} is detected, \ShowCode{piton} is loaded (but there's an option to avoid the loading), whereas \ShowCode{pyluatex} needs to be manually loaded, due to the declaration of the executable.
231
232 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
233 %with pdflatex
234 \usepackage{pynotebook}
235 \end{codehigh}
236
237 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
238 %with LuaLaTeX and piton
239 \usepackage{pynotebook}
240 \usepackage[options]{pyluatex}
241 \end{codehigh}
242
243 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
244 %with LuaLaTeX but without piton capability
245 \usepackage[nopiton]{pynotebook}
246 \end{codehigh}
247
248 \subsection{Global usage}
249
250 In order to respect the left-alignment, the \textit{titles} \ShowCode{In [ ]} and \ShowCode{Out[ ]} can add a blank character, to avoid offset due to counter with two digits !
251
252 \pagebreak
253
254 \section{Common text blocks}
255
256 \subsection{Intro}
257
258 The different text blocks are given with their own output.
259
260 The package provides environments :
261
262 \begin{itemize}
263 \item for a \textsf{RAW} block, with \texttt{teletype} font ; for a \textsf{Mardown} block, with all \hologo{LaTeX} support ;
264 \item a version with \textsf{piton} is given, in order to align perfectly the blocks !
265 \end{itemize}
266
267 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
268 \begin{NotebookRaw}[options tcbox]{<width>}
269 <code>
270 \end{NotebookRaw}
271 \end{codehigh}
272
273 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
274 \begin{NotebookMarkdown}[options tcbox]{<width>}
275 <code>
276 \end{NotebookMarkdown}
277 \end{codehigh}
278
279 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
280 \begin{NotebookPitonRaw}[options tcbox]{<width>}
281 <code>
282 \end{NotebookPitonRaw}
283 \end{codehigh}
284
285 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
286 \begin{NotebookPitonMarkdown}[options tcbox]{<width>}
287 <code>
288 \end{NotebookPitonMarkdown}
289 \end{codehigh}
290
291 \subsection{Examples}
292
293 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
294 \begin{NotebookMarkdown}{\linewidth}
295 {\Large\bfseries This is a test for a \textsf{Markdown} block.}\\
296 It's possible to use \LaTeX{} formulas, like %
297 \[
298 \left\lbrace\begin{array}{l}
299 F_0 = 0\\
300 F_1 = 1 \\
301 F_{n+2} = F_{n+1} + F_n
302 \end{array}\right.
303 \]
304 \end{NotebookMarkdown}
305
306 \begin{NotebookRaw}{\linewidth}
307 This is a sample block, with RAW output.
308
309 Just to use all capacities of Jupyter notebook ;-)
310 \end{NotebookRaw}
311 \end{codehigh}
312
313 \begin{NotebookMarkdown}{\linewidth}
314 {\Large\bfseries This is a test for a \textsf{Markdown} block.}\\
315 It's possible to use \LaTeX{} formulas, like %
316 \[
317 \left\lbrace\begin{array}{l}
318 F_0 = 0\\
319 F_1 = 1 \\
320 F_{n+2} = F_{n+1} + F_n
321 \end{array}\right.
322 \]
323 \end{NotebookMarkdown}
324
325 \begin{NotebookRaw}{\linewidth}
326 This is a sample block, with RAW output.
327
328 Just to use all capacities of Jupyter notebook ;-)
329 \end{NotebookRaw}
330
331 \pagebreak
332
333 \section{The code blocks, with listings}
334
335 \subsection{Intro}
336
337 With \ShowCode{listings}, the different blocks are given with their own output (no code execution).
338
339 The package provides environments :
340
341 \begin{itemize}
342 \item with \texttt{In~[...]} ;
343 \item with \texttt{Out[...]} ;
344 \item without \textit{header}, eg for a \textit{console execution}.
345 \end{itemize}
346
347 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
348 \begin{NotebookIn}(*)[options tcbox]{<width>}
349 <code>
350 \end{NotebookIn}
351 \end{codehigh}
352
353 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
354 \begin{NotebookOut}(*)[options tcbox]{<width>}
355 <code>
356 \end{NotebookOut}
357 \end{codehigh}
358
359 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
360 \begin{NotebookConsole}[options tcbox]{<width>}
361 <code>
362 \end{NotebookConsole}
363 \end{codehigh}
364
365 The starred versions removes the counter, and don't display it.
366
367 The blocks with \textit{header} (\texttt{In/Out}) are automatically numbered, and the global style is fixed.
368
369 \subsection{Examples}
370
371 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
372 \begin{NotebookIn}{\linewidth}
373 def fibonacci_aux(n,a,b):
374 if n == 0 :
375 return a
376 elif n == 1 :
377 return b
378 else:
379 return fibonacci_aux(n-1,b,a+b)
380
381 def fibonacci_of(n):
382 return fibonacci_aux(n,0,1)
383
384 [fibonacci_of(n) for n in range(10)]
385 \end{NotebookIn}
386
387 \begin{NotebookOut}{\linewidth}
388 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
389 \end{NotebookOut}
390
391 \begin{NotebookConsole}{\linewidth}
392 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
393 \end{NotebookConsole}
394 \end{codehigh}
395
396 \pagebreak
397
398 \begin{NotebookIn}{\linewidth}
399 def fibonacci_aux(n,a,b):
400 if n == 0 :
401 return a
402 elif n == 1 :
403 return b
404 else:
405 return fibonacci_aux(n-1,b,a+b)
406
407 def fibonacci_of(n):
408 return fibonacci_aux(n,0,1)
409
410 [fibonacci_of(n) for n in range(10)]
411 \end{NotebookIn}
412
413 \begin{NotebookOut}{\linewidth}
414 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
415 \end{NotebookOut}
416
417 \begin{NotebookConsole}{\linewidth}
418 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
419 \end{NotebookConsole}
420
421 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
422 \begin{NotebookIn}*[flush right]{13cm}
423 def fibonacci_aux(n,a,b):
424 if n == 0 :
425 return a
426 elif n == 1 :
427 return b
428 else:
429 return fibonacci_aux(n-1,b,a+b)
430
431 def fibonacci_of(n):
432 return fibonacci_aux(n,0,1)
433
434 [fibonacci_of(n) for n in range(10)]
435 \end{NotebookIn}
436
437 \begin{NotebookOut}*[flush right]{13cm}
438 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
439 \end{NotebookOut}
440
441 \begin{NotebookConsole}[flush right]{13cm}
442 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
443 \end{NotebookConsole}
444 \end{codehigh}
445
446 \begin{NotebookIn}*[flush right]{13cm}
447 def fibonacci_aux(n,a,b):
448 if n == 0 :
449 return a
450 elif n == 1 :
451 return b
452 else:
453 return fibonacci_aux(n-1,b,a+b)
454
455 def fibonacci_of(n):
456 return fibonacci_aux(n,0,1)
457
458 [fibonacci_of(n) for n in range(10)]
459 \end{NotebookIn}
460
461 \begin{NotebookOut}*[flush right]{13cm}
462 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
463 \end{NotebookOut}
464
465 \begin{NotebookConsole}[flush right]{13cm}
466 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
467 \end{NotebookConsole}
468
469 \pagebreak
470
471 \section{The code blocks, with piton and pyluatex}
472
473 \subsection{Intro}
474
475 With \ShowCode{piton} and \ShowCode{pyluatex}, the different blocks are given with the code to be displayed (\texttt{In/Out}) or with the code to be executed (\textsf{Out} or \textsf{Console}).
476
477 The package provides environments :
478
479 \begin{itemize}
480 \item with \texttt{In~[...]} ;
481 \item with \texttt{Out[...]} ;
482 \item with \texttt{In[...]\&{}Out[...]} ;
483 \item without \textit{header}, eg for a \textit{console execution}.
484 \end{itemize}
485
486 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
487 \begin{NotebookPitonRaw}[options tcbox]{width}<gobble options>
488 <code>
489 \end{NotebookPitonRaw}
490 \end{codehigh}
491
492 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
493 \begin{NotebookPitonMarkdown}[options tcbox]{width}
494 <code>
495 \end{NotebookPitonMarkdown}
496 \end{codehigh}
497
498 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
499 \begin{NotebookPitonIn}(*)[options tcbox]{width}<gobble options>
500 <code>
501 \end{NotebookPitonIn}
502 \end{codehigh}
503
504 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
505 \begin{NotebookPitonOut}(*)[options tcbox]{width}<gobble options>
506 <code>
507 \end{NotebookPitonOut}
508 \end{codehigh}
509
510 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
511 \begin{NotebookPitonInOut}(*)[options tcbox]{width}<gobble options>
512 <code>
513 \end{NotebookPitonInOut}
514 \end{codehigh}
515
516 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
517 \begin{NotebookPitonConsole}[options tcbox]{width}<gobble options>
518 <code>
519 \end{NotebookPitonConsole}
520 \end{codehigh}
521
522 The starred versions removes the counter, and don't display it.
523
524 The blocks with \textit{header} (\texttt{In/Out}) are automatically numbered, and the global style is fixed.
525
526 \smallskip
527
528 \texttt{gobble options} are given within \textsf{piton} syntax :
529
530 \begin{itemize}
531 \item \texttt{auto-gobble} ;
532 \item \texttt{env-gobble} ;
533 \item \texttt{gobble=xx} ;
534 \item \texttt{tabs-auto-gobble} ;
535 \item a mix of them.
536 \end{itemize}
537
538 \subsection{Examples}
539
540 Due to the necessary usage of \hologo{LuaLaTeX} and \textsf{--shell-escape}, examples are given in a separate file.
541
542 \pagebreak
543
544 \section{Some customization}
545
546 \subsection{Ideas}
547
548 The package provides macros, in order to :
549
550 \begin{itemize}[itemsep=2pt]
551 \item configure the \textit{words} \texttt{In/Out} in \textsf{french} ;
552 \item configure the spacing before and after the boxes (\ShowCode{0.33\textbackslash baselineskip} by default).
553 \end{itemize}
554
555 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
556 \SetJupyterLng{fr} %set french words
557
558 \SetJupyterParSkip{<length>} %modify space before/after (or default)
559
560 \setcounter{JupyterIn}{<nb>} %modify the counter
561 \end{codehigh}
562
563 \subsection{Examples}
564
565 \begin{codehigh}[language=latex/latex2,style/main=cyan!10,style/code=cyan!10]
566 \SetJupyterLng{fr}
567 \SetJupyterParSkip{\baselineskip}
568 \setcounter{JupyterIn}{14}
569
570 \begin{NotebookIn}{0.75\linewidth}
571 def fibonacci_aux(n,a,b):
572 if n == 0 :
573 return a
574 elif n == 1 :
575 return b
576 else:
577 return fibonacci_aux(n-1,b,a+b)
578
579 def fibonacci_of(n):
580 return fibonacci_aux(n,0,1)
581
582 [fibonacci_of(n) for n in range(15)]
583 \end{NotebookIn}
584
585 \begin{NotebookOut}{0.75\linewidth}
586 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
587 \end{NotebookOut}
588 \end{codehigh}
589
590 \SetJupyterLng{fr}
591 \SetJupyterParSkip{\baselineskip}
592 \setcounter{JupyterIn}{14}
593
594 \begin{NotebookIn}{0.75\linewidth}
595 def fibonacci_aux(n,a,b):
596 if n == 0 :
597 return a
598 elif n == 1 :
599 return b
600 else:
601 return fibonacci_aux(n-1,b,a+b)
602
603 def fibonacci_of(n):
604 return fibonacci_aux(n,0,1)
605
606 [fibonacci_of(n) for n in range(10)]
607 \end{NotebookIn}
608
609 \begin{NotebookOut}{0.75\linewidth}
610 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
611 \end{NotebookOut}
612
613 \end{document}