Debian Codegraph @

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

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % pyjupyter.tex
3 % pyjupyter package documentation
4 % Author: Ouyèté Bright BARA
5 % Date: 2026/04/07
6 % Version: 1.2.1
7 % License: LPPL v1.3c
8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9
10 \documentclass[12pt,a4paper]{article}
11 \usepackage[T1]{fontenc}
12 \usepackage[utf8]{inputenc}
13 \usepackage[english]{babel}
14 \usepackage{mathpazo}
15 \usepackage[colorlinks]{hyperref}
16 \usepackage{url}
17 \usepackage{geometry}
18 \geometry{margin=2.5cm, bottom=2cm}
19 \usepackage{parskip}
20 \usepackage{pyjupyter}
21
22 \title{The \texttt{pyjupyter} package\\[0.25cm]\large Version: 1.2.1 - 2026/04/07}
23 \author{Ouyèté Bright BARA\\ \hypersetup{hidelinks}\href{mailto:[email protected]}{\ttfamily [email protected]}}
24 \date{\today}
25
26
27
28 \begin{document}
29
30 \maketitle
31
32 \renewcommand{\contentsname}{\bfseries\Large Contents\\[-0.6cm]\hrule}
33 {\hypersetup{hidelinks}\tableofcontents \vspace*{0.25cm}\hrule}
34
35
36 \section{Introduction}
37
38 The \texttt{pyjupyter} package provides a lightweight environment
39 for typesetting Python code in \LaTeX\ documents with a visual style
40 inspired by Jupyter notebooks.
41
42 The package combines the syntax highlighting capabilities of
43 \texttt{listings} with the layout and framing features of
44 \texttt{tcolorbox}. The result is a structured and readable
45 presentation of Python code blocks suitable for:
46
47 \begin{itemize}
48 \item scientific reports
49 \item programming assignments
50 \item lecture notes
51 \item reproducible research documents
52 \end{itemize}
53
54 A key motivation for this package is to provide a clean alternative to
55 standard Jupyter-to-\LaTeX\ conversions. Typical conversion tools
56 generate extremely verbose code, rely on heavy \texttt{Pygments}
57 dependencies, and clutter the preamble with numerous \texttt{renewcommand}
58 definitions, making the source file difficult to read and maintain.
59 \texttt{pyjupyter} avoids this complexity by offering a native,
60 minimalist approach that remains fully compatible with standard
61 \LaTeX\ workflows.
62
63 It is important to note that \textbf{the purpose of this package is purely
64 typographical}: \texttt{pyjupyter} does not execute or compile Python
65 code. Its goal is solely to provide a clean and convenient way to
66 display Python source code inside \LaTeX\ documents.
67
68 \section{Installation}
69
70 If you are using a modern \LaTeX\ distribution (such as \TeX\ Live or MiKTeX),
71 the package can be installed automatically through your package manager.
72
73 Alternatively, you can manually install the package by following these steps:
74
75 \begin{enumerate}
76 \item Download the package from: {\hypersetup{colorlinks, urlcolor=blue}\url{https://mirrors.ctan.org/macros/latex/contrib/pyjupyter}}\\
77 \item Place the file \verb|pyjupyter.sty| in the working directory of your
78 project or in your local \texttt{texmf} tree.\\
79 \item Run \texttt{texhash} (if necessary) to update your database.
80 \end{enumerate}
81
82
83 \section{Loading the package}
84
85 Load the package in the preamble:\\
86
87 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
88 \usepackage{pyjupyter}
89 \end{lstlisting}
90
91 The package automatically loads the required dependencies.
92
93 \section{The \texttt{jupyter} environment}
94
95 The package defines a single environment called \texttt{jupyter}.\\
96
97 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
98 \begin{jupyter}[]
99 # Python code
100 print("Hello")
101 \end{jupyter}
102 \end{lstlisting}
103
104 \subsection*{Example}
105
106 \begin{jupyter}[]
107 # Example Python code
108 def square(x):
109 return x ** 2
110
111 print(square(4))
112 \end{jupyter}
113
114 The environment produces a framed code block with Python syntax
115 highlighting.
116
117 \section{Important notes}
118
119 \subsection{About the brackets}
120
121 The \texttt{jupyter} environment must always be invoked with brackets immediately
122 after\\ \verb|\begin{jupyter}| :
123 \vspace*{0.5cm}
124
125 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
126 \begin{jupyter}[]
127 ...
128 \end{jupyter}
129 \end{lstlisting}
130
131 This applies even when no options are specified.
132
133 The reason for this is that Python comments start with the character \texttt{\#}.
134 In \TeX\, the character \texttt{\#} is also used internally to denote macro parameters.
135
136 If the environment is started without the optional argument brackets
137 and the first line of code begins with a Python comment, \TeX{} may
138 interpret the character incorrectly during argument parsing.
139
140 Providing the optional brackets ensures that the environment is fully
141 initialized before the code content is processed by the
142 \texttt{listings} engine. This prevents compilation errors when the
143 first line of the code block is a Python comment.
144
145 \subsection{About highlighting of operators and spaces}
146
147 The current version of \texttt{pyjupyter} relies on the \texttt{listings}
148 engine for syntax highlighting. Due to the way this engine parses
149 characters, mathematical and logical operators (such as \texttt{+},
150 \texttt{-}, \texttt{=}, \texttt{*}, etc.) are highlighted in
151 \textbf{violet} only when they are surrounded by spaces.
152
153 If the code is written in a compact way (e.g., \texttt{x=5+2}), the
154 operators will remain in the default text color (black).
155
156 \begin{itemize}
157 \item \textbf{Correct highlighting} : \texttt{x = 5 + 2}
158 \item \textbf{No highlighting} : \texttt{x=5+2}
159 \end{itemize}
160
161 While this is a technical limitation of the underlying engine, it
162 also promotes the \textbf{PEP 8} style guidelines, which recommend
163 surrounding operators with spaces for better readability.
164
165
166 \begin{jupyter}[numbered]
167 # Operators are highlighted with spaces:
168 a = 10
169 b = 5
170 result = a + b
171 # Operators remain black without spaces:
172 result=a+b
173 \end{jupyter}
174
175 \section{Line numbering}
176
177 Line numbering can be activated using the \texttt{numbered} option.\\
178
179 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
180 \begin{jupyter}[numbered]
181 for i in range(5):
182 print(i)
183 \end{jupyter}
184 \end{lstlisting}
185
186 \begin{jupyter}[numbered]
187 for i in range(5):
188 print(i)
189 \end{jupyter}
190
191 The option internally configures the settings of the
192 \texttt{listings} engine.
193
194
195 \section{Font and Size Configuration}
196
197 By default, \texttt{pyjupyter} uses the \texttt{sourcecodepro} family at \texttt{small} size. These settings can be customized globally through package options to suit your document's aesthetic.
198
199 \subsection{Font Families}
200 You can globally customize the font for all your Jupyter blocks by passing the \texttt{font} option when loading the package. The following families are supported:
201
202 \begin{itemize}
203 \item \texttt{sourcecodepro} (default)
204 \item \texttt{inconsolata}
205 \item \texttt{beramono}
206 \item \texttt{lmtt}
207 \item \texttt{courier}
208 \item \texttt{txtt}
209 \end{itemize}
210
211 \subsection{Code Size}
212 For the code size, you can specify the \LaTeX\ font size command \textbf{without the backslash}.
213 The most common values are:
214
215 \begin{itemize}
216 \item \texttt{tiny}, \texttt{scriptsize} or \texttt{footnotesize}
217 \item \texttt{small} (default)
218 \item \texttt{normalsize}
219 \end{itemize}
220
221 \textit{Note: While larger options like \texttt{large}, \texttt{Large}, or \texttt{LARGE} are technically supported, they are generally not recommended for code listings.}
222
223 \subsection{Usage Example}
224
225 To use \texttt{inconsolata} with a smaller font size (\texttt{footnotesize}) throughout your entire document, write this in the preamble:\\
226
227 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
228 \usepackage[font=inconsolata, codesize=footnotesize]{pyjupyter}
229 \end{lstlisting}
230
231
232 \section{Customization}
233
234 \subsection{Passing options to \texttt{tcolorbox}}
235
236 The \texttt{jupyter} environment allows further customization by
237 passing options to the underlying \texttt{tcolorbox} environment.\\
238
239 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
240 \begin{jupyter}[colback=blue!3,colframe=Navy,title=Example]
241 print("Custom box")
242 \end{jupyter}
243 \end{lstlisting}
244
245 \begin{jupyter}[colback=blue!3,colframe=Navy,title=Example]
246 print("Custom box")
247 \end{jupyter}
248
249 \subsection{Passing options to \texttt{listings}}
250
251 Advanced users may pass options directly to the \texttt{listings}
252 engine using the key \texttt{listing options}.\\
253
254 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
255 \begin{jupyter}[listing options={basicstyle=\ttfamily\small}]
256 print("Custom listings configuration")
257 \end{jupyter}
258 \end{lstlisting}
259
260 \begin{jupyter}[listing options={basicstyle=\ttfamily\small}]
261 print("Custom listings configuration")
262 \end{jupyter}
263
264 \section{Embedding \LaTeX\ code}
265
266 It is possible to insert \LaTeX\ code inside a code block using the
267 escape delimiters defined in the listings configuration.
268
269 The escape characters are two @:
270
271 \begin{verbatim}
272 @ ... @
273 \end{verbatim}
274
275 Everything between @ delimiters is processed as normal \LaTeX\ code.
276
277 Example:
278
279 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
280 \begin{jupyter}[]
281 x = 10
282 y = 20
283 print(x+y) @\hspace*{3cm}\color{orange}$\alpha$ Result@
284 \end{jupyter}
285 \end{lstlisting}
286
287 \begin{jupyter}[]
288 x = 10
289 y = 20
290 print(x+y) @\hspace*{3cm}\color{orange}$\alpha$ Result@
291 \end{jupyter}
292
293 This feature allows the user to manually emphasize elements such as function or method names when they are not automatically detected by the syntax highlighting engine.
294
295 \section{Including Python files}
296
297 The package provides a dedicated command \verb|\inputpythonfile| for including external Python files into your \LaTeX\ document.
298
299 This command wraps the file in a \texttt{tcolorbox} with the same Jupyter-style formatting as the \texttt{jupyter} environment, and fully supports syntax highlighting, line numbering, and font customization.
300
301 \subsection*{Basic usage}
302
303 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
304 \inputpythonfile{example.py}
305 \end{lstlisting}
306
307 This will include the entire Python file with default styling.
308
309 \subsection*{Line numbering}
310
311 To enable line numbers inside the included file, pass the \texttt{numbered} option:\\
312
313 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
314 \inputpythonfile[numbered]{example.py}
315 \end{lstlisting}
316
317 \subsection*{Selecting specific lines}
318
319 You can include only a specific range of lines using the \texttt{startline} and \texttt{endline} options:
320
321 \begin{lstlisting}[language=TeX, basicstyle=\ttfamily\small, frame=single]
322 \inputpythonfile[numbered, startline=5, endline=10]{example.py}
323 \end{lstlisting}
324
325 This is useful to show only relevant portions of a long Python file.
326
327 \subsection*{Custom box options}
328
329 You can also customize the \texttt{tcolorbox} appearance by passing standard box options:
330
331 \begin{lstlisting}[language=TeX,basicstyle=\ttfamily\scriptsize, frame=single]
332 \inputpythonfile[title=Included Python code, colframe=Navy, colback=blue!2]{example.py}
333 \end{lstlisting}
334
335 \inputpythonfile[numbered, title=Included Python code, colframe=Navy, colback=blue!2]{example.py}
336
337 This approach allows you to display Python files in a clean, readable format while controlling which lines are shown, preserving syntax highlighting, line numbering, and font style from the global \texttt{pyjupyter} configuration.
338
339
340 \section{Syntax highlighting}
341
342 Python syntax highlighting is provided by the \texttt{listings}
343 engine. The package defines a custom style called
344 \texttt{pyjupyter-syntax}.
345
346 The style includes:
347
348 \begin{itemize}
349 \item highlighting of Python keywords
350 \item highlighting of operators
351 \item colored comments
352 \item colored strings
353 \item support for triple-quoted strings
354 \item automatic line breaking
355 \end{itemize}
356
357 \section{UTF-8 characters}
358
359 The package includes support for several accented UTF-8 characters
360 when used inside listings environments.
361
362 This allows code comments written with common accented characters
363 to compile correctly.
364
365 \begin{jupyter}[]
366 print("L'étudiant Ouyèté se débrouille en LaTeX !")
367 \end{jupyter}
368
369
370 \section{Version history}
371
372 The following list summarizes the evolution of the \texttt{pyjupyter} package.
373
374 \begin{itemize}
375
376 \item \textbf{v1.2.1 (2026/04/07)}: Minor update about numbering. It consists in a redesigning of the \texttt{numbered} style.
377
378 \item \textbf{v1.2.0 (2026/03/15)}: Added support for including external Python files.
379 \begin{itemize}
380 \item New command \verb|\inputpythonfile| for embedding Python files in a Jupyter-style box.
381 \item Ability to display only specific lines using the \texttt{startline} and \texttt{endline} options.
382 \item Full support for syntax highlighting, line numbering, font customization and custom box options within included files.
383 \end{itemize}
384
385 \item \textbf{v1.1.0 (2026/03/10)}: Major update with enhanced flexibility.
386 \begin{itemize}
387 \item Global font selection: Added the \texttt{font} package option to choose between \texttt{sourcecodepro}, \texttt{beramono}, \texttt{inconsolata}, \texttt{lmtt}, \texttt{courier}, and \texttt{txtt}.
388 \item Improved numbering: Redesigned the \texttt{numbered} style to ensure compatibility with any font selected in the preamble.
389 \item Code size customization: Introduced the \texttt{codesize} option to globally modify the font size of code blocks from the preamble.
390 \end{itemize}
391
392 \item \textbf{v1.0.0 (2026/03/06)}: Initial release.
393 \begin{itemize}
394 \item Basic Jupyter-like styling using \texttt{tcolorbox} and \texttt{listings}.
395 \item Default syntax highlighting for Python.
396 \item Integration of \texttt{sourcecodepro} as the default font.
397 \end{itemize}
398
399 \end{itemize}
400
401 \end{document}