I try to write a setcounter command to a file (using the newfile package) by something like the following


However, since the whole thing is protected, \myvariable isn't expanded and since it's value changes later in the document, the wrong value ends up in my counter. So, how can I 'unprotect' the variable here?

---EDIT--- Here's the minimal example:







It outputs a file called stream.file with the content


but I want

  • Can you please add a minimal example from \documentclass to \end{document}? As far as I know, macros are expanded, which is the reason why \setcounter needs \protect.
    – egreg
    Mar 13, 2013 at 19:03
  • @egreg I added a minimal example. I hope it is more clear now.
    – grackkle
    Mar 13, 2013 at 22:52
  • You have to say \themycounter or \arabic{mycounter} in the \addtostream command.
    – egreg
    Mar 13, 2013 at 22:52

2 Answers 2


If othercounter and mycounter are LaTeX counters, you can't say


but, in general, you should say


However, for your application you need a different way, because you need the actual value of mycounter, not referring to the value at the time the assignment is performed. Thus


is what you're looking for. The write operation will expand \arabic{mycounter} to the current decimal representation of the counter's value.


\protect only protects the next token, not the arguments of the next token. If \myvariable is not expanded, then it is perhaps not expandable (e.g. defined by \newcount\myvariable. Then \number helps:


You must log in to answer this question.

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