Document Classes ⇒ Command name concatenation
Command name concatenation
How can I concatenate a base name of my command with something returned by counter?
These are defined commands (theoretically may be an infinite set):
\basenamei
\basenameii
\basenameiii
I'd like to call one of them depending on what is returned by \roman{counter}.
How can I do it?
These are defined commands (theoretically may be an infinite set):
\basenamei
\basenameii
\basenameiii
I'd like to call one of them depending on what is returned by \roman{counter}.
How can I do it?
NEW: TikZ book now 40% off at Amazon.com for a short time.

Command name concatenation
Hi,
you can use conditionals. The following example illustrates a simple use of the \ifnum TeX primitive to redefine the command \example depending on the value of the counter mycount.
Cahpter 13, Conditionals, of the book TeX by topic (by Victor Eijkhout) contains a useful description of the various conditionals in TeX.
you can use conditionals. The following example illustrates a simple use of the \ifnum TeX primitive to redefine the command \example depending on the value of the counter mycount.
Code: Select all
\documentclass{article}
\newcounter{mycount}
\newcommand\namei{a}
\newcommand\nameii{b}
\newcommand\nameiii{c}
\newcommand\example{}
\renewcommand\example{%
\ifnum \value{mycount}=1
\namei\else
\ifnum \value{mycount}=2
\nameii\else
\ifnum \value{mycount}=3
\nameiii
\else\relax
\fi\fi\fi}
\begin{document}
\setcounter{mycount}{1}
\example
\setcounter{mycount}{2}
\example
\setcounter{mycount}{3}
\example
\setcounter{mycount}{5}
\example
\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Re: Command name concatenation
Oh boy, READ CAREFULLY, if that was so simple, I wouldn't bother u. 
I wrote that a set of commands theoretically can be infinite.
You solved problem for my simple example. What if there will be 200 of such defined commands?
I can't know that number! That is why I need to concatenate command name with string returned by \roman{counter}.

I wrote that a set of commands theoretically can be infinite.
You solved problem for my simple example. What if there will be 200 of such defined commands?

Re: Command name concatenation
No need to get snippy.
I could be wrong, but I don't think it is possible to use variables of any sort in new command declarations, but likely what you're trying to accomplish can be done in a slightly different way. Can you elaborate on exactly what you're trying to do?
I could be wrong, but I don't think it is possible to use variables of any sort in new command declarations, but likely what you're trying to accomplish can be done in a slightly different way. Can you elaborate on exactly what you're trying to do?
-
- Site Moderator
- Posts: 814
- Joined: Tue Jul 01, 2008 2:19 pm
Command name concatenation
What's wrong with or in a code block with \makeatletter active
Code: Select all
\csname basename\roman{counter}\endcsname
Code: Select all
\@nameuse{basename\roman{counter}}
Joseph Wright
Command name concatenation
It is possible. I create "dynamically" commands this way:frabjous wrote: I could be wrong, but I don't think it is possible to use variables of any sort in new command declarations (...)
Code: Select all
\newcommand{\addnextcommand}[3]{
\stepcounter{counter}
\expandafter\newcommand\expandafter{\csname basename\roman{counter}\endcsname}{#1-#2 #3}
}
There's nothing wrong with that. That is what I was looking for. Wow! That's simple. How could I miss that? Thank you. You were very helpful.josephwright wrote:What's wrong withCode: Select all
\csname basename\roman{counter}\endcsname