# Add a possibility for zero optional inputs to own command

Some very nice people here helped me write the code snippet below. And the code works great it prints out the correct subtext, and have a optional argument by typing *

\documentclass[10pt,a4paper]{article}
\newcounter{problem}
\setcounter{problem}{0}
\newcounter{alternative}
\setcounter{alternative}{0}
\newif\iffirstalt
\usepackage{xparse}

\NewDocumentCommand{\Oppgave}{s m}{%
\IfBooleanTF{#1}%
{\stepcounter{alternative}\iffirstalt\else\stepcounter{problem}\firstalttrue\fi}%
{\setcounter{alternative}{0}\stepcounter{problem}\firstaltfalse}%
\section*{Oppgave \arabic{problem}%
{\normalfont\IfBooleanTF{#1}{~Alternative \Roman{alternative}\ }{}
\normalsize (#2 poeng)}%
}
\begin{document}

\tableofcontents

\oppgaveS{3}

\oppgaveS{4}

\oppgaveS*{2}

\oppgaveS*{2}

\oppgaveS{3}

\oppgaveS*{2}

\end{document}


Is there any possibility to add zero optional inputs? I mean so that i can write

\Oppgave and \Oppgave*


To be displayed as

Oppgave 1 and Oppgave 1 Alternativ 1

I just want to have no small text, if I just plainly write the command =)

-
Do you mean 1 optional parameter? If so you can use [1][<default>] to specify that the first parameter of a \newcommand as optional. Or use the o, O type parameter with \NewDocumentCommand. –  Peter Grill Nov 22 '11 at 7:23
Note that your MWE does not compile and uses \Oppgave while \oppgaveS is the defined command. –  Werner Nov 22 '11 at 7:42
@Werner: I assumed that \oppgaveS was intended to be the new version. –  Peter Grill Nov 22 '11 at 7:52

Here is an example with default TeX syntax and without the xparse package which is already more for package developers than for users with local macros:

\documentclass[10pt,a4paper]{article}
\newcounter{problem}
\newcounter{alternative}
\newif\iffirstalt

\makeatletter
\def\Oppgave{\@ifnextchar*{\Oppgave@i}{\Oppgave@ii}}
\def\Oppgave@i*{\@ifnextchar\bgroup{\Oppgave@iii}{\Oppgave*{1}}}
\def\Oppgave@ii{\@ifnextchar\bgroup{\Oppgave@iv}{\Oppgave{1}}}
\def\Oppgave@iii#1{%
\setcounter{alternative}{0}\stepcounter{problem}\firstaltfalse
\section*{Oppgave \arabic{problem}%
\normalfont~Alternative \Roman{alternative}\ \normalsize (#1 poeng)}%
\def\Oppgave@iv#1{\stepcounter{alternative}\iffirstalt\else\stepcounter{problem}\firstalttrue\fi
\section*{Oppgave \arabic{problem} \normalsize (#1 poeng)}%
\makeatletter
\begin{document}
\tableofcontents

\Oppgave{3}
\Oppgave{4}
\Oppgave*{2}
\Oppgave*{2}
\Oppgave{3}
\Oppgave*{2}
\Oppgave
\Oppgave*
\end{document}


-

The following requires a small modification to your input. Instead of using \oppgaveS*{<num>}, use \oppgaveS*[<num>]:

\documentclass[10pt,a4paper]{article}
\newcounter{problem} %\setcounter{problem}{0}
\newcounter{alternative} %\setcounter{alternative}{0}
\newif\iffirstalt
\usepackage{xparse}% http://ctan.org/pkg/xparse

\NewDocumentCommand{\oppgaveS}{s o}{%
\IfBooleanTF{#1}%
{\stepcounter{alternative}\iffirstalt\else\stepcounter{problem}\firstalttrue\fi}%
{\setcounter{alternative}{0}\stepcounter{problem}\firstaltfalse}%
\section*{Oppgave~\arabic{problem}~%
{\normalfont\IfBooleanTF{#1}{Alternative \Roman{alternative}~}{}}%
\IfNoValueTF{#2}{}{\normalfont\normalsize (#2 poeng)}}%
}
\begin{document}

\tableofcontents

\oppgaveS[3]
\oppgaveS[4]
\oppgaveS*[2]
\oppgaveS*[2]
\oppgaveS[3]
\oppgaveS*[2]
\oppgaveS*
\oppgaveS*[3]
\oppgaveS
\oppgaveS
\oppgaveS*
\end{document}


The optional argument specification o from xparse takes the value \NoValue if it is empty (when using \oppgaveS or \oppgaveS*), and conditioned upon using \IfNoValueTF.

-

You can declare the argument as optional by changing the first line of the definition into

\NewDocumentCommand{\Oppgave}{s g}{%


You have to change also \normalsize (#2 poeng) into

\IfNoValueTF{#2}{}{\ \normalsize (#2 poeng)}


deleting \ (control space) in the line before.

On the other hand it would be clearer if you adopted the syntax

\Oppgave*[n]


(simply change the "g" in the argument to \NewDocumentCommand to an "o").

\NewDocumentCommand{\oppgaveS}{s g}{% Put o instead of g for clearer syntax
\IfBooleanTF{#1}%
{\stepcounter{alternative}\iffirstalt\else\stepcounter{problem}\firstalttrue\fi}%
{\setcounter{alternative}{0}\stepcounter{problem}\firstaltfalse}%
\section*{Oppgave~\arabic{problem}%
{\normalfont\IfBooleanTF{#1}{\ Alternative \Roman{alternative}}{}}%
\IfNoValueTF{#2}{}{\ \normalfont\normalsize (#2 poeng)}}%
}

-

If I understand you correctly, you want to be able to say \Oppgave and that should be the same as \Oppgave{1} and \Oppgave* to be the same as \Oppgave*{1}

If so, you can just replace the line

\NewDocumentCommand{\Oppgave}{s m}{%


with

\NewDocumentCommand{\Oppgave}{s O{1}}{%


Then the first parameter (#2 since the s represents a * which is considered #1) is optional and will take on a value of {1} if not specified. The portion with the {} is the default value if a parameter is not specified.

From the xparse documentation, you can define optional arguments with either a lower case o, or an upper case O:

o A standard LaTeX optional argument, surrounded with square brackets, which will supply the special \NoValue token if not given.

O As for o, but returns {default} if no value is given. Should be given as O{default}.

if you want to be able to use {} for the optional args you should use

g An optional argument given inside a pair of TEX group tokens (in standard LATEX, {}), which returns \NoValue if not present.

G As for g but returns default if no value is given: G{default}.

The MWE you provided was using the m option which is documented as:

m` A standard mandatory argument, which can either be a single token alone or multiple tokens surrounded by curly braces. Regardless of the input, the argument will be passed to the internal code surrounded by a brace pair. This is the xparse type specifier for a normal TEX argument.

-
I've changed the reference from xstrings to xparse –  egreg Nov 22 '11 at 8:40