# Conditionals in \newcommand

I find myself using the \newcommand macro a lot and apart from having the occasional difficulty with properly defining the optional parameter the macro works well. There is one serious limitation though, which I hope will show itself as being my ignorance rather than a limitation, namely the ability to make the macro definition subject to the particular value of a parameter. A simple example: I find frequent use of the expression {1, 2, ..., n} what I call an n-set and am using the following simple macro:

\newcommand{\myNset}{\{1,2,\dots,n\}}

Wouldn't it be helpful if I could pass an integer as parameter, make n its default, and produce {1,2} or {1,2,3) etc. if the parameter happens to be 2 or 3. Certainly beats the clumsy:

\newcommand{\myTwoset}{\{1,2\}}
\newcommand{\myThreeset}{\{1,2,3\}}

etc., which of course is too clumsy to be of use. Where draw the line?, at 10?, at 27?

This may only be a trivial example, but I find applications for such conditional processing of my \newcommand macros time and time again, for instance in connection with creating matrices. It would take my use of LaTeX to whole new level.

By the way, I have read through the relevant pages in the LaTeX Companion, but no luck.

-
Thanks Werner, still coming to grips with texting in questions, comments, answers. I now know how to put code in a block, but how would I have put that \newcommand inline? –  Reinhard Neuwirth Mar 12 '12 at 8:57

\documentclass{article}
\newcommand\myNset[1][99]{\{1,2\ifnum#1=3 ,3\else\ifnum#1>3 ,\dots,n\fi\fi\}}

\begin{document}

\myNset
\myNset[3]
\myNset[2]

\end{document}

\myNset[0] and [1] are the same as [2]

-
Thanks, that works, and I am experimenting with it. Evidently I should have consulted the LaTeX Companion on control structures, and it goes without saying that all those structures can then be used in \newcommand definitions. Can they? –  Reinhard Neuwirth Mar 12 '12 at 9:00
sure, no problem –  Herbert Mar 12 '12 at 10:39
Herbert, I am impressed with that logo in your profile. Any chance of letting me have the source code for that? –  Reinhard Neuwirth Mar 12 '12 at 23:07
it can be found at the 3d gallery of PSTricks.tug.org –  Herbert Mar 13 '12 at 12:31
Thanks, I was hoping it is TikZ, but psTricks I will get around to eventually. –  Reinhard Neuwirth Mar 13 '12 at 23:21

With this LaTeX3 implementation, the optional argument can be also a variable:

\documentclass{article}
\usepackage{amsmath,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\Nset}{O{n}}
{
\{1
\prg_case_str:nnn { #1 }
{
{1} {}
{2} {,2}
{3} {,2,3}
{4} {,2,3,4}
}
{,2,\dots,#1}
\}
}
\ExplSyntaxOff

\begin{document}
$\Nset$

$\Nset[1]$

$\Nset[2]$

$\Nset[3]$

$\Nset[4]$

$\Nset[5]$

$\Nset[m]$

$\Nset[k-1]$
\end{document}

With \NewDocumentCommand{\Nset}{O{n}}{...} we define a command having a unique optional argument, with default value n. This command typesets a left brace and 1, then examines the argument checking against a list of known values, in this case 1, 2, 3 and 4, taking the appropriate action (i.e., typesetting all the numbers missing to the argument, separated by commas); if the optional argument doesn't appear in the list the last action is performed, that is, typesetting ,2,\dots,<argument>. Finally, the closing brace is found.

## Important change

Due to the changes made to expl3 in Summer 2012, the function \prg_case_str:nnn should be renamed to

\str_case:nnn

(same syntax).

-
Thank you, that works. I was completely unaware that I have access to LaTeX3, thought I was working with LaTeX2 only. Where do I read up on this stuff? It looks very promising, but I am completely unfamiliar with the syntax. For example I am having a merry time getting the macro put {} around my n-sets. –  Reinhard Neuwirth Mar 12 '12 at 9:05
@ReinhardNeuwirth texdoc expl3 and texdoc interface3. I'll fix the braces, sorry. :) –  egreg Mar 12 '12 at 9:07
(1) Now that you fixed the braces I too can see where they had to go! (2) You are way ahead of me, how do I get access to texdoc exp13 and interface3? All documemts I find are varieties of LaTeX2. (3) I intend to give Herbert the benefit of having answered my question. He got me on the right track and I have spent the last hour fixing up large numbers of newcommands and it is therefore proper to give him credit for that. Is there a way to give you credit for launching me into the wonderful world of LaTeX3? Your answer works, even if I don't yet know how to write similar stuff myself. –  Reinhard Neuwirth Mar 12 '12 at 9:57
The texdoc command is a shell command. It depends on the OS you're using how to issue it; for example, TeXShop has a menu entry to access texdoc. On other systems or with other front-ends it's different. An upvote if the answer is deemed useful is sufficient; please, accept Herbert's answer. –  egreg Mar 12 '12 at 10:01
I am using Windows7 and TeXMaker 3.3.1 front-ending MiKTeX 2.9 –  Reinhard Neuwirth Mar 12 '12 at 23:15

Here is a solution based on catoptions package:

\documentclass{article}
\usepackage{catoptions}
\newcommand*\Nset[1][1]{%
\{1%
\cptifcasse\ifstrcmpTF{#1}
{1}{}
{2}{, 2}
{3}{, 2, 3}
{4}{, 2, 3, 4}
\elseif
, 2, \dots, #1%
\endif
\}%
}

\begin{document}
$\Nset$
\par
$\Nset[1]$
\par
$\Nset[3]$
\par
$\Nset[n]$
\end{document}
-
The \cptifcasse command is not documented. –  egreg Mar 12 '12 at 23:46
@egreg: Unfortunately, less than one-half of the commands in the catoptions package are documented. I have to pay bills outside TeX. –  Ahmed Musa Mar 15 '12 at 12:19