TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am a newbie at LaTeX programming, so please be patient. :-)

I am writing a .sty file which I want to include a command \dan which is overloaded with three separate versions depending on whether called with zero, one or two arguments.

E.g. I want:


to generate

Full English




to generate

streaky Bacon


back Bacon

respectively, and


to generate

streaky Bacon with fried Eggs.

"streaky", "fried" etc. are just examples, the users should be able to input any argument.

I have looked into the xparse package, but am finding the documentation inpenetrable. Please can you advise on how to code this?

share|improve this question
Are you sure that you want that syntax (which you can implement but) It does not follow the pattern of latex syntax which never uses differing numbers of brace groups. LaTeX syntax conventions would make \dan[sreaky,fried] far more preferable. Optional, so [] not {} and taking a comma separated list. – David Carlisle Mar 12 '12 at 22:53
I'm not fully decided on syntax yet, although I have a preference for the above as I think that will be most natural for the users. Thanks for your point though that optional parameters are usually in [] parentheses, that is something we need to consider - also a comma-separated list is an excellent alternative suggestion. – dan8394 Mar 12 '12 at 22:59
I recommend to remove your question "Also, is xparse stable enough to use to build a package that you plan to release on CTAN?" from this question and to ask it as a separate question. Also, as you say "I have looked into the xparse package, but am having no success", it's always good to show what you've tried so far. On the one hand, users here can perhaps give you the decisive clue how to go on, on the other hand your question won't be in danger of appearing as a "can i have teh codez plz" question. – doncherry Mar 12 '12 at 23:10
Thanks doncherry. I have modified my question as you suggested. – dan8394 Mar 13 '12 at 9:21
up vote 7 down vote accepted

This is how.


        {Full English}
        {#1 Bacon}%
     {#1 Bacon with #2 Eggs}%







This said, I advise you against doing so. Users of your package will have a bad time figuring out how to use the commands. Just changing the argument specifier in {oo} instead of {gg} the syntax would be more in line with the LaTeX conventions:





If you can be more precise about your intentions, a different strategy may be suggested.

share|improve this answer
This is extremely helpful, and just what I needed. Thanks very much. – dan8394 Mar 12 '12 at 23:11

As noted in comments I'd recommend using a more conventional LaTeX syntax. @egreg has given a LaTeX3 version, so here's using a loop from LaTeX2e:



\def\dansep{\def\dansep{, }}%

\@namedef{dan:full}{Full English}
\@namedef{dan:streaky}{Streaky Bacon}
\@namedef{dan:back}{Back Bacon}
\@namedef{dan:fried}{Fried Egg}
\@namedef{dan:mushrooms}{Grilled Mushrooms}



a \dan

b \dan[streaky]

c \dan[back]

e \dan[streaky,fried]

f \dan[streaky,fried,mushrooms]

share|improve this answer
+1 for \def\dansep{\def\dansep{, }}. The author of etoolbox uses it in other contexts. – Ahmed Musa Mar 13 '12 at 0:32
I don't think this is what I need. I need to take general arguments, bacon, eggs, etc. are just placeholders. Sorry for not making that clear. – dan8394 Mar 13 '12 at 9:18
Yes it was clear that it was a toy example, but I also assumed that your real example might require more than two items, which is why I generalised it to an arbitrary list. – David Carlisle Mar 13 '12 at 9:30

As noted in the comments it is much more preferable to use a key value interface and the answer given by David Carlisle, gives the traditional LaTeX2e way. For more complicated packages, I would lean on using a PGF style of keys. If nothing the amount of users asking questions on this site about TikZ, indicates that there is a large community of users familiar with the PGF style key value interface.

%% Step one define family
\pgfkeys{/bacon/.is family}
%% Define your keys
     type/.store in=\type,
     cook/.store in=\cook,
%% Process keys and set defaults, for later use
  \pgfkeys{/bacon #1}
      \type\ cook \cook,
\dan[type=shoulder bacon, cook=well]{}

You get tremendous flexibilty with this approach.

share|improve this answer

Here is a solution using catoptions package:

      ,\space\iflastindris and\space\fi
% Default inner list parser = , (comma)
% Default outer list parser = ; (semicolon)

% Example:
  full,      Full English;
  streaky,   Streaky Bacon;
  back,      Back Bacon;
  fried,     Fried Egg;
  mushrooms, Grilled Mushrooms

% The following is also possible:
  full       =Full English,
  streaky    =Streaky Bacon,
  back       =Back Bacon,
  fried      =Fried Egg,
  mushrooms  =Grilled Mushrooms

\item\dan[streaky, fried]
\item\dan[streaky, fried, mushrooms]
%\item\dan[xstreaky] % error: undefined

enter image description here

share|improve this answer

Your Answer


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.