26

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}
5
  • 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.
    – blubbafett
    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. May 29 '12 at 11:02
  • I've updated the question with an MWE now.
    – blubbafett
    May 29 '12 at 11:08
  • 1
    \newcommand\inputtikzfigure[1]{\begin{figure}[ht]\tikzsetnextfilename{#1}\input{#1.tikz}\end{figure} May 29 '12 at 11:13
22

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}
6
  • 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
  • @DavidK.: Well, if you put 1-images_INP into the macro definition, you shouldn't use it again when you call \includetikz. Are you looking for a solution where the input and output folders are static, i.e. you only define them when you're defining the macro, or do you want to be able to set the paths when you're calling \includetikz? I've edited the answer to include a way of achieving the former.
    – Jake
    Nov 16 '12 at 10:41
  • 1
    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
1

Quite old thread but I want to add a solution when dealing with tikz files saved in different subfolders (as @David K. in his comment to the approved answer mentions on Nov 16 '12 at 11:18):

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

A MWE would be (just taken from the approved answer and modified):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=./images_OUT/]

% Define the command. Note that input folder is NOT STATIC ANYMORE!
\newcommand{\inputtikz}[2]{%
    \tikzsetnextfilename{#2}%
    \input{#1#2.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}

\inputtikz{images_INP/}{circle}

\end{document}

This way, circle.pdf is saved to the output folder './images_OUT'. You have to specify the output path in the command call though.

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.