在环境之间切换

在环境之间切换

我在大学任教,用德克萨斯州出题。我想出一份包含多个问题的考试,其中每个问题我都包含空行和带轴的空图形(用于实际考试 - 学生填写)或解决方案,包括完整的图形。例如:

Q. Illustrate what happens to the demand of housing when interest rates go down.

\begin{nosolution}
A FEW HORIZONTAL LINES
\begin{figure}
\includegraphics[scale=1]{EmptyGraph_withAxes}
\end{figure}
\end{nosolution}

\begin{solution}
It will increase.... 
\begin{figure}
\includegraphics[scale=1]{HousingDemand}
\end{figure}
\end{solution}

然后在考试开始时设置一个参数,例如 \printsolution,表示打印 \begin{solution}...\end{solution} 中的内容,否则打印 \begin{nosolution} ... \end{nosolution} 中的内容。我定义了一个 solution 和 nosolution 环境:

\newenvironment{solution}
{
\vspace{0.5cm}
\textbf{Solution.} \quad \itshape
}{}

\newenvironment{nosolution}
{
\hrulefill
\hrulefill
\hrulefill
}{}

有没有办法根据初始参数(如 \printsolution)在两者之间切换?我一直在尝试考试类(exam.cls),但它并不完美,因为它无法在解决方案中使用浮点数:我收到了丢失浮点数的消息。我还查看了文档,确认不支持浮点数,因为它使用框架。谢谢,Mario

答案1

exsheets软件包已为此做好了解决方案。以下示例

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{exsheets}

% custom headings:
\DeclareInstance{exsheets-heading}{myheading}{default}{
  runin           = true ,
  title-post-code = \space ,
  attach          = { main[l,vc]points[l,vc](\linewidth+\marginparsep,0pt) } ,
  join            = { main[r,vc]title[r,vc](0pt,0pt) }
}

% setup:
\SetupExSheets{
  headings       = myheading ,
  question/name  = Q. ,
  solution/name  = A. ,
  % solution/print = true
  solution/print = false
}

\begin{document}

\begin{question}
  Illustrate what happens to the demand of housing when interest rates go down.
\end{question}

% if solutions are not printed:
\PrintSolutionsF{%
  \blank[width=4.8\linewidth,linespread=1.5]{}% 4.8 lines to write on
  \begin{center}
    \includegraphics[scale=1]{EmptyGraph_withAxes}% demo picture
  \end{center}
}

% if solutions are pinted:
\begin{solution}
  It will increase\ldots
  \begin{center}
    \includegraphics[scale=1]{HousingDemand}
  \end{center}
\end{solution}

\end{document}

给出

在此处输入图片描述

solution/print = false

在此处输入图片描述

solution/print = true

答案2

以下是MWE基于的etoolbox

\documentclass{article}
\usepackage{etoolbox}

%\edef\printsolution{} % Uncomment to print solutions.

\newcommand{\nosolution}[1]{%
    \ifdefmacro{\printsolution}{}{#1}%
}

\newcommand{\solution}[1]{%
    \ifdefmacro{\printsolution}{#1}{}%
}{}

\begin{document}


\nosolution{
No solution.
}

\solution{
Solution.
}

\end{document}

答案3

感谢大家的建议。最后我借鉴了大家的建议,并制作了自己的定制命令。希望这对其他人有用

\documentclass{article}
\usepackage{forloop,etoolbox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solutions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define an new command to switch between solution and nosolution with 
% empty lines instead
% Command showlines defines the number of lines. 

\newcommand{\nosolution}[1]{%
    \ifdefmacro{\printsolution}{}{#1}%
}

\newcommand{\solution}[1]{%
    \ifdefmacro{\printsolution}{\vskip0.1cm \noindent \textbf{Answer:} \textit{#1}}{}%
}

\newcounter{numlines}
\newcommand\showlines[1]{%
    \setcounter{numlines}{0}%
    \forloop{numlines}{0}{\value{numlines} < #1}{\vskip0.3cm \hrulefill}%
 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\edef\printsolution{} % Uncomment to print solutions.

\begin{document}

THIS IS THE QUESTION

\solution{THIS IS THE ANSWER. 
}
\nosolution{
\showlines{6}
}

\end{document}

答案4

您可以执行以下操作,设置\showanswerstrue\showanswersfalse确定显示的内容或隐藏的内容。它提供了两种添加答案的方法:

使用answer/noanswer环境,该环境在包中定义comment。或者,您也可以直接在问题下方输入答案,然后使用 来\onlyanswer隐藏部分内容。您可能希望向其中添加一个可选参数,以提供替换文本或\hrule,但目前尚未内置。这可用于隐藏填空题答案的部分内容或从图表中删除图表。请注意,如果图表中没有图表,则必须手动设置axis

最后有一个\showlines{<n>}绘制<n>线条的命令。

\documentclass{article}

%% Define the switch to control which is shown
\usepackage{comment,forloop}
\newif\ifshowanswers
\showanswersfalse   % Comment out one of these to show
%\showanswerstrue    % or hide the answers

%% Define the answer/noanswer environments and hide when necessary
\specialcomment{answer}{}{\vspace{2em}}     % Add some space between
\specialcomment{noanswer}{}{\vspace{2em}}   % the environments
\ifshowanswers% Hide either of the two environments defined above
  \excludecomment{noanswer}
\else
  \excludecomment{answer}
\fi

%% Define an environment to markup the questions
\newcounter{questionnumber}
\setcounter{questionnumber}{0}
\newenvironment{question}%
  {
    \stepcounter{questionnumber}
    \bf \arabic{questionnumber}.
  }
  {\par\vspace{1em}}

%% Some extra commands that might be helpful
\newcommand\onlyanswer[1]{\ifshowanswers{#1}\else{}\fi} % Discard contents if not needed
\newcommand\showlines[1]{%
    \newcounter{numlines}
    \forloop{numlines}{0}{\value{numlines} < #1}{\hrulefill\par}%
  }

%% Only necessary for the plot in the second example question
\usepackage{pgfplots}

\begin{document}
Introduction to exam

\begin{question}
Some question
\end{question}
\begin{answer}
The true answer to the question
\end{answer}
\begin{noanswer}
\showlines{3}
\end{noanswer}

\begin{question}
A second question with a plot to be filled in

\begin{tikzpicture}
  \begin{axis}[
      xmin=0, xmax=10,
      ymin=0, ymax=100,
    ]
    \onlyanswer{\addplot[domain=0:10,no marks] {x^2};}
  \end{axis}
\end{tikzpicture}
\end{question}

\end{document}

更新:添加了一些空格和计数器来对问题进行编号

相关内容