Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Maybe this: Commands that may take a variable number of arguments, also be interesting for you. –  OSjerick Nov 29 '13 at 23:40
1  
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

6 Answers 6

up vote 10 down vote accepted
\newcommand{\add}[2]{\textit{#1} {`#2'}}

For more reading material on this, see

share|improve this answer
    
thanks for the edit. @zeroth –  shyamupa Nov 29 '13 at 23:22
1  
you are welcome, AND welcome to TeX.SE! :) –  zeroth Nov 29 '13 at 23:23
2  
@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
1  
@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}}

\newcommand\Add[1]{%%
  \bgroup
  \jeroen@set@keys{#1}%%
  \textit{\jeroen@get{entity}} `\jeroen@get{title}'%%
  \egroup}

\makeatother

\begin{document}

\Add{title=\LaTeX,entity=article}

\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}}
\newcommand\Add[1]{%%
  \bgroup
  \jeroen@set@keys{#1}%%
  \textit{\jeroen@get{entity}} `\jeroen@get{title}'%%
  \egroup}
\makeatother

\begin{document}

\Add{title=\LaTeX,entity=article}

\Add{title=long nosed creature,entity=elephant}

\Add{title=mathematics}

\end{document}

enter image description here

share|improve this answer

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
\keys_define:nn { jeroen/add }
 {
  title  .tl_set:N  = \l_jeroen_add_title_tl,
  title  .initial:n = {},
  entity .tl_set:N  = \l_jeroen_add_entity_tl,
  entity .initial:n = article,
 }

% The key-value interface
\NewDocumentCommand{\Add} { m }
 {
  \group_begin:
  \keys_set:nn { jeroen/add } { #1 }
  \jeroen_add:VV \l_jeroen_add_entity_tl \l_jeroen_add_title_tl
  \group_end:
 }

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

% The main command
\cs_new:Nn \jeroen_add:nn
 {
  \textit{#1}\nobreakspace`#2'
 }

% The variant needed for the key-value interface
\cs_generate_variant:Nn \jeroen_add:nn { VV }
\ExplSyntaxOff

\begin{document}

Key-value syntax:

\Add{title=\LaTeX,entity=article}

\Add{title=long nosed creature,entity=elephant}

\Add{title=mathematics}

\medskip

Two argument syntax:

\add{article}{\LaTeX}

\add{elephant}{long nosed creature}

\add{article}{mathematics}

\end{document}

enter image description here

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.

share|improve this answer

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

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\Add}{mm}{\textit{#1}{} `#2'}
\begin{document}
 \Add{article}{\LaTeX}
\end{document}

enter image description here

For details texdoc xparse or visit texdoc.net

share|improve this answer
1  
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}
share|improve this answer
1  
It is set to community wiki to prevent me from getting reputation points whenever you like this answer and vote it up. –  In PSTricks we trust 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}'}

share|improve this answer
3  
Not \def, please! –  egreg Nov 29 '13 at 23:21

Your Answer

 
discard

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.