GeneralHow to force expansion of a TeX counter

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
demac
Posts: 6
Joined: Thu Jun 17, 2010 9:27 pm

How to force expansion of a TeX counter

Post by demac »

I am trying to set-up code that defines a suite of commands using a \loop and \repeat structure the end results should be

\setNames{ABC}{1}{10} should result in the following commands being defined
\ABCi as ABC--1
\ABCii as ABC--2 and so on through
\ABCx as ABC--10

Code: Select all

\documentclass[12pt]{article}
\usepackage{trace}

\makeatletter
\gdef\make@neName#1#2{\expandafter\newcommand\csname#1\romannumeral#2\endcsname{#1--#2}}}
\gdef\setNames#1#2#3{
    \newcount\n
    \n=#2
   \loop\ifnum\n<#3
        \expandafter\make@neName{#1}{\number\n}
        \advance\n by 1
    \repeat
}
\makeatother

\traceon
\setNames{ABC}{1}{20}
\traceoff

\setNames{ABC}{1}{20}

\begin{document}

This is a test of the well name macro that should set \\
\ABCii,
\ABCiii,
\ABCiv,
\ABCv,
\ABCvi,
\ABCvii,
\ABCviii,
\ABCix,
\ABCxi,
\ABCxii,
\ABCxiii,
\ABCxvi,
\ABCxvi,
\ABCxvii,
\ABCxviii,
\ABCxix,
and ABCxx.
\end{document} 
The problem is that the second argument to \make@neName is always \number\n. So all the commands are actually
\ABCi though \ABCx are all defined as ABC--\number\n which expands to whatever \n is at the time. I will work if I call \make@neName directly (i.e \makeOneName{ABC}{10} gives the correct \ABCx as ABC--10.

Thanks for the help in advance
Jeff
Last edited by demac on Mon Jun 13, 2011 3:29 pm, edited 1 time in total.

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

How to force expansion of a TeX counter

Post by Stefan Kottwitz »

Hi Jeff,

you can force expansion by \edef and \xdef:

Code: Select all

\gdef\make@neName#1#2{\expandafter\edef\csname#1\romannumeral#2\endcsname{#1--#2}}
Stefan
LaTeX.org admin
demac
Posts: 6
Joined: Thu Jun 17, 2010 9:27 pm

Re: How to force expansion of a TeX counter

Post by demac »

Beauty! Simple when you see it! Thanks Stefan :)
Post Reply