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

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}