Text Formatting\newcommand with variable number of arguments

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

\newcommand with variable number of arguments

Post by DavidG26 »

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.

Code: Select all

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

Code: Select all

\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?

Thanks in Advance,

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Re: using /newcommand

Post by frabjous »

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

\newcommand with variable number of arguments

Post by Montag »

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

Code: Select all

\begin{itemize}
\item
\end{itemize}
Which helps a lot.
OS: Win 7 64-bit LaTeX: MikTeX 2.9 64-bit Editor: TXC 1 RC1
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\newcommand with variable number of arguments

Post by cgnieder »

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:

Code: Select all

\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…
site moderator & package author
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

\newcommand with variable number of arguments

Post by Stefan Kottwitz »

Hi Clemens,

thanks for providing an answer to that question! Even if it's a question from last year, the answer could help other readers now. I changed the topic title from "using \newcommand" to "\newcommand with variable number of arguments", so it has a better chance to be listed as similar topic when such a question would be posted again.

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

Stefan
LaTeX.org admin
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\newcommand with variable number of arguments

Post by cgnieder »

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
site moderator & package author
bla1089
Posts: 2
Joined: Wed Feb 17, 2016 7:24 pm

Re: \newcommand with variable number of arguments

Post by bla1089 »

Can someone explain the `>` character in the beginning of the \NewDocumentCommand?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\newcommand with variable number of arguments

Post by cgnieder »

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

Code: Select all

>{\ProcessorB} >{\ProcessorA} m
would apply \ProcessorA followed by \ProcessorB to the tokens grabbed by the m argument.
Regards
site moderator & package author
bla1089
Posts: 2
Joined: Wed Feb 17, 2016 7:24 pm

Re: \newcommand with variable number of arguments

Post by bla1089 »

It does, thank you.
Post Reply