Showing
3 changed files
with
165 additions
and
0 deletions
pytex.py
0 → 100755
This diff is collapsed. Click to expand it.
pytex.sty
0 → 100644
1 | +\ProvidesPackage{pytex} | ||
2 | + | ||
3 | +\RequirePackage{pgfopts} | ||
4 | +\RequirePackage{sverb} | ||
5 | +\RequirePackage{fancyvrb} | ||
6 | + | ||
7 | +\makeatletter | ||
8 | + | ||
9 | +%% | ||
10 | +%% | ||
11 | + | ||
12 | +\edef\py@port{12345} | ||
13 | +\edef\py@base{\jobname.pytmp} | ||
14 | + | ||
15 | +\pgfkeys{ | ||
16 | + /pytex/.cd, | ||
17 | + base/.store in=\py@base, | ||
18 | + port/.store in=\py@port, | ||
19 | +} | ||
20 | + | ||
21 | +\ProcessPgfOptions{/pytex} | ||
22 | + | ||
23 | +\immediate\write18{pytex -b {\py@base} --listen {\py@port} {\jobname}} | ||
24 | +\AtEndDocument{\immediate\write18{pytex -b {\py@base} --quit {\py@port}}} | ||
25 | + | ||
26 | +\IfFileExists{\py@base/\jobname} | ||
27 | + {\global\edef\py@base{\py@base/}} | ||
28 | + {\global\edef\py@base{\py@base.}} | ||
29 | + | ||
30 | +%% | ||
31 | +%% | ||
32 | + | ||
33 | +\newcounter{py@count} | ||
34 | +\newwrite\py@file | ||
35 | +\newwrite\py@jobs | ||
36 | + | ||
37 | +\immediate\openout\py@jobs=\jobname.pytex | ||
38 | + | ||
39 | +\def\py@basename{\py@base\thepy@count} | ||
40 | + | ||
41 | +\def\py@next #1{% | ||
42 | + \edef\py@last{\py@basename}% | ||
43 | + \addtocounter{py@count}{1}% | ||
44 | + \edef\py@basename{\py@base\thepy@count}% | ||
45 | + \edef\py@src{\py@basename.py}% | ||
46 | + \edef\py@@src{{\py@src}}% | ||
47 | + \edef\py@out{{\py@basename.out}}% | ||
48 | + \edef\py@tex{{\py@basename.tex}}% | ||
49 | + \immediate\write\py@jobs{#1 \py@src}% | ||
50 | +} | ||
51 | + | ||
52 | +\def\py@open{\immediate\openout\py@file=\py@src} | ||
53 | +\def\py@write{\immediate\write\py@file} | ||
54 | +\def\py@close{\immediate\closeout\py@file} | ||
55 | +\def\py@compile #1{\immediate\write18{pytex -b {\py@base} --call {\py@port} #1 {\py@src}}} | ||
56 | +\def\py@input #1{\expandafter\expandafter\expandafter | ||
57 | + \InputIfFileExists\csname py@#1\endcsname{}{}} | ||
58 | + | ||
59 | +\def\pyv #1{% | ||
60 | + \py@next{eval}% | ||
61 | + \py@open | ||
62 | + \py@write{#1}% | ||
63 | + \py@close | ||
64 | + \py@compile{eval}% | ||
65 | + \py@input{out}% | ||
66 | +} | ||
67 | + | ||
68 | +\def\pyx #1{% | ||
69 | + \py@next{exec}% | ||
70 | + \py@open | ||
71 | + \py@write{#1}% | ||
72 | + \py@close | ||
73 | + \py@compile{exec}% | ||
74 | + \py@input{out}% | ||
75 | +} | ||
76 | + | ||
77 | +\def\pyc #1{\leavevmode | ||
78 | + \py@next{code}% | ||
79 | + \py@open | ||
80 | + \py@write{#1}% | ||
81 | + \py@close | ||
82 | + \pyx{pygmentize("\py@last.py", inline=True)}% | ||
83 | +} | ||
84 | + | ||
85 | +\def\pyd #1#2{% | ||
86 | + \py@next{eval}% | ||
87 | + \py@open | ||
88 | + \py@write{#2}% | ||
89 | + \py@close | ||
90 | + \py@compile{eval}% | ||
91 | + \pyx{makedef("#1", "\py@last")}% | ||
92 | +} | ||
93 | + | ||
94 | +\newenvironment{pyeval}{% | ||
95 | + \py@next{eval}% | ||
96 | + \expandafter\verbwrite\py@@src | ||
97 | +}{\endverbwrite | ||
98 | + \py@compile{eval}% | ||
99 | + \py@input{out}% | ||
100 | +} | ||
101 | + | ||
102 | +\newenvironment{pyexec}{% | ||
103 | + \py@next{exec}% | ||
104 | + \expandafter\verbwrite\py@@src | ||
105 | +}{\endverbwrite | ||
106 | + \py@compile{exec}% | ||
107 | + \py@input{out}% | ||
108 | +} | ||
109 | + | ||
110 | +\newenvironment{pycode}{% | ||
111 | + \py@next{exec}% | ||
112 | + \expandafter\verbwrite\py@@src | ||
113 | +}{\endverbwrite | ||
114 | + \pyx{pygmentize("\py@last.py", inline=False)} | ||
115 | +} | ||
116 | + | ||
117 | +%% | ||
118 | +%% | ||
119 | + | ||
120 | +\makeatother | ||
121 | + | ||
122 | +\pyx{reset()} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
test.tex
0 → 100644
1 | +\documentclass{article} | ||
2 | + | ||
3 | +\usepackage{pytex} | ||
4 | +\parindent=0pt | ||
5 | + | ||
6 | +\begin{document} | ||
7 | + | ||
8 | +***\pyv{" ".join(s.capitalize() for s in "hello world!".split())}*** | ||
9 | + | ||
10 | +*\pyx{x=40}**\pyv{x+2}*** | ||
11 | + | ||
12 | +***\pyd{hello}{x+2}***\hello*** | ||
13 | + | ||
14 | +\begin{pyexec} | ||
15 | +def fact (n) : | ||
16 | + f = 1 | ||
17 | + for i in range(1, n+1) : | ||
18 | + f *= i | ||
19 | + return f | ||
20 | +\end{pyexec} | ||
21 | + | ||
22 | +\begin{pyeval} | ||
23 | +[fact(n) for n in range(10) | ||
24 | + if n % 2 == 0] | ||
25 | +\end{pyeval} | ||
26 | + | ||
27 | +********************** | ||
28 | + | ||
29 | +Code ``\pyc{lambda x : x + 1}'' is inlined. | ||
30 | + | ||
31 | +Code block: | ||
32 | +% | ||
33 | +\begin{pycode} | ||
34 | +def fact (n) : | ||
35 | + f = 1 | ||
36 | + for i in range(1, n+1) : | ||
37 | + f *= i | ||
38 | + return f | ||
39 | +\end{pycode} | ||
40 | + | ||
41 | +\IfFileExists{pytex.tmp}{found}{NO}. | ||
42 | + | ||
43 | +\end{document} |
-
Please register or login to post a comment