summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/python
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/python
Initial commit
Diffstat (limited to 'macros/latex/contrib/python')
-rw-r--r--macros/latex/contrib/python/README25
-rw-r--r--macros/latex/contrib/python/python.sty89
2 files changed, 114 insertions, 0 deletions
diff --git a/macros/latex/contrib/python/README b/macros/latex/contrib/python/README
new file mode 100644
index 0000000000..8e290a6e9f
--- /dev/null
+++ b/macros/latex/contrib/python/README
@@ -0,0 +1,25 @@
+This package enables you to embed Python code (www.python.org) in LaTeX and insert the output into your LaTeX document.
+
+In you LaTeX document insert:
+
+\usepackage{python}
+...
+\begin{python}[optional.py]
+print("42")
+\end{python}
+
+The file optional.py is included together with the code between \begin{python} and \end{python}. Hence, you can extract common code to a shared file.
+
+Compile the document with the shell escape option and of course Python installed:
+
+% latex -shell-escape document.tex
+
+or insert/change "shell_escape = t" in your texmf.cnf
+
+If you have any comments, suggestions, or patches please email them to me.
+
+Please note that this material is subject to 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.
+See http://www.gnu.org/copyleft/gpl.html for the details of that license.
+
+Martin R. Ehmsen
+martin@ehmsen.org \ No newline at end of file
diff --git a/macros/latex/contrib/python/python.sty b/macros/latex/contrib/python/python.sty
new file mode 100644
index 0000000000..2d08fdf6cd
--- /dev/null
+++ b/macros/latex/contrib/python/python.sty
@@ -0,0 +1,89 @@
+%% 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.
+%%
+%% Author: Martin R. Ehmsen, martin@ehmsen.org.
+%%
+%% 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 .
+
+%% 0.2 -> 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}[2007/06/07 v0.21 Python in LaTeX]
+
+\newwrite\@out
+\newwrite\@module
+
+\begingroup \catcode `|=0 \catcode `[=1
+\catcode`]=2 \catcode `\{=12 \catcode `\}=12
+\catcode`\\=12 |gdef|@xpython#1\end{python}[|immediate|write|@out[#1]|end[python]]
+|endgroup
+
+\def\python{\kernel@ifnextchar [{\@python}{\@python[]}}
+
+\def\@python[#1]{%
+\gdef\@pythoninclude{#1}
+\immediate\openout\@out=\jobname.py
+\newlinechar='15
+\begingroup \catcode`\^^M=12 %
+\let\do\@makeother\dospecials\obeyspaces%
+\@xpython}
+
+\def\endpython{%
+\endgroup
+\immediate\closeout\@out
+\@writemodule
+\immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2> \jobname.py.err}
+\immediate\input\jobname.py.out}
+%\immediate\write{\begin{verbatim}}
+%\immediate\input\jobname.py.err
+%\immediate\write{\end{verbatim}}}
+
+\def\@writemodule{%
+\immediate\openout\@module=latex.py
+\immediate\write\@module{jobname="\jobname"}
+\immediate\closeout\@module}
+
+% BUGS:
+% 1. If anything gets send to stderr then it should be included
+% in \begin{verbatim}...\end{verbatim} to be properly displayed
+%
+% \immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2>\jobname.py.err}
+%
+% 2. Watch out for indentation done by aucTeX in Emacs
+%
+% 3. Let the package accept a "final version" option, such
+% that the output of each python run is saved such that it can be
+% inserted into the document by hand
+% (conference, journals are not likely to compile with
+% shell_escape or have python).
+%
+% 4. Somehow cache the output for each \begin{python}...\end{python}
+% and reuse the cached version if it has not changed.
+% Perhaps dangerous if the included python script has changed
+% but we would check for that as well.
+%
+%
+% \gdef\@prepython{}
+% \def\prepython#1{%
+% \gdef\@prepython{#1}
+% }
+% sed -e 's/^ //g' cluster.py
+% \immediate\write18{\@prepython\space\jobname.py > \