# \newcommand with PythonTeX PyCode

Environments are MiKTeX 2.9, XeLaTeX, PythonTeX 0.11 beta, and Python 3.3. I am pretty new to LaTeX and Python/PythonTeX and am struggling with the following piece of code.

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8
\documentclass{article}
\usepackage{fontspec,xunicode,xltxtra}
\usepackage{fancyvrb}
\usepackage{etex}
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{pgfopts}
\usepackage{newfloat}
\usepackage{xcolor}
\usepackage{pythontex}

\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\newcommand{\pow}[2]{\py{#1**#2}}
\newcommand{\poww}[2]{\pyc{print(#1**#2)}}
\newcommand{\powww}[2]{\begin{pycode}print(#1**#2) \end{pycode}}

\begin{document}
\pow{2}{5}      % success
\poww{2}{6}     % success
\powww{2}{7}    % fail
\end{document}


I get an error on \powww and can not understand why. Any help appreciated.

Extraneous input print(2**7) \end {pycode}^^I% fail' between \begin{pycode}[
<key=value>] and line end
.
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}

l.28 \powww{2}{7}^^I% fail


The ultimate goal is to pass sets of 2 unicode strings as parameters from the XeLaTeX document via PythonTeX to Python, but I prefer to "start simple and then expand step by step".

-

## migrated from stackoverflow.comApr 11 '13 at 7:15

This question came from our site for professional and enthusiast programmers.

PythonTeX environments, like the pycode environment, cannot be used inline. There must be a linebreak after the \begin{pycode}, and the \end{pycode} must be on a line by itself as well. Also, PythonTeX environments cannot be used inside LaTeX commands. This limitation is due to the way LaTeX deals with verbatim (literal) text in environments (it's related to TeX's catcodes).
If you want to create custom commands, \py and \pyc are the way to go. If you want to create a command that involves more complex code, then you should probably define a function in a pycode environment, and call that function within \py or \pyc`.