[Tex/LaTex] Automatically set file name of externalized graphics equal to the file name of the tikz/PGF file

tikz-externaltikz-pgf

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}

Best Answer

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}
\fill [orange] circle [radius=3cm];
\end{tikzpicture}
\end{filecontents}

\begin{document}

\includetikz{circle}

\end{document}