saving pgf to a stream is not supported #1625

sciunto opened this Issue Dec 28, 2012 · 12 comments


None yet

3 participants

sciunto commented Dec 28, 2012


The pgf output does not support a stream as illustrated by this snippet

import sys
plt.savefig(sys.stdout, format='pgf')

It returns:

ValueError: saving pgf to a stream is not supported, consider using the pdf option of the pgf-backend

Other formats like png, eps, ps, svg support it. I don't see any good reason to do not support streams for pgf and I guess it is a bug :)


@pwuertz pwuertz was assigned Dec 28, 2012
pwuertz commented Dec 28, 2012

Sorry, not a bug. Figures can contain images. If you want to write the output to a stream these must be embedded in your format, but there is no way to embed binary data like raster graphics in Latex code. The backend must write such an output to possibly multiple files.
Once compiled to PDF the whole figure can be represented by a single file, which is why the PDF output of the PGF backend on the other hand does support streams.

pwuertz commented Dec 28, 2012

I'm going to close the issue since it is not a bug, but feel free to continue if you have further questions.

@pwuertz pwuertz closed this Dec 28, 2012
filmor commented Oct 21, 2013

Wouldn't it suffice to raise the error, when there is actual binary data to handle?

pwuertz commented Oct 21, 2013

This would be an option, but I didn't want users to think that pgf-to-stream is a general viable output method. I think for a toolkit like matplotlib imshow() isn't any less important than plot(). If I can't support such a core function, I'd rather guide the user to other output paths. As a user, I'd be pretty mad if I created some workflow that seems to work perfectly, only to learn some day that it's useless once trying to plot a simple image.

filmor commented Oct 21, 2013

How about a "force" parameter then?

pwuertz commented Oct 21, 2013

Hm, I did some research on this matter. It seems that LuaTeX provides the scripting tools necessary for writing binary files (e.g. png images) from embedded data. One could use a small Lua script that exports a base64 encoded image when the tex code is interpreted, which is then loaded with \includegraphics.

By the way, do you mind elaborating the use case of direct printing pgf code to a stream? Why not saving to a temporary file to stream from? It's not like you can get away with zero disk-access with latex in the pipeline anyway.

sciunto commented Oct 21, 2013

@pwuertz In my case, my workflow uses automation tools to build figures. It's very easy to use a stream and dealing with a file is much much more complicated and need a large refactoring. So, i'm convinced some users may prefer/need a stream.

pwuertz commented Oct 21, 2013

@sciunto What about the pdf output of backend-pgf?

sciunto commented Oct 21, 2013

@pwuertz This is not sufficient for my usage. I really need the source code.

filmor commented Oct 22, 2013

Me too, I use pythontex to embed matplotlib-generated graphs directly in my latex document.

pwuertz commented Oct 22, 2013

@filmor But couldn't your embedded python code just save the figure to a file and return the appropriate \input or \includegraphics command?

For embedding the data for raster images the luatex scripting trick seems to work. When compiling the document the images will be written to files by Lua so they can be included by TeX as usual. I'm not really convinced of the benefit though, since in the end the binary data must be stored to separate files anyway + it increases latex code size and compile time.

Nevertheless, I guess for making your use cases work we could just downgrade that exception to a warning. I'll prepare a pull request for this soon.

@pwuertz pwuertz reopened this Nov 8, 2013
@pwuertz pwuertz closed this in a247d93 Nov 12, 2013
pwuertz commented Nov 12, 2013

Partial stream support is now in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment