4

How would I write macros \firstArgument and \macroName which would do the following:

\firstArgument{\bf{a}}    reduces to a
\macroName{\bf{a}}        reduces to bf

I am aware of the approach \def\dismantle#1{{\let\Large\@firstofone #1}} but it is not a solution for me since it only works for \bf whereas is am looking for macros doing this for all macros.

Update: As it became clear throughout the discussion, "firstArgument" only shall allude to the structure of what is meant here. Similarly \bf only serves as an arbitrary example. That is important, as TeX has no notion of argument when the definition of a macro is not (yet) known and the notion may depend on that definition.

  • 1
    \bf is not defined by default in latex, and when it is defined it does not take an argument, so the syntax would be {\bf a} not \bf{a} – David Carlisle Sep 25 at 21:00
  • Would it help in understanding what I want if I had written \XYZ above? – Nobody-Knows-I-am-a-Dog Sep 25 at 21:08
  • I'd have given the same answer as Phelype :-) – David Carlisle Sep 25 at 21:10
  • it seemed worth raising the bf syntax error as if your actual use case is to extract the font command and the styled text that would matter, if it was just \xyz you picked an unfortunate example since a isn't an argument of \bf so \firstArgument is a bad name. – David Carlisle Sep 25 at 21:14
  • 1
    @Nobody-Knows-I-am-a-Dog sure in the answer I (mostly) stuck to answering that, but in comments other issues were raised such as "why is \bf bad style" and also checking whether you did actually want to grab a real argument (to pre-process it before calling the original, say, in which case checking it is actually an argument may have been relevant:-) – David Carlisle Sep 26 at 8:34
5
\documentclass{article}

\def\firstArgument#1{\zza#1}
\def\macroName#1{\zzb#1}
\def\zza#1#2{#2}
\def\zzb#1#2{\expandafter\zzc\string#1}
\def\zzc#1{}
\begin{document}

\firstArgument{\bf{a}}

\macroName{\bf{a}}
   
\end{document}

enter image description here

note that a is not an argument of \bf here.

| improve this answer | |
  • \zzc is the same as \@gobble? For eliminating the backslash – Raoul Kessels Oct 14 at 16:23
3

As in TeX they are closely tied to the concepts of defining and expanding macros let's avoid the terms "argument" and "parameter" completely and instead use the modified form of Backus/Naur-notation introduced for defining TeX's "parts of speech" in Chapter 24: Summary of Vertical Mode of the TeXbook:

If this is to be applied to the token-pattern

⟨control sequence⟩⟨left brace⟩⟨balanced text⟩⟨right brace⟩

  • ⟨control sequence⟩ neither being an \outer token nor being an active character nor being the "nameless control sequence token" constructable via \csname\endcsname or via a single character of category code 0(escape), i.e., a backslash, at the not-commented end of a line of input while \endlinechar's value is not positive
  • ⟨balanced text⟩ not containing tokens defined in terms of \outer

only, while the \escapechar-parameter has its usual value 92(decimal):

\documentclass{article}

\makeatletter
\newcommand\firstArgument[1]{\@secondoftwo#1}%
\newcommand\macroName[1]{\expandafter\expandafter\expandafter\@gobble\expandafter\string\@firstoftwo#1}%
\makeatother

\begin{document}

\ttfamily\selectfont

\firstArgument{\textbf{a}}
 
\macroName{\textbf{a}}

\end{document}
| improve this answer | |
  • Cood edit of your answer. This is so much more precise in employed concepts. – Nobody-Knows-I-am-a-Dog Sep 26 at 8:34
  • 1
    @Nobody-Knows-I-am-a-Dog Thanks, but it wasn't sufficiently precise so I saw the need of doing another edit. ;-( – Ulrich Diez Sep 26 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.