In a previous question I asked how to generate multiple saveboxes in a loop. The answer build around csname and the part inside csname is rather long and I need the name several times (create the box, fill the box, get properties and use it). Therefore I would like to shorten this. I tried

\@namedef{saveBox}{<what was inbetween \csname \endcsname>}

which is not working for me. A short background information on the inner workings/general intended use would be a bonus.

The awnser leaves me with one issue, I can't get the \def version to work with a direct version, see example for clarification:

%\expandafter\newsavebox\csname\floatRow@currentSaveBox\endcsname   %works
\expandafter\newsavebox\csname{}floatRow@savebox\alph{floatRow@saveboxCount}\endcsname  %gives mising number error

I assume that my separation of \commandName and the string floatRow@savebox is wrong. Here is a MWE:

  • 4
    I suggest to take a look into the etoolbox package. It provides a much cleaner and more complete interface for the construction of command sequences without using kernel primitives.
    – Daniel
    Commented Jul 31, 2013 at 22:15
  • \@nameuse{saveBox} needs two expansions to give you the name \saveBox (\expandafter\expandafter\expandafter\newsavebox\@nameuse{saveBox}) but that won't work either: \newsavebox will complain: ! LaTeX Error: Command \saveBox already defined.
    – cgnieder
    Commented Jul 31, 2013 at 22:15
  • Maybe you can explain a bit what it really is you want to achieve?
    – cgnieder
    Commented Jul 31, 2013 at 22:16
  • @cgnieder this is my attempt at writing code to automatically solve the issue from here: tex.stackexchange.com/q/125675/19326. For that I need a macro which generates saveboxes for every image on the row and store it there. Then I can calculate the scales needed hoepfully write them to an aux file and apply them in the next pass. This is for the first step, creating and writing stuff to saveboxes. Do you need to see the corresponding code mess (you have been warnder ;) ) or is there a general explanation for the intended use?
    – ted
    Commented Jul 31, 2013 at 22:20
  • @cgnieder: also why does \@nameuse require two expansions?
    – ted
    Commented Jul 31, 2013 at 22:21

1 Answer 1


Let's look at what is happening here. The construct

\@namedef{saveBox}{<what was inbetween \csname \endcsname>}

is defining \saveBox as a macro with replacement text <what was inbetween \csname \endcsname>. That's because \@namedef is defined as

#1->\expandafter\def\csname #1\endcsname

That means that what it does is first convert #1 (saveBox in your case) into a control sequence (\saveBox here), the apply \def. So it's become the same as doing

\def\saveBox{<what was inbetween \csname \endcsname>}

This is a useful-enough thing to do, but in the current case isn't actually what you want!

The second line


is going to fail badly for several reasons, as comments already mention. \@nameuse is a wrapper around \csname ...\endcsname, and so with only one expansion you get to

\newsavebox\csname saveBox\endscsname

which tries to define \csname as a new box!. If we do two expansions we get to


which is still wrong as you've already defined \saveBox as a macro.

Based on the linked question, what you seem to want is more of the form


i.e. to save the form of the name in a macro and reuse that.

You may wonder what \@nameuse is for, as it seems to make things more not less complex (\@namedef is handy). This is at least partly historical: when LaTex2e was written, tokens were very 'expensive', so the saving of \@nameuse (one token) over \csname ...\endcsname (two tokens) was worth making when you look at all of the uses in the kernel. Today, that's much less important, although there is an argument that

\def\foo#1{% Some function

is clearer than

\def\foo#1{% Some function
  \csname my@#1\endcsname

as it shows up that something is being used rather than say defined by name.

Using \@namedef is more common, as it allows easy construction of 'on the fly' names. For example, we can imagine storing some user input:


This might then say be used to save some user information: I'll use e-mail addresses but it could be anything

\mycommand{Joe Bloggs}{[email protected]}

If you have a bigger set up doing this, say storing several pieces of data about several 'keys', then using \@namedef keeps things a lot more readable than using \expandafter\def\csname .... \mycommand{A. N. Other}{[email protected]}

  • Is there a way I can make tex show me the expansions step by step? Also, can you please give an example were \@namedef is usefull?
    – ted
    Commented Aug 1, 2013 at 8:34
  • @ted In your addition to the question, the 'direct' example uses a different counter name to the one in the \def. I suspect that one of these is simply not defined: can you check (or provide a full MWE)?
    – Joseph Wright
    Commented Aug 1, 2013 at 9:27
  • 1
    @ted You can use the trace package to control printing of tracing information, or simply can add \tracingall for a reasonably complete listing. (The trace package prints even more info than \tracingall.)
    – Joseph Wright
    Commented Aug 1, 2013 at 9:33
  • @ted You can't use {} in a \csname to stop space skipping: it's an expansion not a typsetting context, and so the {} remains. You need a space: \expandafter\usebox\csname prefix\alph{c}\endcsname.
    – Joseph Wright
    Commented Aug 1, 2013 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .