18

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.

1
  • 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? Mar 12, 2012 at 8:57

3 Answers 3

18
\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]

5
  • 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? Mar 12, 2012 at 9:00
  • sure, no problem
    – user2478
    Mar 12, 2012 at 10:39
  • Herbert, I am impressed with that logo in your profile. Any chance of letting me have the source code for that? Mar 12, 2012 at 23:07
  • it can be found at the 3d gallery of PSTricks.tug.org
    – user2478
    Mar 13, 2012 at 12:31
  • Thanks, I was hoping it is TikZ, but psTricks I will get around to eventually. Mar 13, 2012 at 23:21
18

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

\documentclass{article}
\usepackage{amsmath,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\Nset}{O{n}}
  {
   \{1
   \str_case:nnF { #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}

enter image description here

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.

11
  • 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. Mar 12, 2012 at 9:05
  • 2
    @ReinhardNeuwirth texdoc expl3 and texdoc interface3. I'll fix the braces, sorry. :)
    – egreg
    Mar 12, 2012 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. Mar 12, 2012 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, 2012 at 10:01
  • I am using Windows7 and TeXMaker 3.3.1 front-ending MiKTeX 2.9 Mar 12, 2012 at 23:15
2

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} 
2
  • The \cptifcasse command is not documented.
    – egreg
    Mar 12, 2012 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, 2012 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.