Skip to content

Commit c60f08e

Browse files
committed
Minor tweaks. Fixed error in change log and updated status and acknowledgements. Modified pythontex*.py to better support upgrading. Added pythontex_2to3.py conversion script. Added Pygments example to gallery.
1 parent acc4adf commit c60f08e

8 files changed

Lines changed: 130 additions & 10 deletions

File tree

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ v0.9beta3 (2012/07/17)
4242
all code after updating to the new version. Otherwise, you should delete
4343
the PythonTeX directory and run PythonTeX.
4444
* Improved installation script.
45-
* Added package options: pyfuture, stderr, workingdir, upquote, pyglexer,
46-
pyginline. Renamed the pygextfile option to fvextfile.
47-
* Added custom code command.
45+
* Added package options: pyfuture, stderr, upquote, pyglexer, pyginline.
46+
Renamed the pygextfile option to fvextfile.
47+
* Added custom code and workingdir commands.
4848
* Added the console environment and associated options.
4949
* Rewrote pythontex_utils*.py, creating a new, context-aware interface to
5050
SymPy's LatexPrinter class.

pythontex/pythontex.dtx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
%
3232
%<*driver>
3333
\documentclass{ltxdoc}
34+
\usepackage[T1]{fontenc}
35+
\usepackage[utf8]{inputenc}
3436
\usepackage[svgnames]{xcolor}
3537
\usepackage[fixlr=false]{pythontex} %Need fixlr=false so can demo issue
3638
\usepackage{enumitem}
@@ -69,7 +71,7 @@
6971
% Right brace \} Tilde \~}
7072
%
7173
%
72-
% \changes{v0.9beta3}{2012/07/17}{Added Unicode support, which required the Python code to be split into one set for Python 2 and another set for Python 3. This will require any old installation to be completely removed, and a new installation created from scratch. Refactoring of Python code. Documents should automatically re-execute all code after updating to the new version. Otherwise, you should delete the PythonTeX directory and run PythonTeX. Improved installation script. Added package options: pyfuture, stderr, workingdir, upquote, pyglexer, pyginline. Renamed the pygextfile option to fvextfile. Added custom code command. Added the console environment and associated options. Rewrote pythontex\_utils*.py, creating a new, context-aware interface to SymPy's LatexPrinter class. Content brought in via macros no longer uses labels. Rather, long defs are used, which allows line breaks. Pygments highlighting is now default for PythonTeX commands and environments}
74+
% \changes{v0.9beta3}{2012/07/17}{Added Unicode support, which required the Python code to be split into one set for Python 2 and another set for Python 3. This will require any old installation to be completely removed, and a new installation created from scratch. Refactoring of Python code. Documents should automatically re-execute all code after updating to the new version. Otherwise, you should delete the PythonTeX directory and run PythonTeX. Improved installation script. Added package options: pyfuture, stderr, upquote, pyglexer, pyginline. Renamed the pygextfile option to fvextfile. Added custom code and workingdir commands. Added the console environment and associated options. Rewrote pythontex\_utils*.py, creating a new, context-aware interface to SymPy's LatexPrinter class. Content brought in via macros no longer uses labels. Rather, long defs are used, which allows line breaks. Pygments highlighting is now default for PythonTeX commands and environments}
7375
% \changes{v0.9beta2}{2012/05/09}{Changed Python output extension to .stdout.}
7476
% \changes{v0.9beta}{2012/04/27}{Initial public beta release.}
7577
%
@@ -102,7 +104,7 @@
102104
%
103105
%
104106
% \section*{Package status}
105-
% \pytex\ is currently in ``beta.'' Almost features intended for version 0.9 are present, and almost all are fully functional (at least, so far as is known!). Testing is the main task that remains. Much of that will involve testing installation and functionality under different operating systems and configurations. \pytex\ has been primarily developed and tested under Windows with \TeX\ Live and Python 2.7.
107+
% \pytex\ is currently in ``beta.'' Almost all features intended for version 0.9 are present, and almost all are fully functional (at least, so far as is known!). Testing is the main task that remains. \pytex\ has been primarily developed and tested under Windows with \TeX\ Live and Python 2.7. It has also been tested with Python 3.2 under Windows, and has been used under OS X (10.7) with MacPort's \TeX\ Live and Python 2.7.
106108
% \vfill
107109
%
108110
%
@@ -167,7 +169,7 @@
167169
% \item README
168170
% \item Optional batch file |pythontex.bat| for use in launching |pythontex*.py| under Windows
169171
% \end{itemize}
170-
% The style file |pythontex.sty| may be generated by running \LaTeX\ on |pythontex.ins|. The documentation you are reading may be generated by running \LaTeX\ on |pythontex.dtx|. Two versions of all of the Python scipts are supplied, one for Python 2 and one for Python 3.\footnote{Unfortunately, it is not possible to provide full Unicode support for both Python 2 and 3 using a single script. Currently, all code is written for Python 2, and then the Python 3 version is automatically generated via the |pythontex\_2to3.py| script.}
172+
% The style file |pythontex.sty| may be generated by running \LaTeX\ on |pythontex.ins|. The documentation you are reading may be generated by running \LaTeX\ on |pythontex.dtx|. Two versions of all of the Python scipts are supplied, one for Python 2 and one for Python 3.\footnote{Unfortunately, it is not possible to provide full Unicode support for both Python 2 and 3 using a single script. Currently, all code is written for Python 2, and then the Python 3 version is automatically generated via the |pythontex\_2to3.py| script. This script comments out code that is only for Python 2, and un-comments code that is only for Python 3.}
171173
%
172174
% Until \pytex\ is submitted to CTAN, it must be installed manually. The \pytex\ files should be installed within the \TeX\ directory structure as follows.
173175
% \begin{itemize}
@@ -654,6 +656,12 @@
654656
% Under development. For now, see the To Do list.
655657
% }
656658
%
659+
%
660+
%
661+
% \section*{Acknowledgements}
662+
%
663+
% Thanks to Øystein Bjørndal for suggestions and for help with OS X compatibility.
664+
%
657665
%
658666
%
659667
% \PrintChanges

pythontex/pythontex.pdf

178 KB
Binary file not shown.

pythontex/pythontex2.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,10 @@ def hash_code(data, temp_data, old_data, typedict):
733733
data['pygments_style_defs'] = pygments_style_defs
734734

735735
# Clean up old files, if possible
736-
if loaded_old_data:
736+
# Check for 'files' and 'pygments_files' keys, for upgrade purposes
737+
if (loaded_old_data and
738+
'files' in old_data and
739+
'pygments_files' in old_data):
737740
# Clean up for code that will be run again, and for code that no
738741
# longer exists. We use os.path.normcase() to fix slashes in the path
739742
# name, in an attempt to make saved paths platform-independent.
@@ -767,7 +770,12 @@ def hash_code(data, temp_data, old_data, typedict):
767770
f = os.path.normcase(f)
768771
if os.path.isfile(f):
769772
os.remove(f)
770-
773+
elif loaded_old_data:
774+
print('* PythonTeX warning')
775+
print(' PythonTeX may not have been able to clean up old files.')
776+
print(' This should not cause problems.')
777+
print(' Delete the PythonTeX directory and run again to remove any unused files.')
778+
temp_data['warnings'] += 1
771779

772780

773781

pythontex/pythontex3.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,10 @@ def hash_code(data, temp_data, old_data, typedict):
733733
data['pygments_style_defs'] = pygments_style_defs
734734

735735
# Clean up old files, if possible
736-
if loaded_old_data:
736+
# Check for 'files' and 'pygments_files' keys, for upgrade purposes
737+
if (loaded_old_data and
738+
'files' in old_data and
739+
'pygments_files' in old_data):
737740
# Clean up for code that will be run again, and for code that no
738741
# longer exists. We use os.path.normcase() to fix slashes in the path
739742
# name, in an attempt to make saved paths platform-independent.
@@ -767,7 +770,12 @@ def hash_code(data, temp_data, old_data, typedict):
767770
f = os.path.normcase(f)
768771
if os.path.isfile(f):
769772
os.remove(f)
770-
773+
elif loaded_old_data:
774+
print('* PythonTeX warning')
775+
print(' PythonTeX may not have been able to clean up old files.')
776+
print(' This should not cause problems.')
777+
print(' Delete the PythonTeX directory and run again to remove any unused files.')
778+
temp_data['warnings'] += 1
771779

772780

773781

pythontex/pythontex_2to3.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Convert PythonTeX scripts from Python 2 to Python 3
6+
7+
It isn't possible to have a single PythonTeX code base, since unicode text
8+
needs to be supported. Under Python 2, this means importing unicode_literals
9+
from __future__, or using the unicode function or "u" prefix. Under Python 3,
10+
all strings are automatically unicode.
11+
12+
At the same time, the differences between the Python 2 and 3 versions are
13+
usually very small, involving only a few lines of code. To keep the code base
14+
unified, while simultaneously fully supporting both Python 2 and 3, the
15+
following scheme was devised. The code is written for Python 2. Whenever
16+
code is not compatible with Python 3, it is enclosed with the tags
17+
"#// Python 2" and "#\\ End Python 2" (each on its own line, by itself). If
18+
a Python 3 version of the code is needed, it is included between analogous
19+
tags "#// Python 3" and "#\\ End Python 2". The Python 3 code is commented
20+
out with "#", at the same indentation level as the Python 3 tags.
21+
22+
This script creates Python 3 scripts from the original Python 2 scripts
23+
by commenting out everything between the Python 2 tags, and uncommenting
24+
everything between the Python 3 tags. In this way, full compatibility is
25+
maintained with both Python 2 and 3 while keeping the code base essentially
26+
unified. This approach also allows greater customization of version-specific
27+
code than would be possible if automatic translation with a tool like 2to3
28+
was required.
29+
30+
Copyright (c) 2012, Geoffrey M. Poore
31+
All rights reserved.
32+
Licensed under the BSD 3-Clause License:
33+
http://www.opensource.org/licenses/BSD-3-Clause
34+
35+
'''
36+
37+
38+
# Imports
39+
from __future__ import unicode_literals
40+
from io import open
41+
import re
42+
43+
44+
files_to_process = ('pythontex2.py', 'pythontex_types2.py', 'pythontex_utils2.py')
45+
encoding = 'utf-8'
46+
47+
48+
def from2to3(list_of_code):
49+
fixed = []
50+
in_2 = False
51+
in_3 = False
52+
indent = ''
53+
54+
for line in list_of_code:
55+
if r'#// Python 2' in line:
56+
in_2 = True
57+
indent = line.split('#', 1)[0]
58+
elif r'#\\ End Python 2' in line:
59+
in_2 = False
60+
elif r'#// Python 3' in line:
61+
in_3 = True
62+
indent = line.split('#', 1)[0]
63+
elif r'#\\ End Python 3' in line:
64+
in_3 = False
65+
elif in_2:
66+
line = re.sub(indent, indent + '#', line, count=1)
67+
elif in_3:
68+
line = re.sub(indent + '#', indent, line, count=1)
69+
fixed.append(line)
70+
return fixed
71+
72+
73+
for file in files_to_process:
74+
f = open(file, 'r', encoding=encoding)
75+
converted_code = from2to3(f.readlines())
76+
f.close()
77+
f = open(re.sub('2', '3', file), 'w', encoding=encoding)
78+
f.write(''.join(converted_code))
79+
f.close()
80+
81+
100 KB
Binary file not shown.

pythontex_gallery/pythontex_gallery.tex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
\documentclass[11pt]{article}
2+
\usepackage[T1]{fontenc}
3+
\usepackage[utf8]{inputenc}
24
\usepackage{amsmath,amssymb}
35
\usepackage{fullpage}
46
\usepackage{graphicx}
@@ -7,6 +9,7 @@
79
\usepackage[stderr]{pythontex}
810
\restartpythontexsession{\thesection}
911

12+
1013
\usepackage[framemethod=TikZ]{mdframed}
1114

1215
\newcommand{\pytex}{Python\TeX}
@@ -48,6 +51,18 @@ \section{General Python interaction}
4851
But we don't have to typeset the code. It can be hidden.\pyc{mystring=r'\textbf{This is a message from Python}'} And then we can access it later: \py{mystring}.
4952

5053

54+
\section{Pygments highlighting}
55+
56+
\pytex\ supports syntax highlighting via Pygments. Any language supported by Pygments can be highlighted. Unicode is supported. Consider this snippet copied and pasted from a Python 3 interactive session. (Using random strings of Unicode for variable names is probably not a good idea, but \pytex\ will happily highlight it for you.)
57+
58+
\begin{pygments}{pycon}
59+
>>> âæéöø = 123
60+
>>> ßçñðŠ = 456
61+
>>> âæéöø + ßçñðŠ
62+
579
63+
\end{pygments}
64+
65+
5166
\section{Python console environment}
5267

5368
\pytex\ includes an environment that emulates a Python interactive session. Commands are entered within the environment, each line is treated as input to an interactive session, and the result is typeset.

0 commit comments

Comments
 (0)