GeneralCreating commands from list of names

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
chris
Posts: 1
Joined: Sun Feb 14, 2010 1:40 am

Creating commands from list of names

Post by chris »

Hi there,

I've run into a little problem.

I have a list of command names that I am trying to read in from an input file. I would then like LaTeX to generate a command from the list of names to set a counter.

I have been trying the code below but it has a problem in that it is writing the command in such a way as it will always set the last counter, rather than the current one in the for loop.

I have tried for sometime to work out what is going on without any success and was wondering if anyone has any ideas.

Thanks in advance.

Cheers
Chris

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{forloop}

\newcounter{counter}
\setcounter{counter}{0}

\newcommand{\setName}[1]{%
  \addtocounter{counter}{1}
  \expandafter\newcommand\expandafter{\csname Name\roman{counter}\endcsname}{#1}
  \newcounter{anotherCount\roman{counter}}
  \setcounter{anotherCount\roman{counter}}{100}
}

\newcounter{ct}

\newcommand{\createCommands}{%
  \forloop{ct}{1}{\value{ct}<\value{counter}}{%
    \expandafter\newcommand\expandafter{\csname \expandafter\csname Name\roman{ct}\endcsname\noexpand\endcsname}[1]{%
      \setcounter{anotherCount\roman{ct}}{##1}
    }
  }
}

\begin{document}

\setName{Foo}
\setName{Bar}
\setName{FooBar}
\setName{Blank}

\createCommands

\Foo{1}
\Bar{2}
\FooBar{3}

\theanotherCounti \\
\theanotherCountii \\
\theanotherCountiii \\
\theanotherCountiv

\end{document}
Expected Output

1
2
3
100

Actual Output

100
100
100
3

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Post Reply