# Automatically set file name of externalized graphics equal to the file name of the tikz/PGF file

As the title says, is it possible to set some options for the externalize library in order to for the filename of the externalized graphics to be equal to the filename of the TikZ-file without using \tikzsetnextfilename{} for each and every figure that is to be externalized?

E.g. if I have a file pendulum.tikz, I'd like the externalized output to get the file name pendulum.pdf without having to explicitly specify the name using \tikzsetnextfilename{pendulum}.

Might be that this is specified in the manual, but then I haven't understood how this is done.

Hope someone can help.

Edit:

an "MWE" (which doesn't work as I want it to) with the TikZ-options I use per today.

\documentclass[12pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{circuits.logic.US,circuits.logic.IEC}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/]

\begin{document}

\tikzsetnextfilename{circuit}
\begin{tikzpicture}[circuit logic IEC]
\matrix[column sep=7mm]
{
\node (i0) {0}; & & \\
& \node [and gate] (a1) {}; & \\
\node (i1) {0}; & & \node [or gate] (o) {};\\
& \node [nand gate] (a2) {}; & \\
\node (i2) {1}; & & \\
};
\draw (i0.east) -- ++(right:3mm) |- (a1.input 1);
\draw (i1.east) -- ++(right:3mm) |- (a1.input 2);
\draw (i1.east) -- ++(right:3mm) |- (a2.input 1);
\draw (i2.east) -- ++(right:3mm) |- (a2.input 2);
\draw (a1.output) -- ++(right:3mm) |- (o.input 1);
\draw (a2.output) -- ++(right:3mm) |- (o.input 2);
\draw (o.output) -- ++(right:3mm);
\end{tikzpicture}

\end{document}


Edit #2

I have have something like this in my document that I want to externalize

\begin{figure}[ht]
\input{pendulum.tikz}
\end{figure}
\begin{figure}[ht]
\input{circuit.tikz}
\end{figure}
\begin{figure}[ht]
\input{somethingelse.tikz}
\end{figure}


I'd like the externalized graphics to have the filenames pendulum.pdf, circuit.pdf, and somethingelse.pdf, but without having to do it like this:

\begin{figure}[ht]
tikzsetnextfilename{pendulum}
\input{pendulum.tikz}
\end{figure}
\begin{figure}[ht]
\tikzsetnextfilename{circuit}
\input{circuit.tikz}
\end{figure}
\begin{figure}[ht]
\tikzsetnextfilename{somethingelse}
\input{somethingelse.tikz}
\end{figure}

-
Does \tikzsetnextfilename{\jobname} give the desired result? – Daniel May 29 '12 at 10:56
Unfortunately, no. I get a longer error from the TikZ package. – ritualmagick May 29 '12 at 11:01
Could you clarify the situation a little, perhaps including a MWE? Your phrase "each and every figure" suggests that there is more than one figure in the document, in which case setting the filename to the filename of the TikZ-file will clobber all but the last picture. But maybe the pictures are included in the main file and you want them to have the name of the included file. It's not all that clear what the situation is here. – Loop Space May 29 '12 at 11:02
I've updated the question with an MWE now. – ritualmagick May 29 '12 at 11:08
\newcommand\inputtikzfigure[1]{\begin{figure}[ht]\tikzsetnextfilename{#1}\input‌​{#1.tikz}\end{figure} – Loop Space May 29 '12 at 11:13

Once the .tikz file has been included in your main document using \input, TikZ won't know the name of the file that the code came from. The easiest way to achieve what you're trying to do might be to define a new command like

\newcommand{\includetikz}[1]{%
\tikzsetnextfilename{#1}%
\input{#1.tikz}%
}


Then you can include your images using something like \includetikz{pendulum}.

If you want to keep your input and output files in separate folders, you can simply add the necessary paths to the macro:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

% Define the command. Note that the input and output folders are static!
\newcommand{\includetikz}[1]{%
\tikzsetnextfilename{images_OUT/#1}%
\input{images_INP/#1.tikz}%
}

% Create a test .tikz file in the images_INP folder
\begin{filecontents}{images_INP/circle.tikz}
\begin{tikzpicture}
\end{tikzpicture}
\end{filecontents}

\begin{document}

\includetikz{circle}

\end{document}

-
Thanks! Works exactly as I wanted it to. – ritualmagick May 29 '12 at 11:17
Great idea! BUT: I want to load from a subfolder 'images'. Therein are all my .tikz files. How can your command be modified to get it worked? (I don't want the whole path as filename of the externalized graphic, I yust want (as above) the name of the input-file as my externalized filename) – David K. Nov 16 '12 at 10:20
@DavidK.: I'm not sure I understand correctly: So you want to include the .tikz files from a subfolder, but the generated .pdf files should reside in the same folder as your .tex file? In that case, couldn't you simply change \input{#1.tikz}% to \input{subfolder/#1.tikz}? – Jake Nov 16 '12 at 10:26
@Jake: e.g i run my .tex document on the desktop. Furthermore i've a folder 'images_INP' and a folder 'images_OUT' also on the desktop. Now I want to load a .tikz file from the folder 'images_INP', compile the document and save the externalized pdf into 'images_OUT' I tired with: \newcommand{\includetikz}[1]{% \tikzsetnextfilename{#1}% \input{1-images_INP/#1.tikz}% } ... ... \begin{figure}[ht!] \centering \includetikz{1-images_INP/image1} \caption{so ein toller Würfel} \end{figure} but then i get: LaTeX Error: File 1-images_INP/1-images_INP/image1.tikz' not found. – David K. Nov 16 '12 at 10:32
In case of using tikzscale, the above method fails, a fix would be: \newcommand{\includetikz}[2][]{ \let\oldtikzpicture\tikzpicture \def\tikzpicture{\tikzsetnextfilename{images_OUT/#2}\oldtikzpicture} \includegraphics[#1]{images_INP/#2.tikz} \let\tikzpicture\oldtikzpicture }` – M.Reza Jun 29 '14 at 14:43