I want to defined a command that uses another command in its definition, like so:


I understand that csname may be used for that (and also to use numbers in the name ov the command, I believe) - but using csname seems not so easy and unfortunately I don't have the time to familiarize myself with these finer point of TeX and other examples . Could you please help me?

Please not that this command should only be used inside a math environment like so:

Here is a hitherto unknown mathematical object: $\command2+1$. %should display $\alpha^x_{\beta} +1$

And then use $\commandB+1$.

Using numbers in commands is not possible except doing hacks, but it's not a good solution. If you want to define csnames followed by numbers there are already hacks written. Here's a simpler trick, but I don't recommend it.

\newcommand*\docommand{\csname command\the\tmpcount\endcsname}
\expandafter\newcommand\csname command1\endcsname{\alpha^{x}}
\expandafter\newcommand\csname command2\endcsname{\command1_{\beta}}

but it's a bit of a mess, couldn't you use \command{1} rather than \command1? Or just go with \commandA and \commandB.

| improve this answer | |
  • great answer! Could you please let me know , why your trick isn't recommended, is it brittle and may not work anymore for later versions of LaTeX? – l7ll7 Dec 12 '17 at 12:24
  • 2
    Well, numbers were not designed to go into the name of commands, so this is just that, a trick in some sense. Much better if you went with \commandA or \command{10} which would be “standard”. In this case you can go with \command12 but still it feels like an unnecessary hack. – Manuel Dec 12 '17 at 12:26

Another simple way. Here, the numbers are not part of the command's name, but the parameter of \command. It's a bit of a scam.

\[\command1+1+\command2+2\] % More correctly: \[\command{1}+1+\command{2}+2\]

enter image description here

| improve this answer | |
  • 1
    This doesn't work for numbers bigger than 10 (e.g., \command10 wouldn't work) but it's the easiest approach, if OP is willing to use \command{10}. By the way, you could use \ifcase#1\or \alpha^x\or\command{1}_{\beta}\or ...\fi. – Manuel Dec 12 '17 at 12:00
  • hehe, very clever way, if I were to go with smallnumber, +1 – l7ll7 Dec 12 '17 at 12:23
  • @Manuel -- Yes, yes, you are right!!! I was concentrating on cases 1 and 2 only:) The best way the \ifcase!!! – user91669 Dec 12 '17 at 12:35

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.