\newcommand 带有不同输出选项

\newcommand 带有不同输出选项

我正在创建一个考试模板。我想使用\newcommand一方面创建仅包含问题和答案空间的考试,另一方面创建解决方案。我已经定义了以下命令,但到目前为止它只显示问题。

\newcommand{\OffeneFrage}[3]{
\begin{enumerate}[resume]
\begin{samepage}
    \item {#1} \unskip\hspace*{\fill}~{#3}~P
    \nopagebreak
    \multido{}{#2}{\item[] \dotfill 
    }
\end{samepage}
\end{enumerate}
}

第一个参数代表问题,第二个参数代表答案行数,第三个参数代表分数。

如果在同一命令中列出了可选参数,我希望命令仅显示问题和答案,例如

\OffeneFrage[A dog has four legs.]{How many legs does a dog have?}{2}{1}

下面是一个示例,展示了它现在的样子和可能的样子。有人能帮我吗?提前谢谢了。

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{multido}
\usepackage{enumitem}

\newcommand{\OffeneFrage}[3]{
\begin{enumerate}[resume]
\begin{samepage}
    \item {#1} \unskip\hspace*{\fill}~{#3}~P
    \nopagebreak
    \multido{}{#2}{\item[] \dotfill % Punkte
    }
\end{samepage}
\end{enumerate}
}

\begin{document}

\OffeneFrage{How many legs does a dog have?}{2}{5}

\end{document}

在此处输入图片描述

答案1

以下提供了您请求的功能 - 一个可选的第一个参数,用于提供问题的答案。条件\IfValueTF{#1}检查是否提供了答案,并设置答案或答案加上一些空白行(以试图产生与没有提供答案时类似的垂直间隙)。

在此处输入图片描述

\documentclass{article}

\usepackage{multido,xcolor,xfp}
\usepackage{enumitem}

\NewDocumentCommand{\OffeneFrage}{ o m m m }{%
  \begin{enumerate}[resume]
    \begin{samepage}
      \item #2% Set question
      \unskip\hspace*{\fill}~#4~P\par% Set marks/points
      \nopagebreak
      \IfValueTF{#1}
        {\item[] {\color{red}#1}%
         \multido{}{\inteval{#3-1}}{\item[]}}% An answer is provided
        {\multido{}{#3}{\item[] \dotfill}}% No answer provided
    \end{samepage}
  \end{enumerate}
}

\begin{document}

\OffeneFrage{How many legs does a dog have?}{2}{5}

\OffeneFrage[A dog has four legs]{How many legs does a dog have?}{2}{5}

\OffeneFrage{How many legs does a cat have?}{2}{5}

\end{document}

\usepackage{xparse}如果你的 LaTeX 发行版不是最新的,则可能需要序言。


您可能还对您的问题的不同键值接口感兴趣:

\OffeneFrage{question=...,answer=..,lines=.,points=.}

顺序并不重要,您可以省略一些值( 和linespoints它们分别默认为41)。如果没有提供,则不会打印任何内容。并且只有设置了question时才会打印答案。\printanswerstrue

如果你设置了\printanswerstrue,则输出如下:

在此处输入图片描述

\documentclass{article}

\usepackage{multido,xcolor,xfp}
\usepackage{enumitem,xkeyval}

\makeatletter
\define@cmdkey{QA}{question}[\relax]{}
\define@cmdkey{QA}{answer}[\relax]{}
\define@cmdkey{QA}{lines}[4]{}% Default of 4 lines
\define@cmdkey{QA}{points}[1]{}% Default of 1 point
\newif\ifprintanswers

\NewDocumentCommand{\OffeneFrage}{ m }{%
  \setkeys{QA}
    {question,answer,lines,points,% Set default values
     #1}% Set user-specified values
  % Empty check: https://tex.stackexchange.com/q/53068/5764
  \if\relax\detokenize\expandafter{\cmdKV@QA@question}\relax
    % No question was supplied; do nothing
  \else
    \begin{enumerate}[resume]
      \begin{samepage}
        \item \cmdKV@QA@question% Set question
          \unskip\hspace*{\fill}%
          \ifnum\cmdKV@QA@points>0 % Print points if > 0
            ~\cmdKV@QA@points~P\par
          \fi
          % Empty check: https://tex.stackexchange.com/q/53068/5764
          \if\relax\detokenize\expandafter{\cmdKV@QA@answer}\relax
            % No answer was supplied
            \multido{}{\cmdKV@QA@lines}{\item[] \dotfill}% No answer provided; set lines
          \else
            % Answer was supplied
            \ifprintanswers
              \item[] {\color{red}\cmdKV@QA@answer}% Set answer
              \multido{}{\inteval{\cmdKV@QA@lines-1}}{\item[]}% Set additional gap
            \else
              \multido{}{\cmdKV@QA@lines}{\item[] \dotfill}% Set answer lines
            \fi
          \fi
      \end{samepage}
    \end{enumerate}
  \fi
}
\makeatother

%\printanswerstrue

\begin{document}

\OffeneFrage{
  question={How many legs does a dog have?},
  lines=2,
  points=5
}

\OffeneFrage{
  answer={A dog has four legs},
  question={How many legs does a dog have?},
  lines=2,
  points=5
}

\OffeneFrage{
  lines=3,
  question={How many legs does a cat have?},
}

\end{document}

因此您可以使用布尔值切换问题/答案。

相关内容