# Save tikz picture to disk

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:

\begin{tikspicture}[...]
...
\end{tikzpicture}


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

Something like:

\begin{tikspicture}[save=filename,...]
...
\end{tikzpicture}


or

\begin{tikspicture}[...]
...
\end{tikzpicture}
\savelasttikzpicture{filename}


(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}')
end


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

Thanks

-
Have a look at this question. – Marc van Dongen 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
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
@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
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

## 1 Answer

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.

-