Search code examples
m4

How to escape $ in m4 macro definition's name?


I tried the following code

define(`$TITLE', `my title')
This is my $TITLE.

but the text remains unchanged when I call it with m4.

It seems that is $ problem. Should I escape it?

EDIT:

I found that

define(`$TITLE', `my title')
This is my indir(`$TITLE')

work, but there is a better way to do it?

Also, such macro name is referred as non-standard, where could I find a list of such name?

GNU M4 1.4.18 manual, section 5.1 Defining a macro

As a GNU extension, the first argument to define does not have to be a simple word. It can be any text string, even the empty string. A macro with a non-standard name cannot be invoked in the normal way, as the name is not recognized. It can only be referenced by the builtins indir (see Indir) and defn (see Defn).


Solution

  • Here is the standard. It writes:

    Macro names shall consist of letters, digits, and underscores, where the first character is not a digit. Tokens not of this form shall not be treated as macros.

    I think use of defn would better (instead of indir) because the POSIX standard doesn't contain indir (defn yes) and defn must work on other m4-implementation (not only GNU).

    I think would better in your case if your variables would begin with underscore (not dollar sign): _TITLE. It's valid macro name and maybe doesn't appear in your text :)