选择特定项目 - 仅限带有宏的项目

选择特定项目 - 仅限带有宏的项目

我有兴趣尝试使用onlyitems 代码发布在这里采用某种方式来排除项目​​内的文本。

我想这样做的原因是我正在排版课程笔记,并希望能够为学生生成所选练习的答案。我读过几篇关于此内容的其他帖子 - 请参阅这两篇关于答案和练习包的帖子(这里这里) 对我没有帮助。上面的代码看起来最好,但我不能完全按照我的意愿使用它。我需要保持项目(练习)编号相同。我尝试了两种解决方案,但都不起作用。

  1. 我有两个环境,statement 和 answer,我希望能够根据我想要生成的版本在它们之间切换。我怀疑\newcommand上面提到的 onlyitems 代码中的 的使用排除了 items 内部的使用\excludecomment。代码对我来说,不使用 也可以工作\excludecomment,但使用 就不行,所以我很感激任何建议。

  2. 我也尝试创建一个\newif可以打开和关闭的 (参见第二个代码示例)。这也不起作用。

以下部分序言来自上面的onlyitems 代码:

`% from https://tex.stackexchange.com/questions/4502/onlyitems-how-to-select-specific-items-from-an-item-list
% A huge list of pgfkeys to be set up.
\pgfkeys{
/onlyitems/.is family, /onlyitems,
% Utility keys
utility/store tag/.store in = \itemOptions,
utility/store bool/.store in = \itemIf,
utility/process true/.style = {process/#1 = \iftrue},
utility/process false/.style = {process/#1 = \iffalse},%\fi
utility/set store bool/.style 2 args = {#1/.style = {utility/store bool = #2}},
utility/set select style/.style 2 args = {utility/set store bool = {select/#1}{#2}},
utility/verdict/.style = {},
utility/add to reset/.style = {
reset/.prefix style = {select/#1/.style = {select/.unknown}}
},
utility/current key/.estore in = \itemKey,
% Nothing in the process family is ever set, so any selection option is set to store a  boolean
process/.unknown/.style = {
utility/current key = \pgfkeyscurrentname,
select/\itemKey/.style = {
utility/set store bool = {utility/verdict}{#1}
},
utility/add to reset/.expand once = \itemKey
},
% For clearing the list of selected items (to support nested {itemize})
reset/.style = {},
reset/reset/.style = {reset/.style = {reset/reset, default}},
reset/reset,
% Here are the options you can actually pass.  These go to the {itemize}...
include/.style = {utility/process true/.list  = {#1}},
exclude/.style = {utility/process false/.list = {#1}},
all/.style  = {utility/set select style = {.unknown}{\iftrue}},
none/.style = {utility/set select style = {.unknown}{\iffalse}},%\fi
default/.style = {all},
% ...and these to the \item's
tag/.style = {utility/store tag = {[#1]}},
id/.style = {select/#1},
}
% Like tikzset, it sets a default key path
\newcommand\onlyitemsset[1]{\pgfkeys{/onlyitems, #1}}

\let\itemLaTeX=\item
% Have to use a \newcount because (ugh) the LaTeX \setcounter is a global assignment
\newcount\itemsSoFar
\renewcommand\item[1][]{%
% The \egroup closes off any \vbox'es from the previously ignored \item.
% The conditional \bgroup cancels it out when there aren't any.
\itemIf\bgroup\fi\egroup
\let\itemIf=\iffalse%\fi
\advance\itemsSoFar by 1 %
\onlyitemsset{utility/store tag = {}, utility/verdict/.style = {},    select/\the\itemsSoFar, #1, utility/verdict}%
\itemIf
\def\next{\expandafter\itemLaTeX\itemOptions}%
\else
% The vbox is set and then discarded, effectively ignoring an entire \item.
% This inner \itemLaTeX is necessary to avoid a glitch when we ignore the first \item of an itemize that itself contains a nested \itemize.  Don't ask, I don't know.
\def\next{\setbox0=\vbox\bgroup\itemLaTeX}%
\fi
\next
}

% \let\itemizeLaTeX=\itemize
% \let\enditemizeLaTeX=\enditemize
% \renewcommand\itemize[1][]{%
%  \let\itemIf=\iftrue
%  \itemsSoFar = 0 %
%  % We have to reset here so that the selections from an outer itemize don't conflict with an inner one.
%  \onlyitemsset{reset, #1}%
%  \itemizeLaTeX
% }
% \renewcommand\enditemize{%
%  % This closes off the last \vbox
%  \itemIf\bgroup\fi\egroup\enditemizeLaTeX
% }

\newcommand\onlyitems[2][]{%
\let\itemIf=\iftrue
\itemsSoFar = 0 %
% We have to reset here so that the selections from an outer itemize don't conflict with an inner one.
\onlyitemsset{reset, #1}%
#2%
% This closes off the last \vbox
\itemIf\bgroup\fi\egroup
}

以下是我想做的事情(需要上述序言):

% Here is my part of the preamble
\newenvironment{answer}{}{}
\newenvironment{statement}{}{}

\usepackage{comment}
%\excludecomment{answer}
\excludecomment{statement}


\begin{document}

\newcommand{\studentanswers}{
\item[id=noanswer]
\begin{statement}
Determine whether...
\end{statement}
\begin{answer}
No
\end{answer}

\item
\begin{statement}
The rectangle...
\end{statement}
\begin{answer}
7
\end{answer}

\item
\begin{statement}
Decide...
\end{statement}
\begin{answer}
Yes
\end{answer}
}

\begin{answer}
\begin{enumerate}\onlyitems[exclude={noanswer}]
\studentanswers
\end{enumerate}
\end{answer}

\end{document}

这是我提出的第二个解决方案的代码。前言的前面部分是必需的,另外还添加了:

\usepackage{ifthen}
\newif\ifshowanswer
\showanswertrue
%\showanswerfalse

\newcommand{\showanswer}[1]{\ifshowanswer #1}

\newif\ifshowstatement
%\showstatementtrue
\showstatementfalse

\newcommand{\showstatement}[1]{\ifshowstatement #1 \fi}


\begin{document}

\newcommand{\studentanswers}{
\item[id=noanswer]
\showstatement{Determine whether...}
\showanswer{No}

\item
\showstatement{The rectangle...}
\showanswer{7}

\item
\showstatement{Decide...}
\showanswer{Yes}
}

\begin{enumerate}\onlyitems[exclude={noanswer}]
\studentanswers
\end{enumerate}

\end{document}

相关内容