((= IPython input/output style =))

((*- extends 'base.tplx' -*))

% Custom definitions

((* block packages *))
    %\usepackage{fancybox}
    \usepackage[breakable]{tcolorbox}
    \tcbset{nobeforeafter}
    \usepackage{needspace}
    ((( super() )))
((* endblock packages*))

((* block definitions *))
    ((( super() )))
    % Pygments definitions
    ((( resources.latex.pygments_definitions )))

    % Exact colors from NB
    \definecolor{incolor}{HTML}{303F9F}
    \definecolor{outcolor}{HTML}{D84315}
    \definecolor{grey}{HTML}{CFCFCF}
    \definecolor{light-grey}{HTML}{F7F7F7}
    
    \newenvironment{InVerbatim}{\VerbatimEnvironment%
		\begin{tcolorbox}[breakable, size=fbox, boxrule=.7pt, pad at break*=1mm,
			colback=light-grey,colframe=grey]
			\begin{Verbatim}
			}{%
			\end{Verbatim}%
		\end{tcolorbox}%
	}
	\newenvironment{OutVerbatim}{\VerbatimEnvironment%
		\begin{tcolorbox}[breakable,boxrule=.5pt, size=fbox, pad at break*=1mm, opacityfill=0]
			\begin{Verbatim}
			}{%
			\end{Verbatim}%
		\end{tcolorbox}%
	}
    
    \newlength{\promptlength}
((* endblock definitions *))

%===============================================================================
% Input
%===============================================================================

((* block input scoped *))
    ((( draw_cell(cell.source | highlight_code(strip_verbatim=True), cell, 'In', 'incolor') )))
((* endblock input *))


%===============================================================================
% Output
%===============================================================================

((* block execute_result scoped *))
    ((*- for type in output.data | filter_data_type -*))
        ((*- if type in ['text/plain']*))
            ((( draw_cell(output.data['text/plain'] | escape_latex, cell, 'Out', 'outcolor') )))
        ((* else -*))
            ((( " " )))
            ((( draw_prompt(cell, 'Out', 'outcolor') )))((( super() )))
        ((*- endif -*))
    ((*- endfor -*))
((* endblock execute_result *))


%==============================================================================
% Support Macros
%==============================================================================

% Name: draw_cell
% Purpose: Renders an output/input prompt
((* macro draw_cell(text, cell, prompt, prompt_color) -*))
((( draw_prompt(cell, prompt, prompt_color) )))
\begin{((( prompt ~ 'Verbatim')))}[commandchars=\\\{\}]
((( text )))
\end{((( prompt ~ 'Verbatim')))}
((*- endmacro *))

% Name: draw_prompt
% Purpose: Renders an output/input prompt
((* macro draw_prompt(cell, prompt, prompt_color) -*))
    ((*- if cell.execution_count is defined -*))
    ((*- set execution_count = "" ~ (cell.execution_count | replace(None, " ")) -*))
    ((*- else -*))
    ((*- set execution_count = " " -*))
    ((*- endif -*))
\needspace{1.1cm}
\settowidth{\promptlength}{ ((( prompt ~ ' [' ~ execution_count ~ ']' ))) }
\hspace{-\promptlength}\hspace{-5pt}
((('{\color{' ~ prompt_color ~ '}' ~ prompt ~ ' [{\\color{' ~ prompt_color ~ '}' ~ execution_count ~ '}]:}' )))
\vspace{-2.7ex}
((*- endmacro *))
