There are two commands, one of which forms a token of kind "token-kind@number-of-section@number-of-this-kind-token". With that the name of token kind string and the corresponding counter name are passed as parameters to the this command. The second command produces a command name with the leading backslash and name itself coinciding with token of the same format, which is produced by the first command. Again, this command has two parameters of the same meaning as the first command. Now I define them as follows:

\def\makeCommandName#1#2{\csname #1@\arabic{section}@\arabic{#2}\endcsname}

But the problem if I would like to change the format of the token, then I should change it in both commands \commandNameToken and \makeCommandName. However, I haven't won the TeX expansion mechanism in such a way that the command \makeCommandName uses the command \commandNameToken to get the token (to change the token format only in \commandNameToken).

Can any TeXpert advice the format of the command \makeCommandName referencing to the command \commandNameToken? :)

  • \def\makeCommandName#1#2{\csname\commandNameToken#1#2\endcsname} should do the trick, since \csname ... \endcsname fully expands what is inside (after the first expansion step, you already have the same state as with your current definition of \makeCommandName). I'm not sure you use the word “token” properly, and not very clear about the usefulness of the first macro either. (?) – frougon Jul 26 at 7:32
  • Actually, if you later redefine \commandNameToken, the \makeCommandName from my previous comment will use the definition of \commandNameToken that is current at the point where the \csname resulting from the expansion of \makeCommandName is expanded. If you want the definition of \makeCommandName to use a “snapshot” of the meaning of \commandNameToken when \makeCommandName is defined, you can use: \edef\makeCommandName#1#2{\expandafter\unexpanded\expandafter{\expandafter\csname\commandNameToken#1#2\endcsname}} (all this is untested because you provided no MWE). – frougon Jul 26 at 7:51
  • 1
    Just complementing frougon's comments, you should add braces around #1 and #2 in case the arguments consist of more than one TeX token: \def\makeCommandName#1#2{\csname\commandNameToken#1#2\endcsname} or \edef\makeCommandName#1#2{\unexpanded\expandafter{\expandafter\csname\commandNameToken#1#2\endcsname}}. – Phelype Oleinik Jul 26 at 14:00
  • 1
    @frougon most (if not all) TeX primitives whose argument is <general text> expand the tokens before the {, so you can use \unexpanded\expandafter{<stuff to expand>} saving you one \expandafter before the \unexpanded. – Phelype Oleinik Jul 26 at 14:03
  • @PhelypeOleinik You are quite right, thanks! The forgotten braces are a stupid mistake... As for the unneeded \expandafter before \unexpanded, I “knew” about that but forgot to apply this still-fragile knowledge—I guess I haven't used it enough yet. I've incorporated your fine suggestions in the answer, of course. – frougon Jul 26 at 16:17

Taking into account Phelype Oleinik's fine comments, you can use:


if you want \makeCommandName to rely on the meaning \commandNameToken will have at the point where the \csname resulting from the expansion of \makeCommandName is expanded. On the other hand, if you want the definition of \makeCommandName to contain a “snapshot” of \commandNameToken's meaning at the point where \makeCommandName is defined, you can proceed as follows:


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.