I have made my own lists. I want to refer to them outside the lists. If I do a calculation using counters at a certain point in my list hierarchy, say \alph{listcounter}, how do I freeze that result at the point I'm calculating it so that when I refer to it later it has the original calculated value. LaTeX delays the calculation of \alph{listcounter} until I eventually put it onto a page at which point the listcounter value is inappropriate.

I am determined not to use a readymade list package and really need a solution to this problem for general use. What I'm after is a way to freeze the result into a variable, and not the calculation that produces it.

I can freeze strings like this in a file and that's overkill I think, but it shows the string can be frozen. The string I'm using has no formatting as that was creating all kinds of bother as well. I realise that generalising this routine for multiple references is going to be a nightmare all its own but the following code is deliberately trivial to demonstrate the problem.

What is the usual way of saving a snapshot of the value of an expression?

I'm a newbie here but only a seminewbie with LaTeX. I've had a great deal of help from this site in the past. Much appreciated.

Addendum 1: Okay, now that seems to be solved in a basic way, I'll move this to the next level of complication. I need flexibility in putting these strings together as I want the list levels used in various combinations. I know how to keep track of which level is which so I'll just focus on the next reference string problem. Using loops to construct a string breaks the \xdef solutions as provided. The following loop works on its own but crashes inside the \xdef.

Addendum2: Okay, I'm clearly not asking too much because I solved this myself. It dawned on me that I could just append stuff to the original on the fly variable so nothing nasty had to be passed to it directly.

@egreg - Your original solution worked with simple expressions that include things like '\alph{counter}' but when using '\loop .. \if .. \repeat' it breaks. Thanks for your help so far, is this further complication too much?
–
Geoff PointerSep 23 '13 at 3:52

@egreg - I've solved this latter complication. See my original post Addendum 2. I knew there had to be a way and I don't normally hang about waiting for other people to solves things for me. Thanks for your input it pushed me in the right direction. BTW, I was already using code for constructing variables with common prefixes, which I found on this website of course. (I'm also going to have to watch not hitting the return key when it's only comment.) - Cheers.
–
Geoff PointerSep 23 '13 at 5:15

I found it difficult to chose an answer. Joseph Wright's answer was fundamentally the same as egreg's and the main point of the problem. Ultimately a good student can code the details. None the less, egreg serendipitously incorporated command family construction, which I'd been working on already, so the extra coding in his answer tied in nicely. Thanks to Joseph for highlighting the \protected aspect and he did give a more detailed explanation of why \xdef is necessary. Of course, a good student will look up the details once pointed in the right direction. Thanks to both of you.
–
Geoff PointerSep 23 '13 at 9:31

You have a not uncommon misunderstanding going on here: \global\def\varstring{\makeref} defines \varstring to expand to \makeref not the value of \makeref. So when you use \varstring you are inserting \makeref, which then expands to

Q\arabic{numqpoints}(\alph{numapoints})

and so you get whatever the current value of the the counters is.

Instead, you need to use an expanded definition, so \varstring contains the current appearance of \makeref. We could use the TeX \xdef primitive for this, but in general you should not use TeX primitive expansion with LaTeX material. Instead, I would use \protected@xdef:

That works because LaTeX then keeps expanding \makeref until only non-expandable things (numbers, letters, ...) remain.

I would probably consider saving only the current values of the counters, as you may want to do stuff other than use the formatted version. However, that is down to you!

Note on the difference between \xdef and \protected@xdef. First, these are global versions of \edef and \protected@edef, respectively, so the comments here apply to both cases. The TeX primitives \edef/\xdef keep expanding everything inside a definition until they reach 'unexpandable' material. At the primitive level, that makes sense but with LaTeX commands can go wrong. Not everything 'makes sense' fully expanded, and indeed can lead to errors as in particular assignments are not carried out during expansion. As such, LaTeX provides \protected@edef/\protected@xdef, which are set up such that various LaTeX commands are 'made safe' and don't get expanded. Normally, it's regarded as best to use the \protected@edef/\protected@xdef for expanding LaTeX material, with \edef/\xdef really only safe if you are in 'full control' of the input.

In the current case, the display counters could be altered from the standard, expandable, \alph, so I'm being cautious.

To be fair I don't have this misunderstanding. I explained in my original question that I understood the delayed evaluation thing. I just didn't have the solution, although I'd found a hack that clarified it for me. Your solution worked when I tried it. Both you and egreg have demonstrated an application of \xdef, but why have you used \protected as well?
–
Geoff PointerSep 23 '13 at 3:29