Managing a project: separate .tex-files, externalized TikZ, folders
You can try something along this:
In main.tex
:
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\def\useexternalfile#1{\tikzsetnextfilename{#1-output}\input{\csname tikzexternal@filenameprefix\endcsname#1.tikz}}
\begin{document}
\tikzsetexternalprefix{./chapter1/tikz/}
\include{chapter1}
\tikzsetexternalprefix{./chapter2/tikz/}
\include{chapter2}
\end{document}
EDIT: A cleaner, more LaTeX-y definition of \useexternalfile
would be
\makeatletter
\newcommand{\useexternalfile}[1]{%
\tikzsetnextfilename{#1-output}%
\input{\tikzexternal@filenameprefix#1.tikz}}
\makeatother
For the record, \tikzexternal@filenameprefix
is the internal macro where \tikzsetexternalprefix
saves the "current prefix". Also, you can substitute #1-output
for your favourite expression (maybe generated-figure-#1
?)
END EDIT
In chapter1.tex
:
...
\begin{figure}
\useexternalfile{myfigure}
\caption{...}
\end{figure}
...
In chapter1/tikz/myfigure.tikz
:
\begin{tikzpicture}
\draw (0,0) rectangle (4,4);
\end{tikzpicture}
(I tested this in my OSX system and it works; I use something slightly simpler). This differs from your question in that myfigure-output.pdf
lives in folder chapter1/tikz/
and not in chapter1/
as you seem to want. Perhaps you (or others) can tweak it a bit more.