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

Postby DavidG26 » Mon Jan 10, 2011 1:55 pm

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.

  1. \begin{itemize}
  2. \item item1
  3. \item item2
  4. \item item3
  5. \end{itemize}


as
  1. \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.
Reason: code marked

Tags:

User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am
Location: Amherst, MA

Postby frabjous » Mon Jan 10, 2011 4:46 pm

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

Postby Montag » Mon Jan 10, 2011 8:42 pm

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
  1. \begin{itemize}
  2. \item
  3. \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: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Sun Feb 12, 2012 2:13 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:
  1. \documentclass{article}
  2. \usepackage{xparse}
  3. \NewDocumentCommand\mylist{>{\SplitList{;}}m}
  4. {
  5. \begin{itemize}
  6. \ProcessList{#1}{ \insertitem }
  7. \end{itemize}
  8. }
  9. \newcommand\insertitem[1]{\item #1}
  10.  
  11. \begin{document}
  12. Short list:
  13. \mylist{a;b}
  14.  
  15. Longer list:
  16. \mylist{a;b;c;d}
  17.  
  18. List within a list:
  19. \mylist{a;b\mylist{A;B;C;D};c;d}
  20. \end{document}


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

User avatar
Stefan Kottwitz
Site Admin
Posts: 8536
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sun Feb 12, 2012 2:49 pm

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
Site admin

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Sun Feb 12, 2012 3:30 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

Postby bla1089 » Wed Feb 17, 2016 7:55 pm

Can someone explain the `>` character in the beginning of the \NewDocumentCommand?

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Thu Feb 18, 2016 11:39 am

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
  1. >{\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

Postby bla1089 » Wed Mar 09, 2016 3:04 am

It does, thank you.


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 4 guests