| 1 | # pyjupyter |
| 2 | |
| 3 | **pyjupyter** is a lightweight LaTeX package that provides a clean and structured way to typeset Python code with a visual style inspired by Jupyter notebooks. |
| 4 | |
| 5 | The package combines the syntax highlighting capabilities of the `listings` package with the flexible layout and framing features of `tcolorbox`. The result is a simple and elegant environment for presenting Python code in scientific reports, programming assignments, lecture notes, and reproducible research documents. |
| 6 | |
| 7 | A key motivation behind **pyjupyter** is to offer a minimal and native alternative to traditional Jupyter-to-LaTeX export tools. Many existing solutions rely on external tools such as **Pygments**, produce extremely verbose LaTeX code, and introduce numerous formatting macros that make the document difficult to read and maintain. |
| 8 | The goal of this package is to provide a clean solution that integrates naturally into standard LaTeX workflows. |
| 9 | |
| 10 | --- |
| 11 | |
| 12 | # Installation |
| 13 | |
| 14 | If you are using a modern LaTeX distribution such as **TeX Live** or **MiKTeX**, the package can be installed automatically through the package manager. |
| 15 | |
| 16 | Alternatively, you can install the package manually: |
| 17 | |
| 18 | 1. Download the package from the CTAN repository |
| 19 | https://mirrors.ctan.org/macros/latex/contrib/pyjupyter |
| 20 | 2. Place `pyjupyter.sty` in your project directory or in your local `texmf` tree. |
| 21 | 3. Run `texhash` if required to refresh the file database. |
| 22 | |
| 23 | --- |
| 24 | |
| 25 | # Loading the package |
| 26 | |
| 27 | Load the package in the preamble of your document: |
| 28 | |
| 29 | ``` |
| 30 | \usepackage{pyjupyter} |
| 31 | ``` |
| 32 | |
| 33 | The package automatically loads all required dependencies. |
| 34 | |
| 35 | --- |
| 36 | |
| 37 | # The `jupyter` environment |
| 38 | |
| 39 | The package defines a single environment called `jupyter` for displaying Python code. |
| 40 | |
| 41 | ``` |
| 42 | \begin{jupyter}[] |
| 43 | # Python code |
| 44 | print("Hello world") |
| 45 | \end{jupyter} |
| 46 | ``` |
| 47 | |
| 48 | This environment produces a framed code block with Python syntax highlighting. |
| 49 | |
| 50 | Example: |
| 51 | |
| 52 | ``` |
| 53 | \begin{jupyter}[] |
| 54 | def square(x): |
| 55 | return x ** 2 |
| 56 | |
| 57 | print(square(4)) |
| 58 | \end{jupyter} |
| 59 | ``` |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | # Important notes |
| 64 | |
| 65 | ## About the brackets |
| 66 | |
| 67 | The `jupyter` environment must always be called with brackets immediately after `\begin{jupyter}`: |
| 68 | |
| 69 | ``` |
| 70 | \begin{jupyter}[] |
| 71 | ... |
| 72 | \end{jupyter} |
| 73 | ``` |
| 74 | |
| 75 | This applies even when no options are specified. |
| 76 | |
| 77 | Python comments begin with the character `#`, which is also used internally by TeX to denote macro parameters. |
| 78 | If the environment is started without the optional argument brackets and the first line of code begins with a comment, TeX may misinterpret the character during argument parsing. |
| 79 | |
| 80 | Providing the brackets ensures that the environment is fully initialized before the code content is processed by the `listings` engine. |
| 81 | |
| 82 | --- |
| 83 | |
| 84 | ## About operator highlighting |
| 85 | |
| 86 | The `listings` engine highlights operators only when they are surrounded by spaces. |
| 87 | |
| 88 | Example: |
| 89 | |
| 90 | Correct highlighting: |
| 91 | |
| 92 | ``` |
| 93 | a = 10 |
| 94 | b = 5 |
| 95 | result = a + b |
| 96 | ``` |
| 97 | |
| 98 | Without spaces the operators remain in the default color: |
| 99 | |
| 100 | ``` |
| 101 | result=a+b |
| 102 | ``` |
| 103 | |
| 104 | Although this is a limitation of the underlying engine, it is consistent with the **PEP 8** Python style guidelines which recommend surrounding operators with spaces. |
| 105 | |
| 106 | --- |
| 107 | |
| 108 | # Line numbering |
| 109 | |
| 110 | Line numbering can be activated using the `numbered` option. |
| 111 | |
| 112 | ``` |
| 113 | \begin{jupyter}[numbered] |
| 114 | for i in range(5): |
| 115 | print(i) |
| 116 | \end{jupyter} |
| 117 | ``` |
| 118 | |
| 119 | --- |
| 120 | |
| 121 | # Font and size configuration |
| 122 | |
| 123 | By default the package uses the **Source Code Pro** font family at `small` size. |
| 124 | |
| 125 | Both the font family and code size can be configured globally when loading the package. |
| 126 | |
| 127 | Example: |
| 128 | |
| 129 | ``` |
| 130 | \usepackage[font=inconsolata, codesize=footnotesize]{pyjupyter} |
| 131 | ``` |
| 132 | |
| 133 | Supported fonts: |
| 134 | |
| 135 | * sourcecodepro (default) |
| 136 | * inconsolata |
| 137 | * beramono |
| 138 | * lmtt |
| 139 | * courier |
| 140 | * txtt |
| 141 | |
| 142 | Supported sizes: |
| 143 | |
| 144 | * tiny |
| 145 | * scriptsize |
| 146 | * footnotesize |
| 147 | * small (default) |
| 148 | * normalsize |
| 149 | * large |
| 150 | |
| 151 | Large sizes are technically supported but generally not recommended for code listings. |
| 152 | |
| 153 | --- |
| 154 | |
| 155 | # Customization |
| 156 | |
| 157 | Additional options can be passed directly to the underlying `tcolorbox`. |
| 158 | |
| 159 | Example: |
| 160 | |
| 161 | ``` |
| 162 | \begin{jupyter}[colback=blue!3, colframe=Navy, title=Example] |
| 163 | print("Custom box") |
| 164 | \end{jupyter} |
| 165 | ``` |
| 166 | |
| 167 | Advanced users may also pass options directly to the `listings` engine using: |
| 168 | |
| 169 | ``` |
| 170 | \begin{jupyter}[listing options={basicstyle=\ttfamily\small}] |
| 171 | print("Custom listings configuration") |
| 172 | \end{jupyter} |
| 173 | ``` |
| 174 | |
| 175 | --- |
| 176 | |
| 177 | # Embedding LaTeX code |
| 178 | |
| 179 | LaTeX code can be inserted inside a Python block using escape delimiters. |
| 180 | |
| 181 | The escape characters are `@ ... @`. |
| 182 | |
| 183 | Example: |
| 184 | |
| 185 | ``` |
| 186 | \begin{jupyter}[] |
| 187 | x = 10 |
| 188 | y = 20 |
| 189 | print(x+y) @\hspace*{3cm}\color{orange}$\alpha$ Result@ |
| 190 | \end{jupyter} |
| 191 | ``` |
| 192 | |
| 193 | Everything between `@` delimiters is processed as normal LaTeX code. |
| 194 | |
| 195 | --- |
| 196 | |
| 197 | # Including Python files |
| 198 | |
| 199 | The package provides a command for including external Python files directly in the document. |
| 200 | |
| 201 | ``` |
| 202 | \inputpythonfile{example.py} |
| 203 | ``` |
| 204 | |
| 205 | This command wraps the file inside a Jupyter-style box with syntax highlighting. |
| 206 | |
| 207 | --- |
| 208 | |
| 209 | ## Line numbering |
| 210 | |
| 211 | ``` |
| 212 | \inputpythonfile[numbered]{example.py} |
| 213 | ``` |
| 214 | |
| 215 | --- |
| 216 | |
| 217 | ## Selecting specific lines |
| 218 | |
| 219 | It is possible to display only a portion of the file. |
| 220 | |
| 221 | ``` |
| 222 | \inputpythonfile[numbered, startline=5, endline=10]{example.py} |
| 223 | ``` |
| 224 | |
| 225 | This is useful when showing only the relevant part of a larger script. |
| 226 | |
| 227 | --- |
| 228 | |
| 229 | ## Custom box options |
| 230 | |
| 231 | Box styling can also be customized. |
| 232 | |
| 233 | ``` |
| 234 | \inputpythonfile[title=Included Python code, colframe=Navy, colback=blue!2]{example.py} |
| 235 | ``` |
| 236 | |
| 237 | --- |
| 238 | |
| 239 | # Syntax highlighting |
| 240 | |
| 241 | Syntax highlighting is provided by the `listings` engine using a custom style named `pyjupyter-syntax`. |
| 242 | |
| 243 | The style includes: |
| 244 | |
| 245 | * Python keyword highlighting |
| 246 | * operator highlighting |
| 247 | * colored comments |
| 248 | * colored strings |
| 249 | * support for triple-quoted strings |
| 250 | * automatic line breaking |
| 251 | |
| 252 | --- |
| 253 | |
| 254 | # UTF-8 characters |
| 255 | |
| 256 | The package supports common UTF-8 accented characters inside code listings. |
| 257 | |
| 258 | Example: |
| 259 | |
| 260 | ``` |
| 261 | \begin{jupyter}[] |
| 262 | print("L'étudiant Ouyèté se débrouille en LaTeX !") |
| 263 | \end{jupyter} |
| 264 | ``` |
| 265 | |
| 266 | --- |
| 267 | |
| 268 | # Version history |
| 269 | |
| 270 | **v1.2.1 (2026/04/07)** |
| 271 | Minor update about numbering. It consists in a redesigning of the \texttt{numbered} style. |
| 272 | |
| 273 | **v1.2.0 (2026/03/15)** |
| 274 | Added support for including external Python files. |
| 275 | |
| 276 | * New command `\inputpythonfile` |
| 277 | * Ability to display only selected lines using `startline` and `endline` |
| 278 | * Full support for syntax highlighting, numbering, and custom box options for included files |
| 279 | |
| 280 | **v1.1.0 (2026/03/10)** |
| 281 | Major update with enhanced flexibility. |
| 282 | |
| 283 | * Global font selection |
| 284 | * Improved numbering compatibility |
| 285 | * New `codesize` option for global code size configuration |
| 286 | |
| 287 | **v1.0.0 (2026/03/06)** |
| 288 | Initial release. |
| 289 | |
| 290 | * Jupyter-style code blocks using `tcolorbox` |
| 291 | * Python syntax highlighting via `listings` |
| 292 | * Default integration of Source Code Pro font |
| 293 | |
| 294 | --- |
| 295 | |
| 296 | # Author |
| 297 | |
| 298 | Ouyèté Bright BARA |
| 299 | [[email protected]](mailto:[email protected]) |