1((=- IPython input/output style -=))
2((*- extends 'base.tex.j2' -*))
3
4((*- block packages -*))
5    \usepackage[breakable]{tcolorbox}
6    \usepackage{parskip} % Stop auto-indenting (to mimic markdown behaviour)
7    ((( super() )))
8((*- endblock packages -*))
9
10((*- block definitions -*))
11    ((( super() )))
12% Pygments definitions
13    (((- resources.latex.pygments_definitions )))
14
15    % For linebreaks inside Verbatim environment from package fancyvrb.
16    \makeatletter
17        \newbox\Wrappedcontinuationbox
18        \newbox\Wrappedvisiblespacebox
19        \newcommand*\Wrappedvisiblespace {\textcolor{red}{\textvisiblespace}}
20        \newcommand*\Wrappedcontinuationsymbol {\textcolor{red}{\llap{\tiny$\m@th\hookrightarrow$}}}
21        \newcommand*\Wrappedcontinuationindent {3ex }
22        \newcommand*\Wrappedafterbreak {\kern\Wrappedcontinuationindent\copy\Wrappedcontinuationbox}
23        % Take advantage of the already applied Pygments mark-up to insert
24        % potential linebreaks for TeX processing.
25        %        {, <, #, %, $, ' and ": go to next line.
26        %        _, }, ^, &, >, - and ~: stay at end of broken line.
27        % Use of \textquotesingle for straight quote.
28        \newcommand*\Wrappedbreaksatspecials {%
29            \def\PYGZus{\discretionary{\char`\_}{\Wrappedafterbreak}{\char`\_}}%
30            \def\PYGZob{\discretionary{}{\Wrappedafterbreak\char`\{}{\char`\{}}%
31            \def\PYGZcb{\discretionary{\char`\}}{\Wrappedafterbreak}{\char`\}}}%
32            \def\PYGZca{\discretionary{\char`\^}{\Wrappedafterbreak}{\char`\^}}%
33            \def\PYGZam{\discretionary{\char`\&}{\Wrappedafterbreak}{\char`\&}}%
34            \def\PYGZlt{\discretionary{}{\Wrappedafterbreak\char`\<}{\char`\<}}%
35            \def\PYGZgt{\discretionary{\char`\>}{\Wrappedafterbreak}{\char`\>}}%
36            \def\PYGZsh{\discretionary{}{\Wrappedafterbreak\char`\#}{\char`\#}}%
37            \def\PYGZpc{\discretionary{}{\Wrappedafterbreak\char`\%}{\char`\%}}%
38            \def\PYGZdl{\discretionary{}{\Wrappedafterbreak\char`\$}{\char`\$}}%
39            \def\PYGZhy{\discretionary{\char`\-}{\Wrappedafterbreak}{\char`\-}}%
40            \def\PYGZsq{\discretionary{}{\Wrappedafterbreak\textquotesingle}{\textquotesingle}}%
41            \def\PYGZdq{\discretionary{}{\Wrappedafterbreak\char`\"}{\char`\"}}%
42            \def\PYGZti{\discretionary{\char`\~}{\Wrappedafterbreak}{\char`\~}}%
43        }
44        % Some characters . , ; ? ! / are not pygmentized.
45        % This macro makes them "active" and they will insert potential linebreaks
46        \newcommand*\Wrappedbreaksatpunct {%
47            \lccode`\~`\.\lowercase{\def~}{\discretionary{\hbox{\char`\.}}{\Wrappedafterbreak}{\hbox{\char`\.}}}%
48            \lccode`\~`\,\lowercase{\def~}{\discretionary{\hbox{\char`\,}}{\Wrappedafterbreak}{\hbox{\char`\,}}}%
49            \lccode`\~`\;\lowercase{\def~}{\discretionary{\hbox{\char`\;}}{\Wrappedafterbreak}{\hbox{\char`\;}}}%
50            \lccode`\~`\:\lowercase{\def~}{\discretionary{\hbox{\char`\:}}{\Wrappedafterbreak}{\hbox{\char`\:}}}%
51            \lccode`\~`\?\lowercase{\def~}{\discretionary{\hbox{\char`\?}}{\Wrappedafterbreak}{\hbox{\char`\?}}}%
52            \lccode`\~`\!\lowercase{\def~}{\discretionary{\hbox{\char`\!}}{\Wrappedafterbreak}{\hbox{\char`\!}}}%
53            \lccode`\~`\/\lowercase{\def~}{\discretionary{\hbox{\char`\/}}{\Wrappedafterbreak}{\hbox{\char`\/}}}%
54            \catcode`\.\active
55            \catcode`\,\active
56            \catcode`\;\active
57            \catcode`\:\active
58            \catcode`\?\active
59            \catcode`\!\active
60            \catcode`\/\active
61            \lccode`\~`\~
62        }
63    \makeatother
64
65    \let\OriginalVerbatim=\Verbatim
66    \makeatletter
67    \renewcommand{\Verbatim}[1][1]{%
68        %\parskip\z@skip
69        \sbox\Wrappedcontinuationbox {\Wrappedcontinuationsymbol}%
70        \sbox\Wrappedvisiblespacebox {\FV@SetupFont\Wrappedvisiblespace}%
71        \def\FancyVerbFormatLine ##1{\hsize\linewidth
72            \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
73                \doublehyphendemerits\z@\finalhyphendemerits\z@
74                \strut ##1\strut}%
75        }%
76        % If the linebreak is at a space, the latter will be displayed as visible
77        % space at end of first line, and a continuation symbol starts next line.
78        % Stretch/shrink are however usually zero for typewriter font.
79        \def\FV@Space {%
80            \nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
81            \discretionary{\copy\Wrappedvisiblespacebox}{\Wrappedafterbreak}
82            {\kern\fontdimen2\font}%
83        }%
84
85        % Allow breaks at special characters using \PYG... macros.
86        \Wrappedbreaksatspecials
87        % Breaks at punctuation characters . , ; ? ! and / need catcode=\active
88        \OriginalVerbatim[#1,codes*=\Wrappedbreaksatpunct]%
89    }
90    \makeatother
91
92    % Exact colors from NB
93    ((*- block style_colors *))
94    \definecolor{incolor}{HTML}{303F9F}
95    \definecolor{outcolor}{HTML}{D84315}
96    \definecolor{cellborder}{HTML}{CFCFCF}
97    \definecolor{cellbackground}{HTML}{F7F7F7}
98    ((*- endblock style_colors *))
99
100    % prompt
101    \makeatletter
102    \newcommand{\boxspacing}{\kern\kvtcb@left@rule\kern\kvtcb@boxsep}
103    \makeatother
104    ((*- block style_prompt *))
105    \newcommand{\prompt}[4]{
106        {\ttfamily\llap{{\color{#2}[#3]:\hspace{3pt}#4}}\vspace{-\baselineskip}}
107    }
108    ((* endblock style_prompt *))
109
110((*- endblock definitions -*))
111
112%===============================================================================
113% Input
114%===============================================================================
115
116((* block input scoped *))
117    ((( draw_cell(cell.source | highlight_code(strip_verbatim=True), cell, 'In', 'incolor', '\\boxspacing') )))
118((* endblock input *))
119
120
121%===============================================================================
122% Output
123%===============================================================================
124
125((*- if charlim is not defined -*))
126    ((* set charlim = 80 *))
127((*- endif -*))
128
129((* block execute_result scoped *))
130    ((*- for type in output.data | filter_data_type -*))
131        ((*- if type in ['text/plain']*))
132            ((( draw_cell(output.data['text/plain'] | wrap_text(charlim) | escape_latex | ansi2latex, cell, 'Out', 'outcolor', '\\boxspacing') )))
133        ((* else -*))
134            ((( " " )))
135            ((( draw_prompt(cell, 'Out', 'outcolor','') )))((( super() )))
136        ((*- endif -*))
137    ((*- endfor -*))
138((* endblock execute_result *))
139
140((* block stream *))
141    \begin{Verbatim}[commandchars=\\\{\}]
142((( output.text | wrap_text(charlim) | escape_latex | strip_trailing_newline | ansi2latex )))
143    \end{Verbatim}
144((* endblock stream *))
145
146%==============================================================================
147% Support Macros
148%==============================================================================
149
150% Name: draw_cell
151% Purpose: Renders an output/input prompt
152((*- if draw_cell is not defined -*)) % Required to allow overriding.
153((* macro draw_cell(text, cell, prompt, prompt_color, extra_space) -*))
154((*- if prompt == 'In' -*))
155((*- set style = "breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder"-*))
156((*- else -*))((*- set style = "breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0"-*))((*-  endif -*))
157
158\begin{tcolorbox}[((( style )))]
159(((- draw_prompt(cell, prompt, prompt_color, extra_space) )))
160\begin{Verbatim}[commandchars=\\\{\}]
161((( text )))
162\end{Verbatim}
163\end{tcolorbox}
164((*- endmacro *))
165((*- endif -*))
166
167% Name: draw_prompt
168% Purpose: Renders an output/input prompt
169((* macro draw_prompt(cell, prompt, prompt_color, extra_space) -*))
170    ((*- if cell.execution_count is defined -*))
171    ((*- set execution_count = "" ~ (cell.execution_count | replace(None, " ")) -*))
172    ((*- else -*))((*- set execution_count = " " -*))((*- endif *))
173
174    ((*- if (resources.global_content_filter.include_output_prompt and prompt == 'Out')
175         or (resources.global_content_filter.include_input_prompt  and prompt == 'In' ) *))
176\prompt{(((prompt)))}{(((prompt_color)))}{(((execution_count)))}{(((extra_space)))}
177    ((*- endif -*))
178((*- endmacro *))
179