Is it possible to define a m4 macro (without arguments), which expands to 1 on first invocation, expands to 2 on second invocation, and so on? In other words, it should have internal memory storing the number of times it is invoked. Can this be done?


You can do that by having two macros, a counter holding the current value, and a count macro that expands to the value and redefines `counter'. For example, it could look like this


When the count macro is used, it firstly redefines counter to hold its next value (adding 1 to its present value), and then it uses that value.

I'm not immediately sure how to do this with a single macro, and if that's an important aspect of your problem then this is not the answer.

  • Thanks Ralph. This works just as I intended to, so long as I use it (with appropriate quotes and evals) within a macro which is defined directly. It provides an invocation number for the called macro. But I also have a macro-defining macro whose purpose is to define a macro named $1 having expansion $2 (and do other processing to the expansion), and I am having trouble to get it to work in this indirect situation. – adgadg May 4 '16 at 2:43
  • You mean like using your own mydef instead of define in the above, where the mydef' would include like define($1,$2)` in its expansion? or is it more like including define($1,F($2)) where F does things to its argument? Actually if you can add an explicit example of the essentials of your `mydef' to your question... – Ralph Rönnquist May 4 '16 at 4:26
  • It is like my own mydef as in define(mydef, define($1, F($2))), which is then used to define more macros using the form mydef(mymacroname, stuff) (I am omitting quotes). Your method should still work, so my guess is that I am probably messing up some nested quoting. I will try some more, and if I am unable to fix it myself, I will ask again with an explicit example added to my question. Thanks for your help. – adgadg May 4 '16 at 5:20
  • I was able to fix my problem -- Ralph's answer works in the indirect situation as well. Thanks. (If any one is interested, I can post the code in my question above.) – adgadg May 5 '16 at 3:38
  • Thanks for this. Very useful. I expanded a bit on how I use this technique in Markdown gist.github.com/alecthegeek/737cb92a4a45cdbbda6329c977f31bd5 – Alec the Geek Dec 9 '18 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.