# How to create new commands with multiple arguments?

I'd like to create a new command \add that accepts 2 arguments, called entity and title. In other words, the command should look like this: \add{entity}{title}. The output should have the following form: \textit{entity} '{title}'.

So if I for example type \add{article}{LaTeX} the result should be article 'LaTeX'.

How can I do this? I hope that I was clear enough about what I want to achieve.

Edit: Is it also possible to give 'names' to the arguments? What I mean is: when one uses the command, the interface displays \add{arg1}{arg2} with @shyamupa's answer. Can this be changed into \add{entity}{title} in order to make it easier for users to understand what they're supposed to enter?

-
Maybe this: Commands that may take a variable number of arguments, also be interesting for you. –  OSjerick Nov 29 '13 at 23:40
The addition you ask for isn't related to TeX; the editor might be configured to type \add{arg1}{arg2} with some shortcut and place the cursor in some convenient place; but this depends on the editor and no universal answer is possible. –  egreg Nov 29 '13 at 23:45
@egreg: Okay, I didn't know that. Thanks for mentioning! –  Jeroen Nov 29 '13 at 23:46

\newcommand{\add}[2]{\textit{#1} {#2'}}


For more reading material on this, see

-
thanks for the edit. @zeroth –  shyamupa Nov 29 '13 at 23:22
you are welcome, AND welcome to TeX.SE! :) –  zeroth Nov 29 '13 at 23:23
@Jeroen If you want correct single quotation marks for the second argument should use #2'. –  OSjerick Nov 29 '13 at 23:27
That's just perfect! Nice start on this platform! :) One more detail: is it also possible to give a name to the arguments, just to make it easier for users to use the command? –  Jeroen Nov 29 '13 at 23:27
@Jeroen Are you thinking to something like \add{entity=article,title=\LaTeX}? –  egreg Nov 29 '13 at 23:32

I'm going to suggest a very different approach which might seem like a bit of overkill in this particular situation but can pay off in the long run.

I would suggest using keys to accomplish what you want.

So, I would set up keys as follows:

\documentclass{article}
\usepackage{pgfkeys}
\makeatletter
\pgfkeys{/jeroen/entities/.cd,
title/.initial=,
entity/.initial=,
}
\def\jeroen@set@keys#1{%%
\pgfkeys{/jeroen/entities/.cd,#1}}
\def\jeroen@get#1{%%
\pgfkeysvalueof{/jeroen/entities/#1}}

\bgroup
\jeroen@set@keys{#1}%%
\textit{\jeroen@get{entity}} \jeroen@get{title}'%%
\egroup}

\makeatother

\begin{document}

\end{document}


In the shortrun, this is a lot of overhead to create for what seems like a simple command. But, in the long run this can pay off dividends.

In my experience, I find instances where I want to use essentially the same underlying macro, but rewriting the macro can be a pain in the neck. For example, I might want to use the macro without calling one of its arguments; I might want to add a third argument or even an optional argument. Using key values, it can be much easier to modify your code or improve upon implementation later.

Key values also get around the issue of remembering which argument is for which content.

By using \bgroup and \egroup, you can more easily define default behaviors when a key has not been called. For example,

\documentclass{article}
\usepackage{pgfkeys}
\makeatletter
\pgfkeys{/jeroen/entities/.cd,
title/.initial=,
entity/.initial=article,
}
\def\jeroen@set@keys#1{%%
\pgfkeys{/jeroen/entities/.cd,#1}}
\def\jeroen@get#1{%%
\pgfkeysvalueof{/jeroen/entities/#1}}
\bgroup
\jeroen@set@keys{#1}%%
\textit{\jeroen@get{entity}} \jeroen@get{title}'%%
\egroup}
\makeatother

\begin{document}

\end{document}


-

An interesting example of how one can define commands with different syntax, but doing the same thing, with minumum effort.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

% Define the keys
{
title  .initial:n = {},
entity .initial:n = article,
}

% The key-value interface
{
\group_begin:
\keys_set:nn { jeroen/add } { #1 }
\group_end:
}

% The two argument interface
{
\jeroen_add:nn { #1 } { #2 }
}

% The main command
{
\textit{#1}\nobreakspace#2'
}

% The variant needed for the key-value interface
\ExplSyntaxOff

\begin{document}

Key-value syntax:

\medskip

Two argument syntax:

\end{document}


The difference between \jeroen_add:nn and \jeroen_add:VV is that the former expects two standard braced arguments and the latter uses the value of two variables passed as arguments (unbraced). It isn't really necessary in this case, because

\jeroen_add:nn { \l_jeroen_add_entity_tl } { \l_jeroen_add_title_tl }


would work the same. In other cases, creating the variant could solve many subtle problems. It is also conceptually better.

-

xparse can save life with its features some times. Here is an example:

\documentclass{article}
\usepackage{xparse}
\begin{document}
\end{document}


For details texdoc xparse or visit texdoc.net

-
details please :-) –  azetina Nov 29 '13 at 23:28
@azetina My typing is not trustworthy ;-) –  Harish Kumar Nov 29 '13 at 23:29

Using \def should be avoided as there is no "compile type checking" whether or not the macro being defined clashes the existing macro with the same name. Use \newcommand instead.

\documentclass[preview,border=12pt]{standalone}

%\def\Marco#1#2{arg1: #1 and arg2: #2} <== it is not recommended as a best practice.
\newcommand\Marco[2]{arg1: #1 and arg2: #2}

\begin{document}
\Marco{Van}{Basten}
\end{document}

-
It is set to community wiki to prevent me from getting reputation points whenever you like this answer and vote it up. –  kiss my armpit Nov 29 '13 at 23:49

\def\Add#1#2{\textit{#1}\ '{#2}'}. Name \Add seems to be safer.

Or, if one dislikes \def, \newcommand{\Add}[2]{\textit{#1}\ '{#2}'}

-
Not \def, please! –  egreg Nov 29 '13 at 23:21