GeneralMacro with argument signature from another macro?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
kreyszig
Posts: 11
Joined: Tue Jan 26, 2010 4:30 am

Macro with argument signature from another macro?

Post by kreyszig »

Hello,

I am trying to write a macro that uses another macro to determine the signature of the arguments but I cannot get it to work. I tried something like this:

\edef\fmt{\string#1}%
\def\tmp{\def\showval}%
\expandafter\tmp\fmt{Here is the argument: '#1'}%

but it throws the error "Illegal parameter number in definition of \showval" like if \fmt was not expanded before \tmp. I guess it is a catcode problem but I can't figure out how to fix it...

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Macro with argument signature from another macro?

Post by josephwright »

There are a couple of solutions. First, you could use a token register (toks):

Code: Select all

\newtoks\mytoks
\mytoks{#1#2}
\expandafter\def\expandafter\temp\the\mytoks{code here}
(A toks is a special kind of storage area, that can hold "#" tokens safely.)

The second approach is to use the e-TeX \unexpanded primitive:

Code: Select all

\edef\tempa{\unexpanded{#1}}
\expandafter\def\expandafter\temp\tempa{code here}
This approach does let you put a "#" inside a macro, something you normally cannot do.
Joseph Wright
kreyszig
Posts: 11
Joined: Tue Jan 26, 2010 4:30 am

Re: Macro with argument signature from another macro?

Post by kreyszig »

Thanks!
Post Reply