# Savebox equivalent of \csname?

I would like to define a command which would increment a counter, and create a new savebox whose name is generated from the counter value. Something like:

\documentclass{article}

\newcounter{mycounter}
\newcommand{\mycommand}[1]{%
\newsavebox{\csname foobox\roman{mycounter}\endcsname}%
\savebox{\csname foobox\roman{mycounter}\endcsname}{#1}%
}

\begin{document}

\mycommand{This is the content of fooboxi.}
\mycommand{This is the content of fooboxii.}
\usebox{\fooboxi}
\usebox{\fooboxii}

\end{document}


My Google-fu has been too weak for me to find a way to code this. Any pointers?

• Are you sure that you want to allocate a box for each call of \mycommand? Also, how can you know what box contains what, after a long string of \mycommand statements? An example of real usage may give ideas for a more efficient way to do the job. – egreg Sep 19 '12 at 6:53

You could do

\documentclass{article}

\newcounter{mycounter}
\newcommand{\mycommand}[1]{%
\expandafter\newsavebox\csname foobox\roman{mycounter}\endcsname
\expandafter\savebox\csname foobox\roman{mycounter}\endcsname{#1}%
}

\begin{document}

\mycommand{This is the content of fooboxi.}
\mycommand{This is the content of fooboxii.}
\usebox{\fooboxi}
\usebox{\fooboxii}

\end{document}


Although generally, you shouldn't. The usual mode is to only allocate a few registers and re-use them as much as possible. Classic TeX only has 256 box registers, and the latex kernel uses up half of them before you start. (e-tex has thousands more, but the general principle remains)

• I understand that this is not best practice, but at least I have something to go on until I eventually learn my way around registers. Thanks a bunch. – Mathieu Sep 18 '12 at 23:23
• adding \usepackage{etex} will make the \newsavebox allocation scheme give you 32768 registers rather than 256 should you ignore my advice and do this:-) – David Carlisle Sep 18 '12 at 23:25
• By default, should I expect XeTeX to behave like e-TeX or classic TeX? – Mathieu Sep 18 '12 at 23:34
• well like pdftex it'll default to having the etex extensions turned on (so the registers will be there) but the latex \newsavebox just calls \newbox which is the original latex2e code and more or less the same as the original plain tex code so it stops at 256 anyway. So you'll still need the etex package or equivalent. – David Carlisle Sep 18 '12 at 23:43