11

I was wondering how some commands manage to take #1 as an argument when used, not when declared.

For instance, biblatex's \DeclareFieldFormat can be used like this:

\DeclareFieldFormat {postnote} {#1}

I have taken a look at biblatex1.sty (lines 3480--3506, v2.3, revison 28161) but couldn't figure out how it can do that. Where is the trick?

This is what I want to achieve:

\foo {bar} {\textbf{#1}}

I want it to typeset bar in bold face; that is, to typeset whatever is given in the first argument in whatever format is given in the second one.

From biblatex1.sty:

%---CUT---
\newrobustcmd*{\DeclareFieldFormat}{%
  \@ifstar
    {\blx@defformat\blx@defplainformat{ffd}*}
    {\blx@defformat\blx@defplainformat{ffd}{}}}
%---CUT---

% {<macro>}{<class>}{<*>}

\def\blx@defformat#1#2#3{%
  \@ifnextchar[%]
    {\blx@defformat@i{#1}{#2}{#3}}
    {\blx@defformat@i{#1}{#2}{#3}[*]}}

% {<macro>}{<class>}{<*>}[<entrytype>]{<name>}

\def\blx@defformat@i#1#2#3[#4]#5{%
  \notblank{#3}
    {\blx@resetformat{#2}{#5}}
    {}%
  \def\blx@defformat@a{#2}%
  \def\blx@defformat@b{#4}%
  \blx@xsanitizeafter{\def\blx@defformat@c}{#5}%
  \afterassignment\blx@defformat@ii
  #1}
%---CUT---
1
  • I've removed the biblatex tag: the issue is noting to do with biblatex.
    – Joseph Wright
    Jan 25, 2013 at 7:06

1 Answer 1

17

You need an auxiliary function, which is defined with one argument:

\documentclass{article}
\newcommand{\foo}[2]{%
  \def\fooaux##1{#2}%
  \fooaux{#1}%
}
\newcommand{\fooaux}[1]{}
\begin{document}
\foo{bar}{\textbf{#1,}} hhhh
\end{document}

The idea here is that \fooaux will end up defined as equivalent to

\renewcommand{\fooaux}[1]{%
  \textbf{#1,}%
}

in the code above.

0

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.