# Recreate plot when data file changes

Is it possible to use the TikZ/external library and have pgfplots recreate a figure when a data file that is plotted changes? Say I have something like the following:

file.tex:

\documentclass{article}

\usepackage{tikz,pgfplots}
\usetikzlibrary{external}
\tikzexternalize
\newcommand{\figFilename}{}

\begin{document}
\begin{figure}[t]
\centering
\renewcommand{\figFilename}{thePlot}
\tikzpicturedependsonfile{\figFilename.tikz}
\tikzsetnextfilename{\figFilename}
\input{\figFilename.tikz}
\caption{The caption.}
\end{figure}
\end{document}


thePlot.tikz:

\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},]
table[x=x,y=y] {data.dat};
\end{axis}
\end{tikzpicture}%


data.dat:

x y
0 20.44
1 20.06
2 19.76
3 19.54
4 19.40
5 19.34


If I change thePlot.tikz, the external library will detect that the figure is no longer up-to-date and recreate it. Is it possible to have the same effect when any of the plotted data files changes as well? I tried something along the lines of \tikzpicturedependsonfile{data.dat}, and it doesn't work. That wouldn't be an ideal solution anyway, as most times I use many data files in each plot and listing all dependent files manually would be more pain than gain.

So is there an easy, automated way for the external library to check all data files included in a tikzpicture and recreate the plot if at least one of them has changed?

-
As far as I know it reads the code not the code contents to compare so that would be difficult (guessing here) –  percusse Sep 3 '13 at 19:41
Are you familiar to arara? You may want to check out the (not yet released) version 4.0. It provides conditionals such as (pseudo-code!!!): arara: pdflatex: if data.dat has changed. Haven't tested or used it, but with that it should be doable. –  LaRiFaRi Sep 3 '13 at 19:43
@percusse: Yes, I assumed it would be something difficult. It'd be a great feature to have in the external library. –  sudosensei Sep 4 '13 at 15:50
@LaRiFaRi: I never heard of arara before. I'll have a look as soon as possible. Meanwhile, if you can provide an answer using arara, it would be great. –  sudosensei Sep 4 '13 at 15:51
@sudosensei Sorry, but I don't have the time at the moment. I would love to, as arara is very cool, and I need the conditionals as well, but I am not good in building stuff from source github.com/cereda/arara . Maybe Paulo steps over this post and has an idea or I'll post sometime later. –  LaRiFaRi Sep 4 '13 at 21:03

EDIT 2

I have committed bugfixes to PGF CVS.

EDIT

I have taken a closer look after your statement that it does not work despite my assurance that it does.

The answer should probably be "it used to work" - right now, it refrains from doing its job due to two related bugs in the external library.

The external lib seems to overwrite the dependencies if it encounters \tikzpicturedependsonfile outside of the externalized' pictures environment.

Your occurance of \tikzpicturedependsonfile causes this failure (note that you have to move it below \tikzsetnextfilename, btw). A solution would be to move this dependency statement into the thePlot.tikz, namely into the environment body. But before you do, let me stress that this particular line of code is superfluos if you have a decent PGF version (more recent than pgf 2.10): PGF auto-detects changes inside of the picture environment and recompiles the external file automatically.

If you use PGF 2.10: move your dependency statement into the tikzpicture environment of thePlot.tikz and you are fine.

If you use a more recent PGF version (i.e. an unstable PGF build): it contains another related bug which also overwrites the dependency file. In this case: proceed as in the last paragraph, but also call \tikzexternalize[up to date check=simple] in your externalization statement - this deactivates the up-to-date check.

I will fix both bugs in PGF and will post an update here once it is ready. The bugfix will (hopefully) allow you to use both requested features out-of-the-box as outlined in my original answer (see below).

The mentioned/requested features

• recompile externalized pdf if the content of the tikzpicture environment changes
• recompile externalized pdf if the content of an included data file data.dat changes

are already available. The first item works out-of-the-box if and only if you have a PGF version which is more recent than 2.10 (as you already mentioned; I'd just like to stress it here).

The second point works if and only if you use \tikzexternalize[mode=list and make]. As soon as you do, it works out-of-the-box because pgfplots always calls \tikzpicturedependsonfile{<filename>.<ext>} whenever it executes an \addplot table {<filename>}.

Currently, the external lib ignores \tikzpicturedependsonfile unless you use mode=list and make.

Note that make is precisely what you want here: you have a sequence of files as dependencies and you need to execture a couple of "targets" whenever one of these files is more recent than the other ones.

-
I am using mode=list and make, but the plot is not recreated when a data file changes. Is this because I invoke \tikzpicturedependsonfile{thePlot.tikz} manually and this prevents the external library from appending the rest of the dependencies? –  sudosensei Sep 4 '13 at 18:10
Please see my edit. Thanks for your note. –  Christian Feuersänger Sep 4 '13 at 18:44
@sudosensei I have committed bugfixes to PGF CVS. It will become part of the next stable. –  Christian Feuersänger Sep 5 '13 at 12:35
That is great. Thank you, Christian. Just to clarify: if I use the latest development version of PGF, the features requested will work out-of-the-box when using mode=list and make, without needing to use \tikzpicturedependsonfile<file.ext>. Is that right? –  sudosensei Sep 6 '13 at 15:18
@sudosensei Yes, right. –  Christian Feuersänger Sep 6 '13 at 15:48