考试类:有条件地定义questionlabel和partlabel

考试类:有条件地定义questionlabel和partlabel

我正在调整期末考试的模板,不幸的是,它需要遵循一些格式指南。特别是,问题编号需要如下所示:

1.0 Question 1
1.1 Part 1 of question 1
1.2 Part 2 of question 1
...
2.0 Question 2
2.1 Part 1 of question 2
2.2 Part 2 of question 2
2.3.0 Part 3 of question 2
2.3.1 Subpart 1 of part 3 of question 2
2.3.2 Subpart 2 of part 3 of question 2
2.4 Part 4 of question 2
...

问题是部件标签有时应该包含两个数字(如果部件没有子部件),但有时应该包含三个数字(如果端口有子部件),最后一个数字为零。我已经成功实现了始终有两个数字或始终有三个数字的格式,但到目前为止我还没有设法将它们组合起来。

这是我的两个数字标签的代码:

\renewcommand{\questionlabel}{\thequestion.0 }
\renewcommand{\partlabel}{\thequestion.\arabic{partno} }
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{2em}
}

以下是三个数字标签的代码:

\renewcommand{\questionlabel}{\thequestion.0.0 }
\renewcommand{\partlabel}{\thequestion.\arabic{partno}.0 }
\renewcommand{\subpartlabel}{\thequestion.\arabic{partno}.\arabic{subpart} }
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{3em}
}
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}
\renewcommand{\subpartshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}

因此,现在一切都取决于某个部分是否有我失败的子部分来将它们组合起来。

编辑:这是MWE:

\documentclass[a4paper,ngerman,12pt]{exam}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=1cm]{geometry}


\pointformat{/\thepoints}
\addpoints
\pointsdroppedatright 

%%% For two numbers labels %%%
\renewcommand{\questionlabel}{\thequestion.0 } %% Has one zero added
\renewcommand{\partlabel}{\thequestion.\arabic{partno} } %% Has no zeros added
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{2em}
}


%%% For three numbers labels %%%
\renewcommand{\questionlabel}{\thequestion.0.0 } %% Has two zeros added
\renewcommand{\partlabel}{\thequestion.\arabic{partno}.0 } %% Has one zero added
\renewcommand{\subpartlabel}{\thequestion.\arabic{partno}.\arabic{subpart} }
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{3em}
}
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep%
}
\renewcommand{\subpartshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}

\begin{document}
    \begin{questions}
        \question question 1
        \begin{parts}
            \part[5] part 1 of question 1
            \droppoints
            \part part 2 of question 1
            \begin{subparts}
                \subpart[2] subpart 1 of part 2 of question 1
                \droppoints
                \subpart[4] subpart 2 of part 2 of question 1
                \droppoints
            \end{subparts}
        \end{parts}
        \question question 2
    \end{questions}
\end{document}

问题看起来是这样的,而且问题出在以下位置:

标签问题

当前解决方案提案如下:

段落宽度问题

答案1

我怀疑部分和子部分标签在 内是右对齐的\labelwidth,因此需要\hbox to \labelwidth。此外,似乎钩子出现在 之后\linewidth

\documentclass[a4paper,ngerman,12pt]{exam}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=1cm, showframe]{geometry}
\usepackage{lipsum}

\pointformat{/\thepoints}
\addpoints
\pointsdroppedatright 

\makeatletter
\renewcommand{\thequestion}{\arabic{question}\@ifundefined{r@part@\arabic{question}@1}{\phantom{.0}}{.0}}
\renewcommand{\thepartno}{\arabic{question}.\arabic{partno}\@ifundefined{r@subpart@\arabic{question}@\arabic{partno}@1}
  {\phantom{.0}}{.0}}
\renewcommand{\thesubpart}{\arabic{question}.\arabic{partno}.\arabic{subpart}} %% Has no zeros added
\renewcommand{\questionlabel}{\thequestion\hfill}%% Has two zeros added
\renewcommand{\partlabel}{\hbox to \labelwidth{\thepartno\hfill}}%% Has one zero added
\renewcommand{\subpartlabel}{\hbox to \labelwidth{\thesubpart\hfill}}
\renewcommand{\questionshook}{%
    \setlength{\leftmargin}{3em}%
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
    \setlength{\linewidth}{\textwidth}%
}
\renewcommand{\partshook}{%
    \setlength{\@totalleftmargin}{0pt}%
    \setlength{\leftmargin}{3em}%
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
    \setlength{\linewidth}{\textwidth}%
}
\renewcommand{\subpartshook}{%
    \setlength{\@totalleftmargin}{0pt}%
    \setlength{\leftmargin}{3em}%
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
    \setlength{\linewidth}{\textwidth}%
}
\makeatother

\begin{document}
    \begin{questions}
        \question question 1 \lipsum[2]
        \begin{parts}
            \part[5] part 1 of question 1 \lipsum[2]
            \droppoints
            \part part 2 of question 1
            \begin{subparts}
                \subpart[2] subpart 1 of part 2 of question 1 \lipsum[2]
                \droppoints
                \subpart[4] subpart 2 of part 2 of question 1
                \droppoints
            \end{subparts}
        \end{parts}
        \question[10] question 2
    \end{questions}
\end{document}

相关内容