I'm having some trouble fixing a macro I wrote. I believe that it has to do with \let, \def, and \edef. I have gone through:
What is the difference between \let and \edef?
but I still can't seem to sort out what I'm misunderstanding. The code is a bit weird, but it is the simplest example I could come up with that resembles my actual code.
What I would like:
- Teach me what I'm doing wrong and how to fix it. In particular, my (obviously wrong) understanding of
\letled me to believe that this should have worked. I tried playing with\defand\edefwithout success. - If the example is clear enough to indicate that I am going about this completely wrong, please suggest a better way of writing such a macro. (Though I'm a bit concerned that since I've simplified this so much, the suggested rewrites won't be flexible enough or will miss my actual use case.)
Code:
\documentclass{minimal}
\usepackage{etoolbox}
\makeatletter
% in actual code these have multiple required arguments
\def\myobjectXA#1{XA:#1}
\def\myobjectYA#1{YA:#1}
\def\myobjectXB#1{XB:#1}
\def\myobjectYB#1{YB:#1}
\newcommand{\makefunc}[3]{%
\ifstrequal{#1}{A}
{%
\let\myobjectX\myobjectXA
\let\myobjectY\myobjectYA
}%
{%
\let\myobjectX\myobjectXB
\let\myobjectY\myobjectYB
}
\expandafter\def\csname #2\endcsname##1{%
\ifstrequal{##1}{X}%
{\def\mytemp{\myobjectX{#3}}}
{\def\mytemp{\myobjectY{#3}}}
% In actual code \mytemp is is sent to another macro, which then
% adds additional required arguments. This is why I think using
% \let is not an option. Using \edef also didn't work for me.
\mytemp
}
}
\makeatother
\begin{document}
\setlength{\parindent}{0pt}
\makefunc{A}{hello}{1}
\makefunc{B}{goodbye}{2}
\hello{X}\\ % desired: XA:1 actual: XB:1
\hello{Y}\\ % desired: YA:1 actual: YB:1
\goodbye{X}\\ % XB:2
\goodbye{Y} % YB:2
\end{document}
Here is some additional information in response to the comments:
Recent engines are all I care about in terms of compatibility.
What's motivating this example is that I have two families X and Y that represent whether you want harpoons or arrows on top of a symbol. Within the families, there are 3 options A,B,C that specify whether it is left pointing, right pointing, or leftright pointing. So I'm making a macro that will let people customize the base object and the family of decorators. I'm expecting people will write it like so:
\makemacro{arrow}{Qaz}{Q}
\makemacro{harpoon}{Qwe}{W}
\Qwe(>) % W with right-pointing harpoon
\Qaz(<) % Q with left-pointing arrow
\Qaz(<)[0][5] % Q with left-pointing arrow and subscript 0:5
\Qaz(<)[][5] % Q with left-pointing arrow and subscript :5
\Qaz(<)[0][] % Q with left-pointing arrow and subscript 0:
\Qaz(<)[0] % Q with left-pointing arrow and subscript 0
So it's not strictly necessary that I use a string comparison, but I figured the readability was nice. Probably I could use xkeyval or something similar instead. The two \ifstrequal checks in the demo code were quick choices for selecting the family (arrow or harpoon) at macro definition time, and for selecting the pointing direction at "runtime". Anyway, hopefully that provides enough information to figure out how I could use \edef properly.
