gpoore / pythontex

A LaTeX package that executes Python and other code in LaTeX documents, and includes the output
874 stars 98 forks source link

keep Python code in .py files #89

Open TheChymera opened 8 years ago

TheChymera commented 8 years ago

For a long time now, I am keeping my pycode snippets under ./pythontex/pycode.tex, a file which ends up containing many entries like this one:

\begin{pycode}[timetable]
pytex.add_dependencies('/home/chymera/src/timeplot/timeplot/timetable.py')
sys.path.append('/home/chymera/src/timeplot/timeplot')
import timetable

select = [["Animal","treatments"],["FMRIMeasurement"],["TreatmentProtocol"],["Treatment","start_date","2015,11,11","2015,11,10"]]
shade = [{"TreatmentProtocol_code":["chrFlu","Treatment_start_date","Treatment_end_date"]},{"TreatmentProtocol_code":["acFlu","Treatment_start_date"]}]
timetable.multi_plot("~/meta.db", select, "Animal_id", shade=shade, saturate=["FMRIMeasurement_date"], saturate_cmap="winter")

flu = latex_figure(save_fig(fig_width=13,fig_height=3.5), caption="Animal Timetable Overview")
\end{pycode}

It's really uncomfortable to edit such a text file, because it is .tex, though mostly I am writing python in it. It would be great if I could benefit from my editors' python features when editing this file.

Any idea how I would be able to store all of this python code in .py files?

I had a couple of ideas, sich as individual files per snippet which I then include in ./pythontex/pycode.tex - or - just populating ./pythontex/pycode.tex with function calls from another file.... but somehow both these solutions seem just as messy as the current situation.

obtitus commented 8 years ago

Just a note on the editor. I am using emacs, and simply type m-x python-mode whenever I am editing within the pycode environment.

To store this code somewhere else, you could do (in timetable.py):

def plot_animl_timetable_overview(latex_figure, save_fig): select = [["Animal","treatments"],["FMRIMeasurement"],["TreatmentProtocol”], ["Treatment","start_date","2015,11,11","2015,11,10"]] shade = [{"TreatmentProtocol_code":["chrFlu","Treatment_start_date","Treatment_end_date"]},{"TreatmentProtocol_code":["acFlu","Treatment_start_date"]}] timetable.multi_plot("~/meta.db", select, "Animal_id", shade=shade, saturate=["FMRIMeasurement_date"], saturate_cmap="winter")

return latex_figure(save_fig(fig_width=13,fig_height=3.5), caption="Animal Timetable Overview”)

and then in tex \begin{pycode}[timetable] pytex.add_dependencies('/home/chymera/src/timeplot/timeplot/timetable.py') sys.path.append('/home/chymera/src/timeplot/timeplot') import timetable timetable. plot_animl_timetable_overview(latex_figure, save_fig) \end{pycode}

On 28 Apr 2016, at 06:28, Horea Christian notifications@github.com wrote:

For a long time now, I am keeping my pycode snippets under ./pythontex/pycode.tex, a file which ends up containing many entries like this one:

\begin{pycode}[timetable] pytex.add_dependencies('/home/chymera/src/timeplot/timeplot/timetable.py') sys.path.append('/home/chymera/src/timeplot/timeplot') import timetable

select = [["Animal","treatments"],["FMRIMeasurement"],["TreatmentProtocol"],["Treatment","start_date","2015,11,11","2015,11,10"]] shade = [{"TreatmentProtocol_code":["chrFlu","Treatment_start_date","Treatment_end_date"]},{"TreatmentProtocol_code":["acFlu","Treatment_start_date"]}] timetable.multi_plot("~/meta.db", select, "Animal_id", shade=shade, saturate=["FMRIMeasurement_date"], saturate_cmap="winter")

flu = latex_figure(save_fig(fig_width=13,fig_height=3.5), caption="Animal Timetable Overview") \end{pycode}

It's really uncomfortable to edit such a text file, because it is .tex, though mostly I am writing python in it. It would be great if I could benefit from my editors' python features when editing this file.

Any idea how I would be able to store all of this python code in .py files?

I had a couple of ideas, sich as individual files per snippet which I then include in ./pythontex/pycode.tex - or - just populating ./pythontex/pycode.tex with function calls from another file.... but somehow both these solutions seem just as messy as the current situation.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

gpoore commented 8 years ago

The suggestion from @obtitus is probably about the best option currently.

In the next batch of major releases, I'm hoping to have better support for working with external files. So eventually it should be possible to do something like \inputpythontex[timetable]{<file>}, which would more or less amount to a shortcut for @obtitus's code. I've been working on a lot of low-level improvements for the last several months, and that's finally all coming together, so I think everything is finally on course for new releases by mid-July. This feature might not make it into that, but it shouldn't take much longer. The new system should be much easier to extend and configure.

TheChymera commented 8 years ago

@gpoore very excited about that!

norok2 commented 7 years ago

I'd be following that. I tried whether it was possible to do it from within TeX without modifying the pythontex source, but I had no real luck with that. It is however possible to use the CTAN package python, but with a clumsy syntax:\begin{python}[<file>]\end{python} (as you can imagine, the environment is meant to insert Python code verbatim). The package itself is quite short, and uses LaTeX's \write18 behind the scenes. Unfortunately, the documentation is limited to the readme file with some more commend in the source, and quite possibly this 10 year old package is unmaintained.

alvitawa commented 6 years ago

Any updates on this? It would be really useful I think.

gpoore commented 6 years ago

@alvitawa No updates unfortunately. I've had essentially no time for anything related to LaTeX for several months, and probably won't for a while longer.

TheChymera commented 6 years ago

@gpoore but we did make a feature draft for this at SciPy2017, no? @alvitawa you can see presentation, poster, and article examples of how (multiple instances of) 1-figure-1-python-script functionality is best obtained from pythontex at this stage - in the RepSeP repository. The solution is actually quite stable and I have been using it and patching kinks out one at a time with every single poster and presentation I've put together in the last year. Try it out yourself!

alvitawa commented 6 years ago

@TheChymera Thanks! That's pretty cool

TheChymera commented 4 years ago

I guess this is already handles by RepSeP: https://github.com/TheChymera/RepSeP

Further discussion regarding upstreaming RepSeP functionality to here, can be found in this issue: https://github.com/gpoore/pythontex/issues/114