-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to share variables between pyconsole and pyblock? #55
Comments
That's not currently possible, and would actually be difficult to implement since pyconsole is executed via Python's If you just want shared startup code between the two, along the lines of |
What I'm trying to do is talk about a function in a module and then illustrate the
And this is how you use the function,
I hope that makes sense. I understand the narrative structure I'm going for is a little bit outside of the design plan of PythonTeX. The problem with your suggestion of using the pyconcode environment is that it results in a lot of duplicated code (one block to be typeset and the other not), which would make it hard for me to maintain as I develop the narrative. Thanks again |
Something like this should do what you want. This creates a \documentclass{article}
\usepackage{pythontex}
\newenvironment{pyconcodeblock}%
{\VerbatimEnvironment
\begin{VerbatimOut}{temp.py}}%
{\end{VerbatimOut}%
\pyconc{exec(compile(open('temp.py', 'rb').read(), 'temp.py', 'exec'))}%
\inputpygments{python}{temp.py}}
\begin{document}
\begin{pyconcodeblock}
def foo(x):
return 2*x
\end{pyconcodeblock}
\begin{pyconsole}
x = 10
foo(x)
\end{pyconsole}
\end{document} |
Could we/you consider a de-coupling of typesetting (display) and execution? Something like: In addition to a similar function to grab code from an external file, with optional, start_line and end_line arguments. I have been using pythontex since the start, and the current environment setup still confuses me (I always need to look it up). On 06 Feb 2015, at 21:05, Geoffrey Poore notifications@github.com wrote:
|
@obtitus That's something I would like to do soon. About 2 years ago, I wrote up a list of future features. A generic environment like you describe, and per-session customization for execution conditions, are the main things that remain to be implemented from that list. I would want a switch for showing code and for showing stdout and/or stderr. And, like you suggest, for turning execution on and off. I'm not sure about I've opened a new issue for this (#56), for continued discussion. I will try to post my old notes on this under that issue soon. |
I am afraid, this workaround does not work for me. The example above yields the following error:
I am using XeLaTeX invoked with
The following file structure is created:
If I fix the workaround to with
instead of
I get
Any ideas? |
@meisterluk Starting in PythonTeX version 0.14, the default working directory is the document root directory, rather that the Otherwise, you would need to modify the paths in the \newenvironment{pyconcodeblock}%
{\VerbatimEnvironment
\begin{VerbatimOut}{temp.py}}%
{\end{VerbatimOut}%
\pyconc{exec(compile(open('../temp.py', 'rb').read(), '../temp.py', 'exec'))}%
\inputpygments{python}{temp.py}} |
@gpoore The |
@gpoore Your
but the above generates errors ( Does anyone have any advice on how to parametrize (with the filename) @gpoore's solution? |
@banbh Environment arguments are only available at the beginning of the environment, not at the end. You will probably want to store |
@gpoore Thank you. I read through the
But now I have a new problem. It seems that the macro does not get expanded in the
Any suggestions on how to get macro-expansion to happen? |
@banbh All of the commands like \newcommand{\expandpyconc}[1]{\expandafter\reallyexpandpyconc\expandafter{#1}}
\newcommand{\reallyexpandpyconc}[1]{\pyconc{exec(compile(open('#1', 'rb').read(), '#1', 'exec'))}}
...
{\end{VerbatimOut}%
\expandpyconc{\snippetfile}} |
@gpoore Thank you! It works perfectly. I will post a MWE example in a day or two. |
Here is a more-or-less MWE of the macros @gpoore proposed:
It works perfectly for me. |
Hello. First, thank you very much for your answers on this topic. In the context of a beamer use of pyconcodeblock, I would like to now if there is a way to box the blocks produced by this function (or pyconc). I was not able to find such a way in the PythonTex manual. Thank you in advance for your help. |
@cccccp There isn't a built-in way to put boxes around the console commands and environments. But you can use the |
I read the documentation, and I suppose this sounds strange, but I want to have python variables shared between the pyconsole and pyblock families.
Is this possible?
Not that I'm using the v0.12 version which came with texlive on my ubuntu box.
Thanks
The text was updated successfully, but these errors were encountered: