我回来了... =) 我实际上是在尝试解决自己的问题,我觉得我在这里学到了很多东西。非常感谢你帮助像我这样无知的人 ^^
我正在尝试制作一个包含任务列表的文本。挪威语中的 Oppgave 与任务相同。每个任务都应有一个强制参数,说明该任务值多少分。任务还应有一个可选参数,alt
该参数将任务作为替代方案 1 输入。下面是我正在寻找的一个最小示例
\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}
输出应如下所示
反对 1 (5 磅)
反对 2 (3 点)
反对 3 (2 磅)
Oppgave 4 Alternative I (4 句)
Oppgave 4 Alternative II (4 句)
反对 5 (3 点)
反对 6 替代方案 I (7 条)
对手 6 备选方案 II (7 条)
对手 6 备选方案 III (7 点)
反对 7 (9 点)
这是我迄今为止工作的一个最小示例。
\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}
答案1
您可以使用该包解析并将最后一个参数设置为可选。另一种方法基于 egreg 的巧妙想法(像往常一样)。您可以定义带星号的版本,而不是使用可选参数,而是用罗马数字打印替代项。以下示例中实现了这两种解决方案。(感谢 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}
答案2
由于您的替代问题只需要一个“步进”罗马数字,因此只需用*
具有替代方案的问题进行声明:
\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}