There's a lot of information buried in the comments so I'm going to try to extract my contributions into some form of answer. My recommended solution is to use pgfkeys
. The key lengths can be short - single letter if really wanted - so the number of key strokes needed to use them is not really that much more than putting the picture name in the node text. Indeed, if you are going to use names that match some pattern (as in the example), there's no need to specify the picture name at all whereupon it isn't necessary to use any key in the actual node commands. Or you could patch into the unknown
key handler so that an unknown key is taken to the the name of a picture. The pgfkeys
method is the most robust, I think, and the most flexible. For example, it will work with matrix of nodes
.
Second in my list of solutions would be Jake's solution of defining an entirely new macro. The only downside to this is that it doesn't work transparently with matrix of nodes
(that is, you can always put the new macro into the cells but you can't take advantage of the automatically inserted nodes).
Way, way down at the bottom of my list is a hack that allows you to actually do what you originally asked: use the node text as the argument to a macro. This is a Very Bad Idea for lots of reasons. Firstly, it involves modifying the TikZ node processing commands directly rather than via hooks. Secondly, the opening bracket of the node text is actually removed before TikZ starts its node machinery (this is what TikZ looks for to know to start processing the text and the test is destructive) so we have to put it back again and this means More Hackery. It is not possible to insert stuff in the begin node/end node hooks for two reasons: Firstly, what appears between those two hooks is not just the node text. It is:
\bgroup%
\aftergroup\unskip%
\ifx\tikz@textcolor\pgfutil@empty%
\else%
\pgfutil@colorlet{.}{\tikz@textcolor}%
\fi%
\pgfsetcolor{.}%
\setbox\tikz@figbox=\box\pgfutil@voidb@x%
\tikz@uninstallcommands%
\aftergroup\tikz@fig@collectresetcolor%
\tikz@halign@check%
\ignorespaces%
followed by the node text. There's even more junk after the node text and before the end hook is called. So if we want to get at the actual node text we need to add something after that \ignorespaces
and it has to have a genuine opening brace.
Here's code illustrating all the above-mentioned solutions.
\documentclass{article}
\usepackage{tikz}
\pgfdeclareimage{firstpicture}{vignettes.pdf}
\pgfdeclareimage{secondpicture}{vignettes.pdf}
\pgfdeclareimage{thirdpicture}{vignettes.pdf}
\pgfdeclareimage{fourthpicture}{vignettes.pdf}
\makeatletter
\newif\if@pgfimagefound
\tikzset{
dummy image with key/.style={
every node/.append style={%
execute at end
node=\phantom{\pgfuseimage{\pgfkeysvalueof{/tikz/pname}}}
},
},
pname/.initial={},
dummy image/.style={
every node/.append style={%
execute at end
node=\phantom{\pgfuseimage{\pgfkeysvalueof{/tikz/pname}}},
/tikz/.unknown/.add code=%
{%
\@ifundefined{pgf@image@\pgfkeyscurrentname!}{\@pgfimagefoundfalse}{%
\let\tikz@key=\pgfkeyscurrentname
\tikzset{pname/.expand once=\tikz@key}%
\@pgfimagefoundtrue
}%
\if@pgfimagefound
\else
}%
{
\fi
}
}
}
}
\let\orig@tikz@do@fig=\tikz@do@fig
\tikzset{
slurp node text/.code={
\def\tikz@do@fig{\orig@tikz@do@fig\expandafter\slurp@node\expandafter{\iffalse}\fi}
},
slurp node/.style={
every node/.append style={
slurp node text
},
slurp node command/.code={#1}
},
}
\def\slurp@node#1{%
\pgfkeys{/tikz/slurp node command={#1}}%
\egroup}
\newcommand\imgnode[2][]{%
\node[#1] {\phantom{\pgfuseimage{#2}}};}
\makeatletter
\begin{document}
\tikzset{every node/.append style={draw}}
\begin{tikzpicture}
\matrix [dummy image with key]
{
\node[pname=firstpicture] {}; & \node[pname=secondpicture] {}; \\
\node[pname=thirdpicture] {}; & \node[pname=fourthpicture] {}; \\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix [dummy image]
{
\node[firstpicture] {}; & \node[secondpicture] {}; \\
\node[thirdpicture] {}; & \node[fourthpicture] {}; \\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix
{
\imgnode{firstpicture} & \imgnode{secondpicture} \\
\imgnode{thirdpicture} & \imgnode{fourthpicture} \\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix [slurp node={\phantom{\pgfuseimage{#1}}}] {
\node {firstpicture}; & \node {secondpicture}; \\
\node {thirdpicture}; & \node {fourthpicture}; \\
};
\end{tikzpicture}
\end{document}
You can't generally load TikZ libraries at begin document.
With a current LaTeX you can do the loading just before begin document:
documentclass[10pt,a4paper]{article}
\makeatletter % this will go in the .sty file
\AddToHook{begindocument/before}{%
\@ifpackageloaded{tikz}{\usetikzlibrary{tikzmark}}{}%
}
\makeatother
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
\draw (0,0) -- (1,1);
\end{tikzpicture}
\end{document}
If you want this to work also with older versions of LaTeX, you need to load etoolbox
and to use \AtEndPreamble
.
\documentclass[10pt,a4paper]{article}
\makeatletter % this will go in the .sty file
\@ifundefined{AddToHook}{%
\RequirePackage{etoolbox}%
\AtEndPreamble{%
\@ifpackageloaded{tikz}{\usetikzlibrary{tikzmark}}{}%
}%
}{%
\AddToHook{begindocument/before}{%
\@ifpackageloaded{tikz}{\usetikzlibrary{tikzmark}}{}%
}%
}
\makeatother
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
\draw (0,0) -- (1,1);
\end{tikzpicture}
\end{document}
Best Answer
If somebody stumbles across this error - for graphdrawing you need
graphdrawing
library (but it should be in the TikZ bundle since 3.0)Loading
tikz
andgraphdrawing
and, if needed, related libraries, such as: