%% This program is free software; you can redistribute it and/or %% modify it under the terms of the GNU General Public License %% as published by the Free Software Foundation; either version 2 %% of the License, or (at your option) any later version. %% %% This program is distributed in the hope that it will be useful, %% but WITHOUT ANY WARRANTY; without even the implied warranty of %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %% GNU General Public License for more details. %% %% You should have received a copy of the GNU General Public License %% along with this program; if not, write to the Free Software %% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. %% %% 0.4 Version by HernĂ¡n Grecco %% 0.3 Version by James Brotchie %% %% Author: Martin R. Ehmsen, ehmsen@imada.sdu.dk. %% Department of Mathematics and Computer Science, %% University of Southern Denmark, DK %% %% You can find an online copy of the GPL at %% http://www.gnu.org/copyleft/gpl.html . %% %% Note: shell-escape needs to be activated for this to work. %% This can either be done by passing -shell-escape as an option to %% latex or by adding/changing "shell_escape = t" in your texmf.cnf . % Python-sty Changelog % ==================== % % % 0.4 (2013-11-28) % ---------------- % - Python code is only executed if it has changed. % - Option to save auxiliary files in a subfolder. % - Option to choose the python binary. % - Code refactoring. % - UTF-8 support. % % % 0.3 (2011-12-22) % ---------------- % % - Intermediate files are now stored with different filenames. % - The return code of each Python shell execution is stored % in an .rc file. % - Any stderr output of a failed Python shell execution is % inserted as a red verbatim in the resulting document. % -- James Brotchie % % 0.21 % ---- % % - Moved \newwrite\@module from \@writemodule and out, since % no more than 15 \newwrites are allowed (and the previous % version created a new every time \@writemodule was called. % % \NeedsTeXFormat{LaTeX2e}[1994/12/01] \ProvidesPackage{python}[2013/11/17 v0.4 Python in LaTeX] \RequirePackage{etoolbox} \RequirePackage{verbatim} \RequirePackage{color} \RequirePackage{ifplatform} \RequirePackage{fancyvrb} \RequirePackage{inputenc} \RequirePackage{kvoptions} % Put the resulting Python code in separate files. \newcommand{\@PYT@outname}{\jobname\arabic{@PythonEnvironmentCounter}} \newcounter{@PythonEnvironmentCounter} \setcounter{@PythonEnvironmentCounter}{1} % Package Options \SetupKeyvalOptions{ family=PYT, prefix=PYT@ } \DeclareVoidOption{subfolder}{ \renewcommand{\@PYT@outname}{py/\jobname\arabic{@PythonEnvironmentCounter}} } \DeclareStringOption[python]{bin}[python] \ProcessKeyvalOptions* \newcommand{\@pythonbin}{\PYT@bin \space runpy.py \space} \typeout{PYTHON: Binary \@pythonbin} \newread\@PYT@retcode \newread\@PYT@diff \newcommand{\@PYT@copytocache}{ \ifwindows \immediate\write18{copy \@PYT@outname.py \@PYT@outname.py.cache} \else \immediate\write18{cp \@PYT@outname.py \@PYT@outname.py.cache} \fi } \newcommand{\@PYT@deletescript}{ \ifwindows \immediate\write18{del \@PYT@outname.py} \else \immediate\write18{rm \@PYT@outname.py} \fi } \newcommand{\@PYT@comparefiles}{ \ifwindows \immediate\write18{fc \@PYT@outname.py \@PYT@outname.py.cache & call echo ^\@percentchar ERRORLEVEL ^\@percentchar > \@PYT@outname.cmp.rc} \else \immediate\write18{cmp \@PYT@outname.py \@PYT@outname.py.cache; echo $? > \@PYT@outname.cmp.rc} \fi } \newcommand{\@PYT@executescript}{ \typeout{PYTHON: Running file \@PYT@outname.py} \immediate\write18{\@pythonbin \space\@PYT@outname.py} % Copy to the current script to keep a cache. \@PYT@copytocache \@PYT@deletescript } \newcommand{\@PYT@returncode}{ \inputencoding{utf8} \input{\@PYT@outname} % Read the return code of the executed Python script. \immediate\openin\@PYT@retcode=\@PYT@outname.py.rc \immediate\read\@PYT@retcode to \rc \immediate\closein\@PYT@retcode % If the return code isn't zero then include % the traceback. \ifnumequal{\rc}{0}{ \typeout{PYTHON: Run was successful} }{% \typeout{PYTHON: Run failed} \begingroup \color{red} \verbatiminput{\@PYT@outname.py.err} \endgroup } } \newenvironment{python} { \typeout{PYTHON: Writing file \@PYT@outname.py} \@PYT@deactivateeightbit\VerbatimOut{\@PYT@outname.py}} { \endVerbatimOut \@PYT@comparefiles \immediate\openin\@PYT@diff=\@PYT@outname.cmp.rc \immediate\read\@PYT@diff to \rc \immediate\closein\@PYT@diff \ifnumequal{\rc}{0}{ % Old file and new file are identical \typeout{PYTHON: No need to run the script again} }{ % Old file and new file are different or old is not present \@PYT@executescript } \@PYT@returncode \immediate\stepcounter{@PythonEnvironmentCounter} } \newcommand{\@PYT@deactivateeightbit}{% \count@=127 \loop \catcode\count@=12 \ifnum\count@<255 \advance\count@\@ne \repeat }