## LaTeX forum ⇒ Text Formatting ⇒ \newcommand with variable number of arguments

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
DavidG26
Posts: 1
Joined: Mon Jan 10, 2011 1:48 pm

### \newcommand with variable number of arguments

Hi guys,

I am completely new to LaTex and am using Kile in Ubuntu 10.10, which is proving to be rather good as an IDE.

I have a question regarding the \newcommand command.

If I have an indeterminate number of arguments can I create a \newcommand feature?

i.e. if I want to create a new command that takes in n arguments and creates an item list, i.e.

\begin{itemize}\item item1\item item2\item item3\end{itemize}

as
\newcommand{\newlist}[??]{??}

as I can set it up for 2, 3, ... any fixed number, but would love to have a input list (as opposed to a fixed number of arguments)

Can this be done?

David
Last edited by Stefan Kottwitz on Sun Feb 12, 2012 2:42 pm, edited 2 times in total.
Reason: code marked

Tags:

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am
I don't understand. How would \newlist{one}{two}{three} know when it is done reading arguments?

Montag
Posts: 340
Joined: Wed Jul 15, 2009 9:25 am
Hi David, welcome to the forum.

I wouldn't know how to solve this request as well, but just out of the blue, lots of editors offer shortcuts to create common environments such as itemize, enumerate, enumerate and so on. So in TeXnicCenter on Windows typing "it" and then hitting CTRL + SPACE leads to
\begin{itemize}\item\end{itemize}
Which helps a lot.
OS: Win 7 64-bit LaTeX: MikTeX 2.9 64-bit Editor: TXC 1 RC1

cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm
A year to late (*): it can be done using the xparse package (which would require you to have an up to date system) and its argument processors:
\documentclass{article}\usepackage{xparse}\NewDocumentCommand\mylist{>{\SplitList{;}}m}   {     \begin{itemize}       \ProcessList{#1}{ \insertitem }     \end{itemize}   }\newcommand\insertitem[1]{\item #1} \begin{document}Short list:\mylist{a;b} Longer list:\mylist{a;b;c;d} List within a list:\mylist{a;b\mylist{A;B;C;D};c;d}\end{document}

(*) I'm not sure xparse had \ProcessList a year ago, though…
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

Stefan Kottwitz
Posts: 9501
Joined: Mon Mar 10, 2008 9:44 pm
Hi Clemens,


Btw. I tested the code with TeX Live 2011. After I updated l3kernel, l3packages and expl3 it worked fine.

Stefan

cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm
Stefan_K wrote:[…] Even if it's a question from last year, the answer could help other readers now.

That's what I thought, too!

Best
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

bla1089
Posts: 2
Joined: Wed Feb 17, 2016 7:24 pm
Can someone explain the > character in the beginning of the \NewDocumentCommand?

cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm
bla1089 wrote:Can someone explain the > character in the beginning of the \NewDocumentCommand?

I'll cite the xparse manual. Please let us know if that helps or not.

xparse introduces the idea of an argument processor, which is applied to an argument after it has been grabbed by the underlying system but before it is passed to code. An argument processor can therefore be used to regularise input at an early stage, allowing the internal functions to be completely independent of input form. Processors are applied to user input and to default values for optional arguments, but not to the special \NoValue marker.

Each argument processor is specified by the syntax >{ processor } in the argument specification. Processors are applied from right to left, so that
>{\ProcessorB} >{\ProcessorA} m
would apply \ProcessorA followed by \ProcessorB to the tokens grabbed by the m argument.

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

bla1089
Posts: 2
Joined: Wed Feb 17, 2016 7:24 pm
It does, thank you.