# newcommand with argument and shell-escape/write18

This example works with shell-escape:

\documentclass{article}
\usepackage{python}
\begin{document}
\begin{python}
from sympy import *
from sympy.abc import *
print "$" + latex(expand( (x+1)*(x-1) )) + "$"
\end{python}
\end{document}


A very primitive (not working) approach of putting the symbolic expansion in a newcommand is

\documentclass{article}
\usepackage{python}
\newcommand{\pyexpand}[1]{
\begin{python}
from sympy import *
from sympy.abc import *
print "$" + latex(expand( #1 )) + "$"
\end{python}
}
\begin{document}
\pyexpand{(x+5)*(x-5)}
\end{document}


Error: ! Use of \@python doesn't match its definition.

How to make this work?

• My feeling is that the python environment is in the same class as verbatin, so it cannot appear as argument to another command. – egreg Jul 25 '17 at 19:32

You can't, sorry.

The python environment is defined to write its contents in an auxiliary file and in order to do this it performs verbatim trickery, which prevents it from being used as the argument to a command.

You can use PythonTeX, though:

\documentclass{article}
\usepackage{pythontex}

\begin{pythontexcustomcode}{py}
from sympy import *
from sympy.abc import *
\end{pythontexcustomcode}

\newcommand{\pyexpand}[1]{%
$\py{latex(expand( #1 ))}$%
}

\begin{document}

\pyexpand{(x-1)*(x+1)}

\end{document}


This requires an intermediate run of pythontex.