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 would like to define a command that accepts a variable number of arguments:

% would result in \emph{foo}

% would result in \emph{foo} $\to$ \emph{bar}

% would result in \emph{foo} $\to$ \emph{bar} $\to$ \emph{baz}

and so on. Is this possible, and if it is, how can I do this?

share|improve this question
Doing it in complete generality is impossible; you need a terminator. A better syntax would be to provide one argument as a comma separated list of values. – egreg Mar 7 '13 at 14:41
Note that in LaTeX you really shouldn't do this as it breaks the syntax rules that latex tries to make consistent. All LaTeX commands always have a fixed number of {} argumnets, optional arguments should be [] or here probably better a comma separated list within a single argument. – David Carlisle Mar 7 '13 at 14:49
@egreg and DavidCarlisle, I can see we all had the same idea. I wrote my answer before reading your comments – JLDiaz Mar 7 '13 at 14:52
I don't think it's a duplicate: the linked question is about a completely different need. – egreg Mar 7 '13 at 15:06
@Tobi You can vote for reopening – egreg Mar 7 '13 at 15:19
up vote 15 down vote accepted

Use menukeys … I guess it’s what your looking for ;-)



% create a new simple style to add arrwos between the items
\newmenustylesimple*{arrows}{\CurrentMenuElement}[ $\to$ ]{blacknwhite}

\menu{foo > bar > baz}


% apply the new style to the old macro. The optional arguments
% defines the separator, the default is a comma
\menu{foo > bar > baz}


The first line shows the default style of \menu, but it’s possible to change it in many ways. The example shows a very simple style. More predefined styles can be found in the manual …

share|improve this answer
+1 for letting me know about menukeys. Oh! And for authoring it :-) – JLDiaz Mar 7 '13 at 15:57
@JLDiaz: Your welcome :-) – Tobi Mar 7 '13 at 16:02
@Tobi I hope you will find a bit of time for working on. :-) – Speravir Mar 7 '13 at 18:27
@Speravir: Me too :-) – Tobi Mar 7 '13 at 21:57

Not exactly what you requested, but with the same functionality could be the following:

\usepackage{pgffor}   % you don't need this if tikz is already included
 \foreach \e in {#1}{%



share|improve this answer

With LaTeX3 macros, using a list of items instead of a variable number of arguments (which is not recommended in LaTeX) is rather easy.

Why not using a variable number of arguments? The main reason has been explained by David in a comment; another reason is in awkwardness of the implementation; one should start a recursion, store the last found argument and check for a left brace; if found continue the recursion, otherwise stop and output the result. Since the arguments need to be stored anyway, it's better to use one argument and implement the command by cycling over the entries.

  \vwegert_menu:nnn { , } { #1 } { \emph }
  \vwegert_menu:nnn { #1 } { #2 } { #3 }
\cs_new_protected:Npn \vwegert_menu:nnn #1 #2 #3
  % split the list at the chosen separator
  \seq_set_split:Nnn \l_vwegert_input_seq { #1 } { #2 }
  \seq_clear:N \l_vwegert_output_seq
  % add `\emph` around the items (or what's desired)
  \seq_map_inline:Nn \l_vwegert_input_seq
   { \seq_put_right:Nn \l_vwegert_output_seq { #3{ ##1 } } }
  % print the result
  \seq_use:Nnnn \l_vwegert_output_seq { $\to$ } { $\to$ } { $\to$ }
\seq_new:N \l_vwegert_input_seq
\seq_new:N \l_vwegert_output_seq






We have a simple command \menu without options and a more powerful one \xmenu with which you can change the separator (leading optional argument) or the formatting (trailing optional argument); the trailing optional argument should be a command taking exactly one argument.

The working is:

  1. Split the list into components; leading and trailing spaces will be removed.

  2. Add to each element the chosen formatting (default \emph).

  3. Produce each element of the list, with $\to$ between any two elements.

enter image description here

share|improve this answer
Looking at this answer, the question could be a duplicate of parse variable length comma separated list into command, or am I mistaken again? :-) – Hendrik Vogt Mar 7 '13 at 16:02
@HendrikVogt The methods are indeed, similar; but the solution comes only after judging unfeasible the "variable number of arguments" path. – egreg Mar 7 '13 at 18:43

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.