Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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---
share|improve this question
    
I've removed the biblatex tag: the issue is noting to do with biblatex. –  Joseph Wright Jan 25 '13 at 7:06

1 Answer 1

up vote 15 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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