I'm struggling (again) with expansions. This time I have a generic function \misdirection
that creates a TikZ environment subject to a few options. Then, I create a macro \freezeStyle
that creates a new macro that has frozen some of the options to \misdirection
. So \freezeStyle
receives the style that I ultimately want
passed to \misdirection
.
I tried a few \edef
s and couldn't get it working, so I opted to put the code in a state that demonstrates something that compiles but has undesirable behavior.
This attempt was inspired by How to submit a set of tikz command with pgfkeys?.
So: How should I modify this so that it works as desired? Please note that my actual misdirection call will involve \csname
, so I'd appreciate expandafter solutions that also show how to adapt your solution to that case as well (see code for details).
For cross-references, here is a similar question: Macros that write macros and unexpected side effects, with the same type of undesirable behavior, but the solution there has to do with defining customized \csname
s, which doesn't apply to this problem.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\newcommand{\misdirection}[2][]{
\begin{tikzpicture}
\node (tree) at (0,0) {tree};
\node (apple) at (3,0) {#2};
\draw[blue,#1] (tree) -- (apple);
\end{tikzpicture}
}
\pgfkeys{
/tikz/.cd,
execute style/.style = {#1},
execute macro/.style = {execute style/.expand once=#1},
/demo/.cd,
name/.store in=\savedname,
style/.store in=\savedstyle
}
\newcommand{\freezeStyle}[1][]{%
\pgfqkeys{/demo}{#1}
\expandafter\def\csname\savedname\endcsname##1{%
% Incorrect because \savedstyle is not expanded until runtime.
% I want the value of \savedstyle hardcoded into this macro.
\misdirection[execute macro = \savedstyle]{##1}
% This doesn't work either...
%\expandafter\misdirection\expandafter[\savedstyle]{##1}
% In my real code, the misdirection call looks more like:
%\csname someArgumentDependentStuff \endcsname[put style here]{args}
}
}
\begin{document}
\freezeStyle[name=helloMom, style={red,thick}]
\freezeStyle[name=helloDad, style={-{>[length=5mm]},green}]
\helloMom{4} % Undesirably, this is green with a big arrow.
\helloDad{5}
\end{document}
expl3
answer for comparison?