1

I found this post by the creator of PythonTeX himself. However, running the below code doesn't work for me; I get an error "Something's wrong--perhaps a missing \item. \end{pyblock}". I'm not convinced that the \pyc command is actually having the macro expanded.

Where am I going wrong? I'm trying to use package keyval to provide a nice interface to some TeX commands which pass the values on to Python, but in order to do that I need to be able to expand the TeX macros which are the key-values so that Python may consume them.

\documentclass{standalone}
\usepackage{pythontex}
\usepackage{lipsum}

\begin{document} \pyc{mytext = """\lipsum[4]"""}

\begin{pyblock}
print(mytext)
\end{pyblock}

\end{document}

I've created an even simpler example below.

\documentclass{standalone}
\usepackage{pythontex}
\def\x{2}
\begin{document}
    \pyc{print(\x)}
\end{document}
  • PythonTeX stderr - error on line 8: File "C:\Users\aosborne\AppData\Local\Temp<outputdir>\py_default_default.py", line 50 print(\x) SyntaxError: unexpected character after line continuation character

aosborne
  • 143
  • Just a guess from looking at the documentation, but perhaps add \printpythontex after the pyblock environment which apparently is not intended to produce output. – Don Hosek Dec 22 '20 at 22:19
  • I don't think it is the pyblock - I put an even simpler example in my question above. – aosborne Dec 22 '20 at 22:49
  • I don't know why but with standalone replaced with article, and package pythontex loaded with option gobble=auto, it works. – muzimuzhi Z Dec 22 '20 at 23:15
  • 1
    Yes! I literally found that 5 minutes ago. – aosborne Dec 23 '20 at 02:51
  • @aosborne I know, I just used your "answer" and added the "close" proposal so that the question can be closed. This will make it easier for other people to see what happened. – Dr. Manuel Kuehner Dec 23 '20 at 03:04

1 Answers1

1

how to pass section numbers to python in pythontex?

This is the answer. Use \setpythontexcontext{x=\x} and access it by \pyc{pytex.context.x}.

aosborne
  • 143