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've created a package which I would like to take in options and turn these into new macros (one new macro for each option). I also want these new macros to produce new macros in turn. For example, calling my package thus:

\usepackage[foo]{mypackage}

Should effectively run the following:

\newcommand{\foo}[1]{%
    \def\fooMagic{some handling of #1}
}

My attempt thus far is below - in an example form.

\ProvidesPackage{mypackage}

\RequirePackage{xkeyval}

% Create a macro to create the macro that will create the macros:
\newcommand{\@mypackage@addnewmacro}[1]{%
    \expandafter\newcommand\expandafter{\csname #1\endcsname}[1]{%
        % Point A is here (for explanatory reasons that will become apparent)
        \expandafter\def\csname #1Magic\endcsname{some handling of ##1}
    }
}

% Interpret (unrecognised) options supplied to the package as new macro names:
\DeclareOptionX*{%
    \@mypackage@addnewmacro{\CurrentOption}
}

% Process the options:
\ProcessOptionsX*

Running this:

\documentclass{report}

\usepackage[foo]{mypackage}

\foo{bar}

\begin{document}

\fooMagic

\end{document}

Should have printed:

some handling of bar

But instead it results in an error (in the log):

Undefined control sequence.\fooMagic

If the following is inserted at Point A:

\PackageWarning{mypackage}{'#1','##1'}

The log shows that #1 is empty, printing:

'','bar'

I'm not really sure why this is happening. My guess is that it might be something to do with defining all the options and then processing them afterwards, at which point the original #1 values have been dropped. Although regardless of the cause, any help to get around/fix this would be much appreciated.

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

If you insert \show\foo into your document you will see that it is defined as

 \foo=\long macro:
#1->\expandafter \def \csname \CurrentOption Magic\endcsname {some handling of 
#1} .

So not defined in terms of "foo" but of \CurrentOption which doesn't have a useful definition by the time you execute this.

On the grounds that a few extra \expandafter cure all known ills, I suggest:

\DeclareOptionX*{%
    \expandafter\@mypackage@addnewmacro\expandafter{\CurrentOption}
}

which then makes your document run as you wished.

share|improve this answer
    
That works beautifully thank you very much! Plus a handy command to boot - I've never encountered \show before, I shall have to remember it. Cheers David ^^ –  Staves Feb 19 '12 at 0:49
1  
boggle; i suppose it \show s the advantage of starting with plain tex, and only later seeking a tape with a version of latex on it... –  wasteofspace Feb 19 '12 at 11:00
add comment

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.