Hi again,
So, I can now do this:
\newcommand{\createDynamicCmd}[1]{\newcommand{\fn#1}{cmdName: #1}}
\createDynamicCmd{Name}
\fnName

with
\fnName displaying as 'cmdName: Name' in the document

This makes me very happy, even if nobody else cares. The code is
at the bottom of this message. A big difficiency is that it doesn't
check that parsing the name will definitely only result in a string, 
though this is easy enough to enforce. 
I am now going to make it so you can edit macro names after they are
created. I have read some previous comments that this can't be done,
but am not clear as to why. Anybody have any comments about this?
Also, at the moment, you need to type in the first line above, select it
& convert it to a macro. The reason is that you can't create macros
within macros through the GUI. Is there any reason for this restriction?

cheers,
andrew

modify Parser::parse1() in math_parser.C
...
} else { // t.cs() == "newcommand"
        
   name = parse_verbatim_item();
   if (name.length() == 0)
      error("Invalid name in \\newcommand \n");
                
   string arg  = getArg('[', ']');
   if (!arg.empty())
      nargs = atoi(arg.c_str());
}

new public function declaration in math_macrotemplate.h
  virtual void substitute(MathMacro const & m);

new function definition in math_macrotemplate.C
void MathMacroTemplate::substitute(MathMacro const & m)
{
  MathNestInset::substitute(m);

  MathArray arName;
  mathed_parse_cell(arName, name());

  arName.substitute(m);
    
  for (MathArray::size_type offs = 0; offs != arName.size(); ++offs)
    if (arName[offs]->asMathMacroArgument()) {
      const MathArray arg(arName[offs]->asMathMacroArgument()->cell(0));
      
      arName.erase(offs);
      arName.insert(offs, arg);
    }
  
  string parsedName = asString(arName);
  
  MathMacroTemplate *p = new MathMacroTemplate(parsedName, numargs(), cell(0), 
cell(1));
    
  MathArray &ar1 = p->cell(0);
  for (MathArray::size_type offs = 0; offs != ar1.size(); ++offs)
    if (ar1[offs]->asMathMacroArgument()) {
      const MathArray arg(ar1[offs]->asMathMacroArgument()->cell(0));
      
      ar1.erase(offs);
      ar1.insert(offs, arg);
    }

  MathArray &ar2 = p->cell(1);
  for (MathArray::size_type offs = 0; offs != ar2.size(); ++offs)
    if (ar2[offs]->asMathMacroArgument()) {
      const MathArray arg(ar2[offs]->asMathMacroArgument()->cell(0));
      
      ar2.erase(offs);
      ar2.insert(offs, arg);
    }
              
  MathMacroTable::create(MathAtom(p));  
}

Reply via email to