I store my exercices in the following way :

  • Super directory Base_Exos on witch points the $TEXINPUTS variable.
  • Sub-directories, one by class level.
  • Sub-Sub-directies, one by chapter
  • Sub-Sub-Sub-directories, one by exercice. This S-S-S-directory contains the text called texte.tex with eventually the correction called corrige.tex and eventually some pictures.

I can use any exercice in a main.tex file witch is far from Base_Exos by :


if this texte.tex contains a picture reference, it is :


Now, I want to use external tikz library and it has still compatible with my Personal Directory System and I don't want to externalize all tikzpicture, just the heavy ones.

That means, that want that the pdf picture produced by the externalization procedure are produced in the same directory than witch the texte.tex file is stored, not in the directory where main.tex is.

So far, I did in main.tex :

My pranbule
\tikzexternalize % activate
\tikzexternaldisable % disable for most pictures

in 3_eme/Droites/exo-002/texte file


But the pdf is produced in the main.tex file directory instead of in the texte.tex one.


  1. How to set the multiple option command to produce the pdf file in the texte.tex directory ?
  2. How to have this pdf files independant from the main.tex file, ie if I include the same texte.tex in two different main.tex, no new pdf file will be produced, then no second loss of time ?

1 Answer 1


I hope I fully understood what you requested. Here goes.

\tikzexternalize accepts a prefix parameter which tells pdflatex where to store the externalised graphics. So even if you use \input in main.tex to include exercises with tikzpictures, you can set prefix=<something> in main.tex to let pdflatex know that externalised graphics should be stored in that directory – specifically, you want them in the directory of the exercise. You can also separate the code from the processing by saving the externalised graphics in a sub-directory where all exercises of a chapter are stored. Here I use external as the directory.

% Macro holding the exercises directory name (in main.tex).
% Macro holding the externalized sub-directory (in main.tex).
% All externalized graphics go go the \externaldirectory
% Externalise only on-demand.

There is a caveat, however: if your main file is not always named main.tex, then the external library will re-externalise graphics with a new filename (I believe the format is \jobname-figureX where X is a running counter starting from 0. This is not what you want; you want to avoid re-externalisation. Thankfully, this issue can be easily circumvented by letting pdflatex know what the externalised graphic should be named using \tikzsetnextfilename:

\tikzsetnextfilename{ex1fig1} % This graphic will always be named ex1fig1.
  % Move along. Nothing to see here.

This means that if this exercise is \input in a file main.tex and a file file.tex compiled with pdflatex -shell-escape, the external library will only create the figure if ex1fig1.pdf does not exist (or if something has changed in the tikzpicture environment etc.)

With all that in mind, your main .tex file would look like this:



% The path to the exercises.
% The path where externalised graphics will be stored.
% Will be used to store the path to the exercise to be processed.

% All auxiliary files and externalised graphics go to \externaldirectory.
% Only externalise on-demand.

% In case you want to include externalised graphics directly.
\graphicspath{ {\externaldirectory} }

% This will combine the \exercisesdirectory and "firstexercise.tex" strings,
% and store them in \pathtoexercise.
\expandafter\def\expandafter\pathtoexercise\expandafter{\exercisesdirectory firstexercise.tex}
% Then we simply input \pathtoexercise.

Where firstexercise.tex is stored in aclass/achapter/exercises/.


This is some text from the first exercise. Below are some cool graphics.

    scale only axis,

    \addplot[red,samples=10] {rnd};


If you want to include only externalised graphics instead of the whole firstexercise.tex, all you need to do is to set the graphicspath in main.tex to the \externaldirectory, like so: \graphicspath{ {\externaldirectory} }. Thereafter, you simply use \includegraphics with the figure filename, like so:

% \includegraphics will automatically search in the graphicspath directory.
\includegraphics{ex1fig1.pdf} % This is actually in aclass/achapter/exercises/external/ex1fig1.pdf
\caption{Exercise $1$, Figure $1$}

I hope this makes at least some sense and that it is in fact what you requested.

  • 1
    Thank you. You perfectly understood what I want, but I didn't explain enought clearly my directory structure. As a I use $TEXINPUT variable, I give the input path to exercice explicitely from the root directory that is in $TEXINPUTS. Then I dont need all the command you use to combine the path. With a newcommand of Input I can set the prefix= and input at once, as prefix= is exactly the directory of the exercice I want. Thank you, with your explanaitions and example I understood better than reading the doc.
    – Tarass
    May 18, 2014 at 18:29
  • @Tarass I'm glad you figured out how to modify it to suite your use-case. Happy to have been of assistance.
    – sudosensei
    May 18, 2014 at 22:36
  • @Tarass Thanks a lot for the huge bounty! It helped me break 1000 reputation points. :-)
    – sudosensei
    May 20, 2014 at 10:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .