-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_sagetex.sty
408 lines (398 loc) · 12.1 KB
/
my_sagetex.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{sagetex}
[2021/08/20 v3.6a alternative version supporting notebook]
\newcommand{\ST@ver}{2021/08/20 v3.6a}
\RequirePackage{verbatim}
\RequirePackage{fancyvrb}
\RequirePackage{listings}
\RequirePackage{color}
\lstdefinelanguage{Sage}[]{Python}
{morekeywords={False,sage,True},sensitive=true}
\lstdefinelanguage{SageOutput}[]{}
{morekeywords={False,True},sensitive=true}
\lstdefinestyle{DefaultSageInputOutput}{
nolol,
identifierstyle=,
name=sagecommandline,
xleftmargin=5pt,
numbersep=5pt,
aboveskip=0pt,
belowskip=0pt,
breaklines=true,
numberstyle=\footnotesize,
numbers=right
}
\lstdefinestyle{DefaultSageInput}{
language=Sage,
style=DefaultSageInputOutput,
basicstyle={\ttfamily\bfseries},
commentstyle={\ttfamily\color{dgreencolor}},
keywordstyle={\ttfamily\color{dbluecolor}\bfseries},
stringstyle={\ttfamily\color{dgraycolor}\bfseries},
}
\lstdefinestyle{DefaultSageOutput}{
language=SageOutput,
style=DefaultSageInputOutput,
basicstyle={\ttfamily},
commentstyle={\ttfamily\color{dgreencolor}},
keywordstyle={\ttfamily\color{dbluecolor}},
stringstyle={\ttfamily\color{dgraycolor}},
}
\lstdefinestyle{SageInput}{
style=DefaultSageInput,
}
\lstdefinestyle{SageOutput}{
style=DefaultSageOutput,
}
%% MY CHANGES
\RequirePackage[many,breakable,listings,minted]{tcolorbox}
\newcommand{\prompt}[3]{%
\llap{\texttt{\color{#1}[#2]:\:}}%
}
% Exact colors from NB
\definecolor{incolor}{HTML}{234c66}
\definecolor{outcolor}{HTML}{b73e2c}
\definecolor{cellborder}{HTML}{CFCFCF}
\definecolor{cellbackground}{HTML}{F7F7F7}
% Python highlighting colors
\definecolor{keywordcol}{HTML}{3a821d}
\definecolor{stringcol}{HTML}{a03626}
\definecolor{commentcol}{HTML}{477998}
\newcounter{NBin}
\newtcbinputlisting{\NBinput}[4]{
breakable,
enhanced,
minted language=python,
size=fbox,
boxrule=1pt,
pad at break*=1mm,
before skip=\medskipamount,
colback=cellbackground,
colframe=cellborder,
phantom=\refstepcounter{NBin}\label{in:\theNBin},
detach title,
overlay unbroken and first={%
\node[anchor=north] at (frame.north west) {\tcbtitle};
},
title=\prompt{incolor}{\theNBin},
listing file={#1},
listing only,
minted options app={
firstline=#2,
lastline=#3,
#4,
},
}
\newtcbinputlisting{\NBoutput}[4]{
breakable,
enhanced,
minted language=text,
size=fbox,
opacityfill=0,
pad at break*=1mm,
phantom=\label{out:\theNBin},
after={\noindent\par},
detach title,
overlay unbroken and first={%
\node[anchor=north] at (frame.north west) {\tcbtitle};
},
title=\prompt{outcolor}{\theNBin},
listing file={#1},
listing only,
minted options app={
firstline=#2,
lastline=#3,
#4,
},
}
\newtcolorbox{NBoutputM}{
breakable,
enhanced,
size=fbox,
opacityfill=0,
pad at break*=1mm,
phantom=\label{out:\theNBin},
detach title,
overlay unbroken and first={%
\node[anchor=north] at (frame.north west) {\tcbtitle};
},
title=\prompt{outcolor}{\theNBin},
}
\definecolor{dbluecolor}{rgb}{0.01,0.02,0.7}
\definecolor{dgreencolor}{rgb}{0.2,0.4,0.0}
\definecolor{dgraycolor}{rgb}{0.30,0.3,0.30}
\RequirePackage{graphicx}
\RequirePackage{makecmds}
\RequirePackage{ifpdf}
\RequirePackage{ifthen}
\IfFileExists{ifxetex.sty}{
\RequirePackage{ifxetex}
}{
\newboolean{xetex}
\setboolean{xetex}{false}}
\newcounter{ST@inline}
\newcounter{ST@plot}
\newcounter{ST@cmdline}
\setcounter{ST@inline}{0}
\setcounter{ST@plot}{0}
\setcounter{ST@cmdline}{0}
\newcounter{ST@notebook}
\setcounter{ST@notebook}{0}
\newlength{\sagetexindent}
\setlength{\sagetexindent}{5ex}
\newif\ifST@paused
\ST@pausedfalse
\AtBeginDocument{\@ifundefined{ST@final}{%
\newwrite\ST@sf%
\immediate\openout\ST@sf=\jobname.sagetex.sage%
\newcommand{\ST@wsf}[1]{\immediate\write\ST@sf{#1}}%
\ST@wsf{%
# -*- encoding: utf-8 -*-^^J%
# This file (\jobname.sagetex.sage) was *autogenerated* from \jobname.tex with
sagetex.sty version \ST@ver.^^J%
import importlib.util^^J%
spec = importlib.util.spec_from_file_location("", "./my_sagetex.py")^^J%
sagetex = importlib.util.module_from_spec(spec)^^J%
spec.loader.exec_module(sagetex)^^J%
_st_ = sagetex.SageTeXProcessor('\jobname', version='\ST@ver', version_check=\ST@versioncheck)}}%
{\newcommand{\ST@wsf}[1]{\relax}}}
\newcommand{\ST@dodfsetup}{%
\@ifundefined{ST@diddfsetup}{%
\newwrite\ST@df%
\immediate\openout\ST@df=\jobname_doctest.sage%
\immediate\write\ST@df{r"""^^J%
This file was *autogenerated* from \jobname.tex with sagetex.sty^^J%
version \ST@ver. It contains the contents of all the^^J%
sageexample environments from \jobname.tex. You should be able to^^J%
doctest this file with "sage -t \jobname_doctest.sage".^^J%
^^J%
It is always safe to delete this file; it is not used in typesetting your^^J%
document.^^J}%
\AtEndDocument{\immediate\write\ST@df{"""}}%
\gdef\ST@diddfsetup{x}}%
{\relax}}
\newcommand{\ST@wdf}[1]{\immediate\write\ST@df{#1}}
\DeclareOption{final}{%
\newcommand{\ST@final}{x}%
\IfFileExists{\jobname.sagetex.sout}{}{\AtEndDocument{\PackageWarningNoLine{sagetex}%
{`final' option provided, but \jobname.sagetex.sout^^Jdoesn't exist! No Sage
input will appear in your document. Remove the `final'^^Joption and
rerun LaTeX on your document}}}}
\DeclareOption{imagemagick}{%
\newcommand{\ST@useimagemagick}{x}%
\AtBeginDocument{%
\@ifundefined{ST@final}{%
\ST@wsf{_st_.useimagemagick = True}}{}}}
\DeclareOption{epstopdf}{%
\AtBeginDocument{%
\@ifundefined{ST@final}{%
\ST@wsf{_st_.useepstopdf = True}}{}}}
\newcommand{\ST@versioncheck}{True}
\DeclareOption{noversioncheck}{%
\renewcommand{\ST@versioncheck}{False}}
\ProcessOptions\relax
\InputIfFileExists{\jobname.sagetex.sout}{}
{\typeout{No file \jobname.sagetex.sout.}}
\AtBeginDocument{\provideenvironment{NoHyper}{}{}}
\newcommand{\ST@sage}[1]{\ST@wsf{%
try:^^J
_st_.current_tex_line = \the\inputlineno^^J
_st_.inline(\theST@inline, #1)^^J%
except:^^J
_st_.goboom(\the\inputlineno)}%
\ifST@paused
\mbox{(Sage\TeX{} is paused)}%
\else
\begin{NoHyper}\ref{@sageinline\theST@inline}\end{NoHyper}%
\@ifundefined{r@@sageinline\theST@inline}{\gdef\ST@rerun{x}}{}%
\fi
\stepcounter{ST@inline}}
\newcommand{\sage}[1]{\ST@sage{latex(#1)}}
\newcommand{\sagestr}[1]{\ST@sage{#1}}
\catcode`\%=12
\newcommand{\percent}{%}
\catcode`\%=14
\newcommand{\ST@plotdir}{sage-plots-for-\jobname.tex}
\newcommand{\ST@missingfilebox}{\framebox[2cm]{\rule[-1cm]{0cm}{2cm}\textbf{??}}}
\newcommand{\sageplot}[1][]{%
\@ifnextchar[{\ST@sageplot[#1]}{\ST@sageplot[#1][notprovided]}}
\def\ST@sageplot[#1][#2]#3{\ST@wsf{try:^^J
_st_.current_tex_line = \the\inputlineno^^J
_st_.plot(\theST@plot, format='#2', _p_=#3)^^Jexcept:^^J
_st_.goboom(\the\inputlineno)}%
\ifthenelse{\boolean{pdf} \or \boolean{xetex}}{
\ifthenelse{\equal{#2}{notprovided}}%
{\ST@inclgrfx{#1}{pdf}}%
{\ST@inclgrfx{#1}{#2}}}
{ \ifthenelse{\equal{#2}{notprovided}}%
{\ST@inclgrfx{#1}{eps}}%
{\@ifundefined{ST@useimagemagick}%
{\IfFileExists{\ST@plotdir/plot-\theST@plot.#2}%
{\ST@missingfilebox%
\PackageWarning{sagetex}{Graphics file
\ST@plotdir/plot-\theST@plot.#2\space on page \thepage\space
cannot be used with DVI output. Use pdflatex or create an EPS
file. Plot command is}}%
{\ST@missingfilebox%
\PackageWarning{sagetex}{Graphics file
\ST@plotdir/plot-\theST@plot.#2\space on page \thepage\space
does not exist. Plot command is}%
\gdef\ST@rerun{x}}}%
{\ST@inclgrfx{#1}{eps}}}}
\stepcounter{ST@plot}}
\newcommand{\ST@inclgrfx}[2]{\ifST@paused
\fbox{\rule[-1cm]{0cm}{2cm}Sage\TeX{} is paused; no graphic}
\else
\IfFileExists{\ST@plotdir/plot-\theST@plot.#2}%
{\includegraphics[#1]{\ST@plotdir/plot-\theST@plot.#2}}%
{\IfFileExists{\ST@plotdir/plot-\theST@plot.png}%
{\ifpdf
\ST@inclgrfx{#1}{png}
\else
\PackageWarning{sagetex}{Graphics file
\ST@plotdir/plot-\theST@plot.png on page \thepage\space not
supported; try using pdflatex. Plot command is}%
\fi}%
{\ST@missingfilebox%
\PackageWarning{sagetex}{Graphics file
\ST@plotdir/plot-\theST@plot.#2\space on page \thepage\space does not
exist. Plot command is}%
\gdef\ST@rerun{x}}}
\fi}
\newcommand{\ST@beginsfbl}{%
\@bsphack\ST@wsf{%
_st_.current_tex_line = \the\inputlineno^^J%
_st_.blockbegin()^^Jtry:}%
\let\do\@makeother\dospecials\catcode`\^^M\active}
\newcommand{\ST@endsfbl}{%
\ST@wsf{except:^^J
_st_.goboom(\the\inputlineno)^^J_st_.blockend()}}
\newenvironment{sageblock}{\ST@beginsfbl%
\def\verbatim@processline{\ST@wsf{ \the\verbatim@line}%
\hspace{\sagetexindent}\the\verbatim@line\par}%
\verbatim}%
{\ST@endsfbl\endverbatim}
\newenvironment{sagesilent}{\ST@beginsfbl%
\def\verbatim@processline{\ST@wsf{ \the\verbatim@line}}%
\verbatim@start}%
{\ST@endsfbl\@esphack}
\newenvironment{sageverbatim}{%
\def\verbatim@processline{\hspace{\sagetexindent}\the\verbatim@line\par}%
\verbatim}%
{\endverbatim}
\newcommand{\sageexampleincludetextoutput}{False}
\newenvironment{sageexample}{%
\ST@wsf{%
try:^^J
_st_.current_tex_line = \the\inputlineno^^J
_st_.doctest(\theST@inline, r"""}%
\ST@dodfsetup%
\ST@wdf{Sage example, line \the\inputlineno::^^J}%
\begingroup%
\@bsphack%
\let\do\@makeother\dospecials%
\catcode`\^^M\active%
\def\verbatim@processline{%
\ST@wsf{\the\verbatim@line}%
\ST@wdf{\the\verbatim@line}%
}%
\verbatim@start%
}
{
\@esphack%
\endgroup%
\ST@wsf{%
""", globals(), locals(), \sageexampleincludetextoutput)^^Jexcept:^^J
_st_.goboom(\the\inputlineno)}%
\ifST@paused%
\mbox{(Sage\TeX{} is paused)}%
\else%
\begin{NoHyper}\ref{@sageinline\theST@inline}\end{NoHyper}%
\@ifundefined{r@@sageinline\theST@inline}{\gdef\ST@rerun{x}}{}%
\fi%
\ST@wdf{}%
\stepcounter{ST@inline}}
\newcommand{\sagecommandlinetextoutput}{True}
\newlength{\sagecommandlineskip}
\setlength{\sagecommandlineskip}{8pt}
\newenvironment{sagecommandline}{%
\ST@wsf{%
try:^^J
_st_.current_tex_line = \the\inputlineno^^J
_st_.commandline(\theST@cmdline, r"""}%
\ST@dodfsetup%
\ST@wdf{Sage commandline, line \the\inputlineno::^^J}%
\begingroup%
\@bsphack%
\let\do\@makeother\dospecials%
\catcode`\^^M\active%
\def\verbatim@processline{%
\ST@wsf{\the\verbatim@line}%
\ST@wdf{\the\verbatim@line}%
}%
\verbatim@start%
}
{
\@esphack%
\endgroup%
\ST@wsf{%
""", globals(), locals(), \sagecommandlinetextoutput)^^Jexcept:^^J
_st_.goboom(\the\inputlineno)}%
\ifST@paused%
\mbox{(Sage\TeX{} is paused)}%
\else%
\begin{NoHyper}\ref{@sagecmdline\theST@cmdline}\end{NoHyper}%
\@ifundefined{r@@sagecmdline\theST@cmdline}{\gdef\ST@rerun{x}}{}%
\fi%
\ST@wdf{}%
\stepcounter{ST@cmdline}}
\newenvironment{sagenotebook}{%
\ST@wsf{%
try:^^J
_st_.current_tex_line = \the\inputlineno^^J
_st_.notebook(\theST@notebook, r"""}%
\ST@dodfsetup%
\ST@wdf{Sage commandline, line \the\inputlineno::^^J}%
\begingroup%
\@bsphack%
\let\do\@makeother\dospecials%
\catcode`\^^M\active%
\def\verbatim@processline{%
\ST@wsf{\the\verbatim@line}%
\ST@wdf{\the\verbatim@line}%
}%
\verbatim@start%
}
{
\@esphack%
\endgroup%
\ST@wsf{%
""", globals(), locals(), \sagecommandlinetextoutput)^^Jexcept:^^J
_st_.goboom(\the\inputlineno)}%
\ifST@paused%
\mbox{(Sage\TeX{} is paused)}%
\else%
\begin{NoHyper}\ref{@sagenotebook\theST@notebook}\end{NoHyper}%
\@ifundefined{r@@sagenotebook\theST@notebook}{\gdef\ST@rerun{x}}{}%
\fi%
\ST@wdf{}%
\stepcounter{ST@notebook}}
\newcommand{\sagetexpause}{\ifST@paused\relax\else
\ST@wsf{print('SageTeX paused on \jobname.tex line \the\inputlineno')^^J"""}
\ST@pausedtrue
\fi}
\newcommand{\sagetexunpause}{\ifST@paused
\ST@wsf{"""^^Jprint('SageTeX unpaused on \jobname.tex line \the\inputlineno')}
\ST@pausedfalse
\fi}
\AtEndDocument{\ifST@paused
\ST@wsf{"""^^Jprint('SageTeX unpaused at end of \jobname.tex')}
\fi
\ST@wsf{_st_.endofdoc()}%
\@ifundefined{ST@rerun}{}%
{\typeout{*********************************************************************}
\PackageWarningNoLine{sagetex}{there were undefined Sage formulas and/or
plots.^^JRun Sage on \jobname.sagetex.sage, and then run LaTeX on \jobname.tex
again}}
\typeout{*********************************************************************}}
\endinput