style_jupyter.tplx 8.1 KB

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