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 am back for more... =) I am actually trying to fix my own problems, and i feel i learn so much here. Thank you so much for helpin someone as clueless as me ^^

I am attempting to make a text, with a list of tasks. Oppgave in norwegian is the same as task. Each task should have a mandatory argument saying how much points the task is worth. The task, should also have an optional argument alt which will input the task as Alternative 1. Below is a minimal example of what I am looking for

\Oppgave{5}
\Oppgave{3}
\Oppgave{2}
\Oppgave{4}{Alt}
\Oppgave{4}{Alt}
\Oppgave{3}
\Oppgave{7}{Alt}
\Oppgave{7}{Alt}
\Oppgave{7}{Alt}
\Oppgave{9}

The output should look like this

Oppgave 1 (5 poeng)

Oppgave 2 (3 poeng)

Oppgave 3 (2 poeng)

Oppgave 4 Alternative I (4 poeng)

Oppgave 4 Alternative II (4 poeng)

Oppgave 5 (3 poeng)

Oppgave 6 Alternative I (7 poeng)

Oppgave 6 Alternative II (7 poeng)

Oppgave 6 Alternative III (7 poeng)

Oppgave 7 (9 poeng)

Here is a minimal example of my work so far.

\documentclass[10pt,a4paper]{article}
\usepackage[hmargin=3cm,vmargin=3.5cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage{amsmath}

\newcounter{problem}
\setcounter{problem}{1}

\newcommand{\oppgave}[1]{\section*{Oppgave \arabic{problem} {\normalfont    \normalsize (#1 poeng)}
\addcontentsline{toc}{section}{Oppgave \arabic{problem} }} \stepcounter{problem} }

\begin{document}

\tableofcontents

\oppgave{3}

\oppgave{4}

\oppgave{2}

\end{document}
share|improve this question
    
It's worth to take a look at the exam class. –  Thorsten Donig Nov 12 '11 at 14:19

2 Answers 2

up vote 7 down vote accepted

You can use the package xparse and to set the last argument as optional. Another method based on the clever idea of egreg (as usual). Instead of using an optional argument you can define a starred version which prints the alternative with a roman numeral instead. Both solution are implemented in the following example. (Thanks to egreg)

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

\NewDocumentCommand{\oppgave}{m o}{%
  \IfNoValueTF{#2}{\setcounter{alternative}{0}\stepcounter{problem}\firstaltfalse}%
                  {\stepcounter{alternative}\iffirstalt\else\stepcounter{problem}\firstalttrue\fi}
  \section*{Oppgave \arabic{problem}%
             {\normalfont\IfNoValueTF{#2}{}{~Alternative \Roman{alternative}\ }
                \normalsize (#1 poeng)}%
   \addcontentsline{toc}{section}{Oppgave \arabic{problem} }} 
}


\NewDocumentCommand{\oppgaveS}{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

\oppgave{3}

\oppgave{4}

\oppgave{2}[a]

\oppgave{2}[a]


\oppgave{3}

\oppgave{2}[EVERY TEXT WILL BE IGNORED]


\oppgaveS{3}

\oppgaveS{4}

\oppgaveS*{2}

\oppgaveS*{2}

\oppgaveS{3}

\oppgaveS*{2}
\end{document}

enter image description here

share|improve this answer
    
I think the OP wants \oppgave macros with optional arguments not to increase the problem counter (starting from the second macro). –  lockstep Nov 12 '11 at 13:50
    
Yes! that is correct lockstep. I attempted to fiddle around inside xparse, but it was quite hard =( –  N3buchadnezzar Nov 12 '11 at 13:56
    
@lockstep: Sorry for the misunderstanding. I changed the code. –  Marco Daniel Nov 12 '11 at 14:02
    
It's more complicated - the first \oppgave macro with optional argument following the macro without such argument should increase the counter. Only the follow-ups with optional argument shouldn't. –  lockstep Nov 12 '11 at 14:05
1  
@N3buchadnezzar: Please see my edit. –  Marco Daniel Nov 12 '11 at 14:19

Since your alternative problems just need a "stepping" Roman numeral, just declare with * a problem that has alternatives:

\documentclass[a4paper]{article}
\usepackage{xparse}
\newcounter{problem}
\newcounter{alternative}[problem]
\renewcommand{\thealternative}{\Roman{alternative}}

\NewDocumentCommand{\Oppgave}{s m}{%
  \IfBooleanTF{#1}
    {\ifnum\value{alternative}=0 \stepcounter{problem}\fi\stepcounter{alternative}}
    {\setcounter{alternative}{0}\stepcounter{problem}}
  \section*{Oppgave \arabic{problem}\space 
    \ifnum\value{alternative}>0 Alternative \thealternative\space\fi
   {\normalfont\normalsize (#2 poeng)}}
   \addcontentsline{toc}{section}{Oppgave \arabic{problem}} 
}

\begin{document}
\tableofcontents

\Oppgave{5}
\Oppgave{3}
\Oppgave{2}
\Oppgave*{4}
\Oppgave*{4}
\Oppgave{3}
\Oppgave*{7}
\Oppgave*{7}
\Oppgave*{7}
\Oppgave{9}

\end{document}
share|improve this answer
    
Can I adapt you solution ;-) –  Marco Daniel Nov 12 '11 at 15:49
    
@MarcoDaniel You can do whatever you want; mine was inspired by yours, actually. –  egreg Nov 12 '11 at 15:51

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.