TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to add an index to a command? For example:

  \def\var\Roman{count}{some data}

The \var\Roman{count} should become the variable \varI, \varII, \varIII and so on.

Background: I need to get some data from external files and store it into variables (which I will later use to create a document). Normally every \varI should be the same as every \varII, but I need to check whether it really is the case or not. If not I should return a warning or error.

Solution for the first problem is in the comments, but I have a second problem.

Suppose I wrote this:

\newcommand\addindex[3]{  % #1 as name of var, #2 as index and #3 as content
  \expandafter\def\csname #1\Roman{#2}\endcsname{#3}  % thanks to egreg


All \var<index> have now as value 5, because they are defined as \arabic{count}, and not their value at the moment. Adding \expandafter before \arabic{count} does not seem to work for some reason...

share|improve this question
\@namedef{var\romannumeral\c@count}{some data} – David Carlisle Jul 20 '12 at 11:59
Or \expandafter\def\csname var\Roman{count}\endcsname{...} – egreg Jul 20 '12 at 12:50
Thanks again egreg, I'm starting to like you :p @DavidCarlisle: thanks, but in that way I have to use TeX counters instead of LaTeX counters. I prefer the LaTeX way :) – Didii Jul 20 '12 at 13:12
Also, is there a way to read them again using another loop? I can't seem to find out how for the moment... – Didii Jul 20 '12 at 13:12
Figured it out, I could just use the \csname environment again :) – Didii Jul 20 '12 at 13:23
up vote 6 down vote accepted

The method is quite simple, but I add a trick that can be useful in your application:

\newcounter{tempcount} % for temporary usage

\newcommand\addindex[4][]{% #2 as name of var, #3 as index and #4 as content
  \csname#1def\expandafter\endcsname\csname #2\Roman{#3}\endcsname{#4}%

Now we want to define \varI, \varII, \varIII and \varIV to expand to some text:


  \addindex{var}{tempcount}{some text}

If instead you want that \varI expands to 1 and so on, here's the role of the optional argument:



By passing e as the optional argument, what's executed is \csname edef\endcsname, that is \edef that expands completely the replacement text before doing the definition. With no optional argument only \def is used.

A more efficient definition would be of \vari, \varii and so on (with lowercase roman numbers):

\newcommand\addindex[4][]{% #2 as name of var, #3 as index and #4 as content
  \csname#1def\expandafter\endcsname\csname #2\roman{#3}\endcsname{#4}%

It's more efficient because \Roman{tempcount} has to scan each character in the expansion of \roman{tempcount} and change it to its uppercase counterpart, while \roman{tempcount} expands to \romannumeral\c@tempcount using the TeX primitive \romannumeral, which is quite fast.

share|improve this answer
Why is the \expandafter needed? I'm currently searching what it exactly does, but can't seem to find it easily.. – Didii Jul 23 '12 at 9:54
The \expandafter last before \endcsname is necessary so that the following \csname is processed before TeX forms the token \def or \edef. So when the counter's value is 1, for instance, what TeX will see is \def\varI. Without the \expandafter it would see \def\csname varI\endcsname which is wrong. – egreg Jul 23 '12 at 10:55
I finally found a question which answers that :). Thanks. – Didii Jul 24 '12 at 9:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.