3

I have the following MWE using pythontex:

%& -shell-escape
\documentclass{article}
\usepackage{pythontex}
\newcommand{\printPython}[1]{
\py{print(r'Hello, #1')}%
\newline
}
\begin{document}

Say hello Python:

%\begin{python}%
%print r"Hello \LaTeX!"
%\end{python}%
\printPython{4}
\begin{pycode}

print(r"\centering")
print(r"\textit{A message from Python!}")
\end{pycode}

\end{document}

When compiling, the result is

Say hello Python:
Hello, 4 None
A message from Python!

Where does the None come from, and how do I remove it?

  • I solved it by replacing \py with \pyc, but nevertheless I am interested in the reason here. – arc_lupus Mar 12 '16 at 11:15
2

\py inserts a string representation of whatever it is given. In this case, it was given the print() function, which returns None. So you get what was printed, plus the return value. Basically, \py is a shortcut so that using print() isn't necessary. You could just use \py{r'Hello, #1'}%.

\pyc is the inline equivalent of the pycode environment. It just executes code. So if you want it to insert anything in the document, you need to use print().

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.