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.

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)}%
   \addcontentsline{toc}{section}{Oppgave \arabic{problem} }} 
}
\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 =)

share|improve this question
    
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
add comment

4 Answers

up vote 3 down vote accepted

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)}%
   \addcontentsline{toc}{section}{Oppgave \arabic{problem} }} 
\def\Oppgave@iv#1{\stepcounter{alternative}\iffirstalt\else\stepcounter{problem}\firstalttrue\fi
  \section*{Oppgave \arabic{problem} \normalsize (#1 poeng)}%
   \addcontentsline{toc}{section}{Oppgave \arabic{problem} }} 
\makeatletter
\begin{document}
\tableofcontents

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

enter image description here

share|improve this answer
add comment

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

enter image description here

\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)}}%
   \addcontentsline{toc}{section}{Oppgave~\arabic{problem}} 
}
\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.

share|improve this answer
add comment

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)}}%
   \addcontentsline{toc}{section}{Oppgave~\arabic{problem}} 
}  
share|improve this answer
add comment

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.

share|improve this answer
    
I've changed the reference from xstrings to xparse –  egreg Nov 22 '11 at 8:40
add comment

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.