Document Classes ⇒ Command name concatenation
Command name concatenation
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?
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
Command name concatenation
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}
Re: Command name concatenation

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
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
Code: Select all
\csname basename\roman{counter}\endcsname
Code: Select all
\@nameuse{basename\roman{counter}}
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