0

I regularly use the pattern

\begin{figure}
\centering
\tikzsetnextfilename{<SOME-NAME>}
    \begin{tikzpicture}
        \subimport{.}{figures/<SOME-NAME>.tex}
    \end{tikzpicture}
\label{fig:<SOME-NAME>}
\end{figure}

It so happens, that I forget to change the label fig:<SOME-NAME> when copy-pasting this code around. That's why I wondered, if there is the possibility to retrieve the value set with \tikzsetnextfilename so that

\label{fig:<SOME-NAME>}

could be changed to

\label{fig:\tikzgetnextfilename}

For a different use-case I would also like to get the value of \tikzsetexternalprefix for the import command.

EDIT: I extended my use-case in the sense that I would like to also use the variable to import the correct file.

EDIT2: I ended up using

\providecommand{\tikzfile}{}

...

\begin{figure}                                         
    \renewcommand{\tikzfile}{<SOME-NAME>}       
    \tikzsetnextfilename{\tikzfile}                    
    \centering                                         
    \begin{tikzpicture}                                
        \subimport{.}{figures/\tikzfile.tex}           
    \end{tikzpicture}%                                 
    \caption{This is the figure with name \tikzfile.}% 
    \label{fig:\tikzfile}%                             
\end{figure}                                           

1 Answer 1

1

The macro \tikzsetnextfilename is very simple, it is defined as

\gdef\tikzexternal@nextfile{#1}

meaning that the filename will be stored in the (global) macro \tikzexternal@nextfile. You can use this macro for your labels, however it contains an @ symbol (indicating that it is an internal macro) which means you can only use it in your own code if you switch off the special meaning of @ temporarily with the \makeatletter and \makeatother macros. This is very inconvenient for the current use case, therefore it is easier to change the original definition in order to create two macros, the original \tikzexternal@nextfile with the @ symbol and another custom macro without the @ symbol that can be used in \label.

MWE, based on the TikZ manual:

\documentclass{article}
% main document, called main.tex
\usepackage{tikz}

\usetikzlibrary{external}
\makeatletter
\def\tikzsetnextfilename#1{\gdef\tikzexternal@nextfile{#1}\gdef\mytikznext{#1}}
\makeatother
\tikzexternalize[prefix=tikzext/] % activate
\begin{document}
\begin{figure}
\centering
\tikzsetnextfilename{trees}
\begin{tikzpicture}
\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
\caption{A tree}
\label{fig:\mytikznext}
\end{figure}

\begin{figure}
\centering
\tikzsetnextfilename{gridlines}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
\end{tikzpicture}
\caption{A grid}
\label{fig:\mytikznext}
\end{figure}

\centering See Figure \ref{fig:trees} and \ref{fig:gridlines}.
\end{document}

Result:

enter image description here

2
  • Ty for your answer. Based on it I thought the following two things should work. But neither did: 1.) print out the desired value to the document with \makeatletter\the\tikzexternal@nextfile\makeatother 2.) define my own setter and getter with \newcommand{\settizkfile}[1]{\tikzsetnextfilename{#1}\renewcommand{\gettikzfile{#1}} Can you explain why both ideas are not working?
    – Franz
    Jan 9, 2020 at 7:37
  • For the first: 1. you don't need \the and 2. the value is reset at some point by TikZ, probably by \end{tikzpicture}, so you cannot use the macro after that. For the second: you forgot a } after \gettikzfile (the syntax is \renewcommand{command name}{definition}, so here \renewcommand{\gettikzfile}{#1}. Moreover in order to use \renewcommand you need to have \newcommand somewhere before that, so \newcommand{\gettikzfile}{\relax} \newcommand{\settikzfile[1]{...} works.
    – Marijn
    Jan 9, 2020 at 15:09

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.