I need to save the result of a tikzpicture to disk in code.

Basically I have written some optimization routines for other things that only create the image if it does not exist on disk. I want to apply the same thing to tikzpictures. I do not want to use externalize(mine is faster and more useful to me).

So, given a tikzpicture:


I need some way to save it to disk as a png or jpg or whatever(png is preferable).

Something like:




(tikz externalize does not work because it doesn't seem to play nice with lualatex. I have code essentially like this:

\directlua{ drawTikzFig(arg); }

in lua:

function drawTikzFig(arg) 
    tex.print('\\begin{tikzpicture} ... \\directlua{getValue(arg);} ... \\end{tikzpicture}')

In fact, I don't know if this is exactly what is breaking tikz externalize or not but it gives errors at \directlua{}


  • Have a look at this question. – user10274 Jan 3 '13 at 19:19
  • @MarcvanDongen As I said, I do not want to use externalize, I have a somewhat better method and more consistent with other things I do. Essentially I would like to have an \export[filename]{...} which will export the ... "visual". This way it can be used with anything like equations, tables, or whatever. – Archival Jan 3 '13 at 20:05
  • 1
    One way would be to have the tikz pictures in separate files using the standalone class, and have a make file which compiles everything. For more on creating tikzpictures using standalone, see this answer. – Vivi Jan 4 '13 at 20:38
  • 1
    @Archival You can include the tikz pictures in the main file either by including the pdf generated by standalone or by using an \include{} in the main file (using \usepackage{standalone}). I don't think it would be that difficult, and I believe it would save you a lot of time in the future. Anyway, food for thought :) – Vivi Jan 4 '13 at 20:57
  • 1
    Archival, @Vivi: I was actually thinking about to add some extraction feature into standalone to help people to switch their existing internal tikzpictures etc. to external files. However, so far I didn't had time. – Martin Scharrer Jan 4 '13 at 21:32

You could use the preview package to create a single page from every tikzpicture environment using the \PreviewEnvironment (or similar, see the manual) macro. The resulting PDF can then be converted to multiple PNGs if required (but I suggest keeping the diagrams vector graphics!). However, this will only number all images and does not allow you to label them. You will run into trouble if the order or number of tikzpictures changes.

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.