TransWikia.com

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

TeX - LaTeX Asked by blubbafett on August 24, 2020

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}

2 Answers

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}

Correct answer by Jake on August 24, 2020

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.

Answered by PaoloPinkel on August 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP