I am in the process of writing commands to typeset chord tabulatures for guitar using TikZ. Because for every chord, there are two equivalent (enharmonic) names, I want to avoid redundancy when defining them, so I want to write a macro which produces slightly different code for every one of them. However, contrary to this question, since the typeset chord name needs to be included in the TikZ code, I need to handle an additional argument which can vary for each version of the command name.
Since I use TikZ anyway, my approach was to use a two-variable \foreach loop and a kind of anaphoric macro \newchord (BTW, I find it interesting that this term is apparently not common for TeX), which uses a \chordname command that should be expanded to the respective name in each definition:
\documentclass{article}
\usepackage{tikz}
\newcommand{\newchord}[2]{%
\foreach \command / \name in {#1} {%
\expandafter\gdef\csname \command\endcsname{%
\def\chordname{\name}
#2
}
}
}
% this is how I want to use the macro:
\newchord{ASharp/{A\(\sharp\)}, BFlat/{B\(\flat\)}}{%
% here, actually some TikZ drawing will happen
I am: \chordname
}
\begin{document}
\ASharp % -> I am: A♯
\BFlat % -> I am: B♭
\end{document}
This example fails with
! Undefined control sequence.
\chordname ->\name
l.20 \ASharp
I suspect that is because \chordname is expanded too early, but honestly, I don't really know much about expansion, anyway...
I wouldn't mind a solution not using \foreach, this was just what I found the easiest to start with. Also the way of using \chordname was just an idea -- as long as a solution works similar to my usage example, it's OK (but it would be interesting to get it work). The slash as separator is also not significant, it is just what \foreach requires to work.


expl3features – Feb 24 '17 at 17:50