5

When I compile the following code I get the error:

"use of \abc doesn't match its definition"

What is the definition of \abc?

\documentclass{article}

\begin{document}

\def\foo#1{\def\abc#1{test}} \foo{gh}

\abc x y z

\end{document}

  • 2
    welcome to TeX.SE! if you want \abc to have "its own" parameter, you can use \def\abc##1{test}. the double # is needed because #1 is already "taken" as the 1st parameter of \foo. – Daniel Diniz Nov 22 '22 at 01:33

2 Answers2

6

\def\abc gh{test}

defines \abc to be a macro that must be followed by gh and expands to test, use as

\abc gh x y z
David Carlisle
  • 757,742
5

Add \show\foo and \show\abc. Then run LaTeX in interactive mode:

\documentclass{article}

\begin{document}

\def\foo#1{\def\abc#1{test}} \foo{gh}

\show\foo \show\abc

\abc x y z

\end{document}

TeX will stop like for an error and show

> \foo=macro:
#1->\def \abc #1{test}.
l.8 \show\foo

Hit return to see the next message

> \abc=macro:
gh->test.
l.9 \show\abc

Hit return again to get

! Use of \abc doesn't match its definition.
l.11 \abc x
            y z

If you can't run in interactive mode, say \meaning\foo instead and look at the generated output.

The format for \show when the token to be shown is a macro is

<parameter text>-><replacement text>.

(note the final period that's not part of the replacement text).

The parameter text in the case of \foo is #1, which means that TeX will look for an argument with the usual rules. Notice that the parameter text denotes something that TeX must find. For instance, if you call {\foo}, TeX will complain because it cannot find an argument: a closing brace is not a suitable token to be taken as argument.

In the case of \abc what's shown as parameter text is gh, which means that after \abc TeX must find gh. If not, an error will be raised.

But you don't really need \show: just follow the expansion. The call

\foo{gh}

will result in TeX replacing \foo with its replacement text after looking for the arguments:

\def\abc gh{test}

Not \def\abcgh as you might expect. In the replacement text of \foo, \abc is a single token and TeX only processes tokens. So you get, with for dividing tokens,

\def•\abc•g•h•{•t•e•s•t•}

The parameter text is anything that follows the macro to be defined up to the first {. It can contain # for denoting required arguments like in the case of \foo, but it needn't.

egreg
  • 1,121,712
  • This answer is arguably better as it teaches OP how to debug the issue other than by staring at the code (and the latter doesn't always work). Nevertheless in this case it's a bit unclear what OP actually wants to get (\abcgh or \abc ##1? in the latter case what's the point of passing #1 into \foo in the first place? Probably initially there wasn't but OP add that to temporarily "fix" the first error) – user202729 Nov 22 '22 at 09:46
  • @user202729 Sorry, I cannot read minds. ;-) – egreg Nov 22 '22 at 10:28