Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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]{%
    \addtocounter{mycounter}{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?

share|improve this question
1  
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

1 Answer 1

up vote 9 down vote accepted

You could do

\documentclass{article}

\newcounter{mycounter}
\newcommand{\mycommand}[1]{%
    \addtocounter{mycounter}{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)

share|improve this answer
    
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

Your Answer

 
discard

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.