python, pythontex and plots

C

chitturk

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{wrapfig} % Allows in-line images
\usepackage{pythontex}
\setpythontexworkingdir{.}
\begin{document}

This is an example of using pythontex

\begin{pycode}

import pylab as p
import numpy as np
x = np.linspace(0.0,1.0,10)
y = 2.0*x + 5.0
xmax = max(x)
ymax = max(y)
p.plot(x,y)
outputfile = 'myplot.png'
p.savefig(outputfile)
p.clf()

\end{pycode}

% this works fine, I can print the name of the output file, variables

The plot is named \py{outputfile}, the max in x was \py{xmax} and \py{ymax}

% now I would like to plot it, this works

\includegraphics[scale=0.75]{myplot.png}

% but when I do this

%\includegraphics[scale=0.75]{\py{outputfile}}

% I get

%! File ended while scanning definition of \filename@base.
%<inserted text>
% }
% no pdf



\end{document}
 
S

Steven D'Aprano

I don't usually top post, but for this I'll make an exception because I
have *no idea* what any of your post means.

Are you actually asking a question? I can't see a question mark in it, so
there's no explicit question. Are you just sharing something interesting
you have learned? Expecting us to read your mind and intuit what the
question is?

Why do nearly, but not quite all, the lines in your post start with % or
\ or sometimes both? It looks like Postscript rather than Python.

Perhaps you should try again, and this time don't assume that we're
familiar with whatever it is you're doing. Remember to include natural
language explaining what your problem is, what result you expected, what
result you actually got. In this forum, use English please. This is
*especially* critical if using an unusual technology that most people are
not familiar with.

I strongly recommend that first you read this:

http://sscce.org/‎

Its written for Java developers, but the same basic principles apply for
Python. Remember that the number one language you should be using for
communication is natural language (English in this forum) and code only
second.

Thank you.


Steve
 
C

chitturk

1) The file is LaTeX
2) the % means LaTeX will ignore it.
3) The question was about using pythontex with LaTeX
4) I included the file so you (OK, others) could see what I was trying
5) The problem had to do with accessing a file name within the python
script using \py{outputfile}
6) pythontex is a terrific package that I enjoy using
7) I was looking for help on debugging this error.
8) I was trying to find out why I could not do \py{outfilename}

I don't usually top post, but for this I'll make an exception because I

have *no idea* what any of your post means.



Are you actually asking a question? I can't see a question mark in it, so

there's no explicit question. Are you just sharing something interesting

you have learned? Expecting us to read your mind and intuit what the

question is?



Why do nearly, but not quite all, the lines in your post start with % or

\ or sometimes both? It looks like Postscript rather than Python.



Perhaps you should try again, and this time don't assume that we're

familiar with whatever it is you're doing. Remember to include natural

language explaining what your problem is, what result you expected, what

result you actually got. In this forum, use English please. This is

*especially* critical if using an unusual technology that most people are

not familiar with.



I strongly recommend that first you read this:



http://sscce.org/



Its written for Java developers, but the same basic principles apply for

Python. Remember that the number one language you should be using for

communication is natural language (English in this forum) and code only

second.



Thank you.





Steve



\documentclass[12pt]{article}
\usepackage{graphicx}

\usepackage{wrapfig} % Allows in-line images \usepackage{pythontex}
\setpythontexworkingdir{.}
\begin{document}

This is an example of using pythontex
\begin{pycode}

import pylab as p
import numpy as np
x = np.linspace(0.0,1.0,10)
y = 2.0*x + 5.0
xmax = max(x)
ymax = max(y)
p.plot(x,y)

outputfile = 'myplot.png'
p.savefig(outputfile)
\end{pycode}

% this works fine, I can print the name of the output file, variables
The plot is named \py{outputfile}, the max in x was \py{xmax} and
\py{ymax}

% now I would like to plot it, this works
\includegraphics[scale=0.75]{myplot.png}

% but when I do this
%\includegraphics[scale=0.75]{\py{outputfile}}

