# Managing a project: separate .tex-files, externalized TikZ, folders

I am currently cleaning up my project to manage it better as it expands in the future. I am wondering if there is a way to manage the following in a smarter and more efficient way.

Currently, this is my LaTeX structure:

main.tex
\include{chapter_1.tex}
\input{fig_1.tikz}
\input{fig_2.tikz}
\include{chapter_2.tex}
\input{fig_1.tikz}
\include{chapter_3.tikz}


Into main.tex I include chapters like \include{chapter_1.tex}. Inside each chapter, there might be figures generated from TikZ, which I include like \input{fig_1.tikz}.

Now, since I would like to keep my project's directory clean, I wanted to have a folder structure, where I have a folder for images, within which images are separated according to the chapters they belong to, like the following:

main_folder/
|-- main.tex
|-- chapter_1.tex
|-- chapter_2.tex
|-- chapter_3.tex
|-- figures/
|-- chapter_1/
|-- generated_fig_0.pdf
|-- generated_fig_1.pdf
|-- tikz/
|-- fig_1.tikz
|-- fig_2.tikz


and so on. Each tikz would then be input like \input{figures/chapter_1/tikz/fig_1.tikz

Now, the problem arises, when I externalize my figure generation from TikZ to speed up my document compilation.

In my document header I have \tikzexternalize[prefix=figures/] to sort all TikZ images into the figures/ folder. In each included chapter, at the start of the file there is { \tikzset{external/figure name/.add={chapter_1/}{}} with a closing brace } after the chapter's body to add a prefix for putting all generated content into figures/chapter_#/ and keep that limited to the scope of the chapter's body.

However, if I want each generated image to have a meaningful name, I need to add \tikzsetnextfilename{image_name} before each inputted TikZ, such that I would always have blocks like this:

\tikszetnextfilename{foo}
\input{figures/chapter/tikz/foo.tikz}


Is there maybe a straightforward way that if I have a chapter foo.tex, into which I want to input bar.tikz, that some \input{bar.tikz} will automatically look for bar.tikz in figures/foo/tikz/ and create the resulting externalized PDF as figures/foo/bar.pdf?

Would the use of LatexMk do anything to this (I want to use it, but I am not sure what role it could play in the above)?

EDIT: It turns out, that \tikzsetnextfilename actually overwrites all naming definitions except for the initial prefix set with \tikzexternalprefix. Hence, I can't give my figures filenames directly.

Therefore another question: Is there a way to a) save the generated images into a subfolder figures/chapter_name/, and b) give the images invidual names?

-
I do not understand your follow up questions. Perhaps you can explain what in the answer below is not working for you, and I'll try to modify my answer accordingly. – Koji Apr 9 '12 at 19:26
I think your solution is working. :-) I just thought, I'd clarify something wrong in my original question. – mSSM Apr 11 '12 at 6:29

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.

-
It is advisable, as you say, it was just a "quick and dirty" solution. You can, of course, do: \newcommand{\useexternalfile}[1]{\tikzset... – Koji Apr 11 '12 at 8:21
Could you maybe quickly explain what \csname tikzexternal@filenameprefix\endcsname#1.tikz does, especially the part tikzexternal@filenameprefix? – mSSM Apr 11 '12 at 10:14
@mSSM \csname tikzexternal@filenameprefix\endcsname evaluates to \tikzexternal@filenameprefix, but I had to define it this way because you cannot use @ in command names (you can simplify a bit this). Now, \tikzexternal@filenameprefix is the internal macro where tikz saves the prefix that you set with \tikzsetexternalprefix. So, \useexternalfile{<name>} becomes \tikzsetnextfilename{<name>-output}\input{<saved-prefix>/<name>.tikz – Koji Apr 11 '12 at 10:43
@mSSM: added a cleaner definition with your suggestions. – Koji Apr 11 '12 at 10:47