Problem.
I'm trying to write a command similar to \newcommand
, but I'm having problems with illegal parameter numbers: for reasons that I can't fix out, it's asking me to use parameters of the type ##1
rather than #1
. I'm familiar with using the former in the case of nested definitions, e.g. in code-snippets such as \def\foo#1{\def\bar##1{#1-##1}}
; but I don't understand why the error is coming up in what I'm trying to do. I'm hoping that someone can show me how to make parameters of the style #1
suffice, in the example I describe below.
Details.
I'm trying to write a macro called \newmacro
, which emulates the syntax of \newcommand
, and which makes each macro write its own definition to an output file the first time it is used. Try out the minimal example below to see precisely what I mean:
\documentclass{article}
\makeatletter
\newwrite\macro@outfile
\immediate\openout\macro@outfile=\jobname.macros%
\typeout{Writing extra macros to file \jobname.macros}%
\def\macro@write#1#2{%
\immediate\write\macro@outfile{\string\renewcommand\string#1#2}%
\renewcommand#1#2%
#1%
}
\def\newmacro#1[#2]#3{%
\def#1{\macro@write{#1}{[#2]{#3}}}}
\makeatother
These macro definitions don't quite work properly: for instance, using the above pre-amble, the second invocation of \newmacro
below throws an error.
\begin{document}
\newmacro\testmacro[1]{foo(##1)}
\testmacro{1}
\testmacro{2}
\newmacro\testmacro[1]{foo --- #1 ---}
\testmacro{3}
\testmacro{4}
\end{document}
If this document is compiled with the preamble above as test.tex
, in addition to the usual output, a new file test.macros
is created which will have the following content:
\renewcommand\testmacro[1]{foo(##1)}
\renewcommand\testmacro[1]{foo --- ##1 ---}
This is almost correct, and it is what I mean by the macro writing "its own definition" to an output file. However, I would like the stored definitions to use the parameters #1
rather than ##1
, and for the invocation of \newmacro
with the parameter #1
not to throw errors. I'm hoping that someone can show me how to do this.