5

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

Archival
  • 271
  • Have a look at this question. –  Jan 03 '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 03 '13 at 20:05
  • Sorry I didn't notice this requirement. However, what's wrong with building your \export facility on top of TikZ's externalise command? –  Jan 03 '13 at 20:39
  • @MarcvanDongen Nothing if it is easy and works well. I thought the externalize did everything behind the scenes so to speak. I'll look into it more to see if it is can do the job without too much work... but I was hoping there was some relatively easy way to do this already... – Archival Jan 03 '13 at 22:18
  • It doesn't seem too difficult. You can define an environment that saves the content with an lrbox* environment. With a tikzpicture you should be able to display the saved content and externalise it. –  Jan 03 '13 at 22:31
  • @MarcvanDongen Ok, after trying tikzexternalize it doesn't seem to work. I'm using lualatex and embed directlua calls inside tikz environments. Since tikzexternal calls pdflatex it seems to crap out on the lua code due to the way I use it. I generate tikz images using lua code so it is quite complex and hence the need to save the image. – Archival Jan 03 '13 at 23:26
  • If there is no simple way to extract a bounding box of the pdf directly(there should be, it's not difficult) then I need some way to do it after the pdf has been generated. – Archival Jan 03 '13 at 23:30
  • 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 04 '13 at 20:38
  • @Vivi: Oh, that would probably work but would be difficult to implement. I have thousands of generated tikz environments. While the it would not be difficult to move them to external files it would be hard to integrate it all back(or maybe not but still more work than the other method). (that is, as long as standalone has no issues with lualatex and I could make sure to include all the external references in the standalone file) – Archival Jan 04 '13 at 20:49
  • 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 04 '13 at 20:57
  • @Vivi Yes, in the future, but I have already designed my code around a different method. The main issue I see is having a ton of separate files(not a big issue though) and having external references to each tikzpicture environment(not every one is isolated)... e.g., they use macros, lua code, and stuff that references other things that will be hard to determine...(although maybe not too hard). I will definitely try it if nothing easier comes about. Thanks.... – Archival Jan 04 '13 at 21:00
  • My next suggestion, which may or may not help you, is for you to try and construct a complete reproducible example so that the real tex-hackers can play with your code and maybe find a solution. – Vivi Jan 04 '13 at 21:03
  • 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 04 '13 at 21:32
  • Archival, I am updating my packages and saw this new package called tikzinclude. Perhaps it will be useful to you? (@MartinScharrer maybe you will also be interested?) – Vivi Jan 06 '13 at 22:54

1 Answers1

3

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.

Martin Scharrer
  • 262,582