-
Notifications
You must be signed in to change notification settings - Fork 567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a notebook cell style to latex conversion #992
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
((=- IPython input/output style -=)) | ||
((*- extends 'base.tplx' -*)) | ||
|
||
((*- block packages -*)) | ||
\usepackage[breakable]{tcolorbox} | ||
\tcbset{nobeforeafter} % prevents tcolorboxes being placing in paragraphs | ||
\usepackage{float} | ||
\floatplacement{figure}{H} % forces figures to be placed at the correct location | ||
((( super() ))) | ||
((*- endblock packages -*)) | ||
|
||
((*- block definitions -*)) | ||
((( super() ))) | ||
% Pygments definitions | ||
(((- resources.latex.pygments_definitions ))) | ||
|
||
% For linebreaks inside Verbatim environment from package fancyvrb. | ||
\makeatletter | ||
\newbox\Wrappedcontinuationbox | ||
\newbox\Wrappedvisiblespacebox | ||
\newcommand*\Wrappedvisiblespace {\textcolor{red}{\textvisiblespace}} | ||
\newcommand*\Wrappedcontinuationsymbol {\textcolor{red}{\llap{\tiny$\m@th\hookrightarrow$}}} | ||
\newcommand*\Wrappedcontinuationindent {3ex } | ||
\newcommand*\Wrappedafterbreak {\kern\Wrappedcontinuationindent\copy\Wrappedcontinuationbox} | ||
% Take advantage of the already applied Pygments mark-up to insert | ||
% potential linebreaks for TeX processing. | ||
% {, <, #, %, $, ' and ": go to next line. | ||
% _, }, ^, &, >, - and ~: stay at end of broken line. | ||
% Use of \textquotesingle for straight quote. | ||
\newcommand*\Wrappedbreaksatspecials {% | ||
\def\PYGZus{\discretionary{\char`\_}{\Wrappedafterbreak}{\char`\_}}% | ||
\def\PYGZob{\discretionary{}{\Wrappedafterbreak\char`\{}{\char`\{}}% | ||
\def\PYGZcb{\discretionary{\char`\}}{\Wrappedafterbreak}{\char`\}}}% | ||
\def\PYGZca{\discretionary{\char`\^}{\Wrappedafterbreak}{\char`\^}}% | ||
\def\PYGZam{\discretionary{\char`\&}{\Wrappedafterbreak}{\char`\&}}% | ||
\def\PYGZlt{\discretionary{}{\Wrappedafterbreak\char`\<}{\char`\<}}% | ||
\def\PYGZgt{\discretionary{\char`\>}{\Wrappedafterbreak}{\char`\>}}% | ||
\def\PYGZsh{\discretionary{}{\Wrappedafterbreak\char`\#}{\char`\#}}% | ||
\def\PYGZpc{\discretionary{}{\Wrappedafterbreak\char`\%}{\char`\%}}% | ||
\def\PYGZdl{\discretionary{}{\Wrappedafterbreak\char`\$}{\char`\$}}% | ||
\def\PYGZhy{\discretionary{\char`\-}{\Wrappedafterbreak}{\char`\-}}% | ||
\def\PYGZsq{\discretionary{}{\Wrappedafterbreak\textquotesingle}{\textquotesingle}}% | ||
\def\PYGZdq{\discretionary{}{\Wrappedafterbreak\char`\"}{\char`\"}}% | ||
\def\PYGZti{\discretionary{\char`\~}{\Wrappedafterbreak}{\char`\~}}% | ||
} | ||
% Some characters . , ; ? ! / are not pygmentized. | ||
% This macro makes them "active" and they will insert potential linebreaks | ||
\newcommand*\Wrappedbreaksatpunct {% | ||
\lccode`\~`\.\lowercase{\def~}{\discretionary{\hbox{\char`\.}}{\Wrappedafterbreak}{\hbox{\char`\.}}}% | ||
\lccode`\~`\,\lowercase{\def~}{\discretionary{\hbox{\char`\,}}{\Wrappedafterbreak}{\hbox{\char`\,}}}% | ||
\lccode`\~`\;\lowercase{\def~}{\discretionary{\hbox{\char`\;}}{\Wrappedafterbreak}{\hbox{\char`\;}}}% | ||
\lccode`\~`\:\lowercase{\def~}{\discretionary{\hbox{\char`\:}}{\Wrappedafterbreak}{\hbox{\char`\:}}}% | ||
\lccode`\~`\?\lowercase{\def~}{\discretionary{\hbox{\char`\?}}{\Wrappedafterbreak}{\hbox{\char`\?}}}% | ||
\lccode`\~`\!\lowercase{\def~}{\discretionary{\hbox{\char`\!}}{\Wrappedafterbreak}{\hbox{\char`\!}}}% | ||
\lccode`\~`\/\lowercase{\def~}{\discretionary{\hbox{\char`\/}}{\Wrappedafterbreak}{\hbox{\char`\/}}}% | ||
\catcode`\.\active | ||
\catcode`\,\active | ||
\catcode`\;\active | ||
\catcode`\:\active | ||
\catcode`\?\active | ||
\catcode`\!\active | ||
\catcode`\/\active | ||
\lccode`\~`\~ | ||
} | ||
\makeatother | ||
|
||
\let\OriginalVerbatim=\Verbatim | ||
\makeatletter | ||
\renewcommand{\Verbatim}[1][1]{% | ||
%\parskip\z@skip | ||
\sbox\Wrappedcontinuationbox {\Wrappedcontinuationsymbol}% | ||
\sbox\Wrappedvisiblespacebox {\FV@SetupFont\Wrappedvisiblespace}% | ||
\def\FancyVerbFormatLine ##1{\hsize\linewidth | ||
\vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@ | ||
\doublehyphendemerits\z@\finalhyphendemerits\z@ | ||
\strut ##1\strut}% | ||
}% | ||
% If the linebreak is at a space, the latter will be displayed as visible | ||
% space at end of first line, and a continuation symbol starts next line. | ||
% Stretch/shrink are however usually zero for typewriter font. | ||
\def\FV@Space {% | ||
\nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font | ||
\discretionary{\copy\Wrappedvisiblespacebox}{\Wrappedafterbreak} | ||
{\kern\fontdimen2\font}% | ||
}% | ||
|
||
% Allow breaks at special characters using \PYG... macros. | ||
\Wrappedbreaksatspecials | ||
% Breaks at punctuation characters . , ; ? ! and / need catcode=\active | ||
\OriginalVerbatim[#1,codes*=\Wrappedbreaksatpunct]% | ||
} | ||
\makeatother | ||
|
||
% Exact colors from NB | ||
((*- block style_colors *)) | ||
\definecolor{incolor}{HTML}{303F9F} | ||
\definecolor{outcolor}{HTML}{D84315} | ||
\definecolor{cellborder}{HTML}{CFCFCF} | ||
\definecolor{cellbackground}{HTML}{F7F7F7} | ||
((*- endblock style_colors *)) | ||
|
||
% prompt | ||
((*- block style_prompt *)) | ||
\newcommand{\prompt}[4]{ | ||
\llap{{\color{#2}[#3]: #4}}\vspace{-1.25em} | ||
} | ||
((* endblock style_prompt *)) | ||
|
||
((*- endblock definitions -*)) | ||
|
||
%=============================================================================== | ||
% Input | ||
%=============================================================================== | ||
|
||
((* block input scoped *)) | ||
((( draw_cell(cell.source | highlight_code(strip_verbatim=True), cell, 'In', 'incolor', '\\hspace{4pt}') ))) | ||
((* endblock input *)) | ||
|
||
|
||
%=============================================================================== | ||
% Output | ||
%=============================================================================== | ||
|
||
((*- if charlim is not defined -*)) | ||
((* set charlim = 80 *)) | ||
((*- endif -*)) | ||
|
||
((* block execute_result scoped *)) | ||
((*- for type in output.data | filter_data_type -*)) | ||
((*- if type in ['text/plain']*)) | ||
((( draw_cell(output.data['text/plain'] | wrap_text(charlim) | escape_latex, cell, 'Out', 'outcolor', '\\hspace{3.5pt}') ))) | ||
((* else -*)) | ||
((( " " ))) | ||
((( draw_prompt(cell, 'Out', 'outcolor','') )))((( super() ))) | ||
((*- endif -*)) | ||
((*- endfor -*)) | ||
((* endblock execute_result *)) | ||
|
||
((* block stream *)) | ||
\begin{Verbatim}[commandchars=\\\{\}] | ||
((( output.text | wrap_text(charlim) | escape_latex | ansi2latex -))) | ||
\end{Verbatim} | ||
((* endblock stream *)) | ||
|
||
%============================================================================== | ||
% Support Macros | ||
%============================================================================== | ||
|
||
% Name: draw_cell | ||
% Purpose: Renders an output/input prompt | ||
((*- if draw_cell is not defined -*)) % Required to allow overriding. | ||
((* macro draw_cell(text, cell, prompt, prompt_color, extra_space) -*)) | ||
((*- if prompt == 'In' -*)) | ||
((*- set style = "breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder"-*)) | ||
((*- else -*))((*- set style = "breakable, boxrule=.5pt, size=fbox, pad at break*=1mm, opacityfill=0"-*))((*- endif -*)) | ||
|
||
\begin{tcolorbox}[((( style )))] | ||
(((- draw_prompt(cell, prompt, prompt_color, extra_space) ))) | ||
\begin{Verbatim}[commandchars=\\\{\}] | ||
((( text ))) | ||
\end{Verbatim} | ||
\end{tcolorbox} | ||
((*- endmacro *)) | ||
((*- endif -*)) | ||
|
||
% Name: draw_prompt | ||
% Purpose: Renders an output/input prompt | ||
((* macro draw_prompt(cell, prompt, prompt_color, extra_space) -*)) | ||
((*- if cell.execution_count is defined -*)) | ||
((*- set execution_count = "" ~ (cell.execution_count | replace(None, " ")) -*)) | ||
((*- else -*))((*- set execution_count = " " -*))((*- endif *)) | ||
|
||
((*- if (resources.global_content_filter.include_output_prompt and prompt == 'Out') | ||
or (resources.global_content_filter.include_input_prompt and prompt == 'In' ) *)) | ||
\prompt{(((prompt)))}{(((prompt_color)))}{(((execution_count)))}{(((extra_space)))} | ||
((*- endif -*)) | ||
((*- endmacro *)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thought is that might be too greedy though with the
-
since that consumes whitespace that would previously have been added by theset cell_style
. I think in this case it makes sense, but we should be careful whenever adding or subtracting-
to our delimiters.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latex comment has been annoying me for a long time. There are a few other places that contribute to an unnecessary amount of whitespace at the top of the generated latex file that also annoy me when working with the latex file (not that it really matters), but I figured that I would start with this one while I was in that file.