Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm interested in trying to use the onlyitems code posted here with some way to exclude text inside the items.

The reason I would like to do this is that I am typesetting course notes and would like to be able to generate answers to selected exercises for students. I've read a couple of other posts about this - see these two about the answers and exercises packages (here and here) that were not helpful to me. The above code seems best, but I can't quite do exactly what I want with it. I need to keep the item (exercise) numbers the same. I have tried two solutions that don't work.

  1. I have two environments, statement and answer, which I would like to be able to switch between depending on the version I want to generate. I suspect that the use of \newcommand in the onlyitems code mentioned above precludes the use of \excludecomment inside the items. The code works for me without the use of \excludecomment but does not work with it, so I would appreciate any suggestions.

  2. I have also tried creating a \newif that could be turned on and off (see second code sample). This also does not work.

The following part of the preamble is from the onlyitems code above:

`% from http://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 what I am trying to do (requires above in preamble):

% 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}

Here is the code for my second proposed solution. The earlier parts of the preamble are required, with the addition of:

\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}
share|improve this question
    
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. –  egreg Mar 29 '12 at 22:52
    
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. I attempted to cut and paste the above into something I could work with, but got errors, so would be helpful if you created a compilable example. –  Peter Grill Apr 3 '12 at 23:55
add comment

closed as not a real question by Joseph Wright May 6 '12 at 7:52

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.