I'm trying to pass a LaTeX-Length (\textwidth) to a python function specified in an pyblock-environment provided by the pythontex-package. Running pythontex gives me an error message, that does not provide any help to me:
This is PythonTeX v0.12
---- Messages for py:default:default ----
* PythonTeX stderr - error on line 10:
File "<outputdir>/py_default_default.py", line 65
print(pytex.formatter(printtexlength(\textwidth )))
^
SyntaxError: unexpected character after line continuation character
--------------------------------------------------
PythonTeX: minimal - 1 error(s), 0 warning(s)
Has anybody an idea what is going wrong here, or how can I pass a LaTeX-Length to a python-function?
Here is a minimal-example:
\documentclass{scrreprt}
\usepackage{pythontex}
\begin{document}
\begin{pycode}
def printtexlength(length):
print('Length ist %f' % length)
\end{pycode}
\newcommand{\printlength}[1]{\py{printtexlength(#1)}}
%\py{printtexlength(\textwidth)} % This although fails with same error message
\printlength{\textwidth}
\end{document}


pythontexcommands don't expand their arguments, so Python receives the literal string\textwidthand doesn't know what to do with it. egreg's solution expands\textwidthto avoid this. The next version ofpythontex(beta on GitHub in a few days) has built-in utilities for passing contextual information like page dimensions to the Python side, as well as conversion functions for going from pt to in, cm, and mm. This can be used to make figures automatically adjust to page dimensions, etc. – G. Poore Jan 27 '14 at 14:14