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:


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.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:


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
  • 1
    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:

\def\useexternalfile#1{\tikzsetnextfilename{#1-output}\input{\csname tikzexternal@filenameprefix\endcsname#1.tikz}}




EDIT: A cleaner, more LaTeX-y definition of \useexternalfile would be


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?)


In chapter1.tex:


In chapter1/tikz/myfigure.tikz:

\draw (0,0) rectangle (4,4);

(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.

  • 1
    Thanks for your answer. Why would you use a TeX' \def command rather than a \newcommand command in your definition? I thought it is always advisable to use \newcommand (or the version with an asterix) for the additional error checking it provides. – mSSM Apr 11 '12 at 7:48
  • 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
  • 1
    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

I have written a VB.NET application that process multiple Tikz files and generates PDF based on a given preamble (options for the Tikz figures).

Can be downloaded from my web page:


I also included a sample project.

VB.NET project is open source and can be downloaded from


I have written it very quickly, so there are probably bugs.

Needs .NET Framework 4.7, TeX system installed and running properly.

May be helpful till you master the Externalization Library of Tikz.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.