% I get
%! File ended while scanning definition of \filename@base. %<inserted

% }
% no pdf



\end{document}
 
S

Steven D'Aprano

chitturk, you will probably get better answers if you actually explain
your question rather than writing as if we can read your mind. More
comments below:

1) The file is LaTeX
2) the % means LaTeX will ignore it.

Shouldn't you ask this on a LaTeX forum then? How is your problem related
to Python? If it is a Python error, you should explain how you know it is
a Python error rather than a LaTeX error, because to me it looks like a
LaTeX error.

3) The question was about using pythontex with LaTeX

And pythontex is what? An interface to Python via LaTeX? Are you getting
a Python error or a LaTeX error? It seems to me that you are getting a
LaTeX error, and that Python is irrelevant here. If you have information
that suggests otherwise, you should share it with us.


4) I included the file so you (OK, others) could see what I was trying

Problem is that it is not clear what parts are the file and what parts,
if any, are your commentary on the file. You shouldn't assume that Python
programmers will be experts on LaTeX syntax and be able to read your code.

5) The problem had to do with accessing a file name within the python
script using \py{outputfile}

If I am reading your file/comments correctly, the error you get is not a
Python error, but a LaTeX error:


%! File ended while scanning definition of \filename@base. %<inserted
text>
% }
% no pdf


If I am wrong, explain to me how you decided that this was a Python error
rather than LaTeX. I've been using Python for over 15 years, and this is
the first time I've seen anything even remotely like that.

It is not clear to me whether "no pdf" is your comment, the output of
pythontex, the output of LaTeX, or something else. It is also not clear
whether %<inserted text> is literally part of the output, or just a
placeholder for something you thought was irrelevant.

6) pythontex is a terrific package that I enjoy using

Glad to hear it.

7) I was looking for help on debugging this error.
8) I was trying to find out why I could not do \py{outfilename}

\py{outputfilename} isn't Python code, so I'm not sure why you think a
Python forum is the right place to ask about this. For us to help you,
we'll probably need to know:

- What is outputfilename?

- Are you sure it corresponds to an actual file? A readable file?
(Check the permissions.) Containing the right output? E.g. if you're
expecting a PDF file, but giving it a PNG file, it probably won't work.

- What Python code ends up being executed when you run \py
{outputfilename}?

- Can you run that code successfully from Python itself, bypassing
pythontex?

- If not, what result did you expect? What result did you get?

- If you got an exception, what was the entire exception?

- Any other relevant information that comes up while investigating.


Help us to help you. Excessively concise questions written for an
audience who already knows everything you know is not likely to get a lot
of help.
 
C

chitturk

True, I did not explain what I was trying to do.

pythontex is a package that allows the inclusion of python code within a
LaTeX document - (sort of like python.sty, but IMO, better) - I use
it along with noweb to create documents that contain documentation,
code and output of the code - and pythontex allows me to access variables
within the python code embedded in the LaTeX - except for the case
I mentioned ... Within the python code (inside the LaTeX document) I had
a "savefig(outputfile)" and I was trying to reference the outputfile
using \py{outputfile}

It may be that someone on the comp.text.tex group may have an answer
(there may be users of pythontex on that newsgroup)
 
P

Piet van Oostrum

True, I did not explain what I was trying to do.

pythontex is a package that allows the inclusion of python code within a
LaTeX document - (sort of like python.sty, but IMO, better) - I use
it along with noweb to create documents that contain documentation,
code and output of the code - and pythontex allows me to access variables
within the python code embedded in the LaTeX - except for the case
I mentioned ... Within the python code (inside the LaTeX document) I had
a "savefig(outputfile)" and I was trying to reference the outputfile
using \py{outputfile}

It could be that \includegraphics needs an expandable TeX form for its
filename, and \py probably isn't. I haven't tried pythontex yet (I
didn't know about it before your posting), so I can only guess. I am
going to try it out later. But if I am correct then the following might
work:

\newcommand{\filename}{}
\edef\filename{\py{outputfile}}

\includegraphics[scale=0.75]{\filename}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top