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 starting with a clean slate creating a solutions manual. I would like the ability, by setting a switch, to produce only the solutions for the odd-numbered problems, or those for all problems, in each section.

I would much prefer not to use one of the answer packages, as I've not had great success with those in the past. My preferred solution would be to modify the itemized list macros in such a way that they would produce no output in particular cases. Or is there another, better way?

I should add that exercises will be numbered chapter.section.#, and it is whether # is even or odd that should govern inclusion.

I would imagine that I've not provided enough information for a decent answer, but I'm not sure what else to say...

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

The following shows a proof-of-concept that may be something you're looking for.

Packages used include

  • enumitem: For advanced manipulation of lists; and
  • environ: To allow for possible lengthy answers that may include paragraphs.

A boolean oddonly is used to condition on whether to print only odd-numbered answers or not. This is set to either true or false using \oddonly<bool> (where <bool> is either true or false). Two counters are used to number questions and answers, respectively. The reason for defining environments (question and answer) is, as mentioned above, because you might want to include a large description in a question/answer, for which the environ package is useful.

\documentclass{book}
\usepackage{enumitem}% http://ctan.org/pkg/enumitem
\usepackage{environ}% http://ctan.org/pkg/environ
\begin{document}

\newif\ifoddonly% Boolean to print odd-only answers
\oddonlytrue% Odd only answers = TRUE
\newcounter{question}[section]% Question counter (within section)
\newcounter{answer}[section]% Answer counter (within section)

\NewEnviron{question}{\stepcounter{question}\item \BODY}% Question
\NewEnviron{answer}{% Answer
  \stepcounter{answer}%
  \ifodd\value{answer}% Condition on value of answer
    \item \BODY% Print odd answer
  \else\ifoddonly\else
      \item \BODY% Print even answer
  \fi\fi%
}

% QUESTIONS ===========

\chapter{Some chapter}
\section{Questions} \label{questions}

\begin{enumerate}[label=\arabic{chapter}.\arabic{section}.\arabic{question})]
  \begin{question} Here is a question? \end{question}
  \begin{question} Here is another question? \end{question}
  \begin{question} This is yet another question? \end{question}
  \begin{question} Oh, and another one? \end{question}
  \begin{question} Here's the final question? \end{question}
\end{enumerate}

% ANSWERS =============

\section{Answers to Section~\ref{questions}}

\begin{enumerate}[label=\ref{questions}.\arabic{answer})]
  \begin{answer} Answer to this question. \end{answer}
  \begin{answer} Answer to that question. \end{answer}
  \begin{answer} Answer to another question. \end{answer}
  \begin{answer} Answer to fourth question. \end{answer}
  \begin{answer} Final answer. \end{answer}
\end{enumerate}

\end{document}

Odd-answers only

Setting \oddonlyfalse (the default) in the above example yields:

All answers

You may want to play around with the alignment of your list labels. However, for this, I suggest viewing some of alignment possibilities in the enumitem package documentation.

share|improve this answer
    
That answer is helpful on so many levels I don't know where to start. Thanks so much. –  rogerl Aug 26 '11 at 17:47
    
What happen if if we do \renewcommand\theanswer{\Roman{answer}} and then \isodd{\theanswers}, or something like that? I think is better use \isodd{\value{answer}}. –  leo Aug 8 '12 at 0:44
    
@leo: Thanks, yes, I've updated the answer. –  Werner Aug 9 '12 at 4:24
add comment

I know that you don't want a solution using the answers package, but for anyone interested I have put one below, which is a stripped-down version of what I have used for this task.

You can show/hide the odd (or even, or both) numbered solutions using these switches, set to 1 (show), or 0 (hide):

\setcounter{showodd}{1}
\setcounter{showeven}{1}

Code:

\documentclass{report}
\usepackage{ifthen}                        % conditionals
\usepackage{answers}                       % solutions to problems
\usepackage{xstring}                       % used to determine if problems are odd or even

% question environment 
\newcounter{question}
\setcounter{question}{0}
\renewcommand{\thequestion}{\thesection.\arabic{question}}
\newenvironment{question}{\refstepcounter{question}\thequestion.}{}

% open the solutions file
\Opensolutionfile{shortsolutions}
\Newassociation{shortsolution}{shortSoln}{shortsolutions}

\setlength{\parindent}{0mm}

\begin{document}

\chapter{Some chapter}
\section{Questions}

\begin{question}
Here's  a question.
\begin{shortsolution}
First answer.
\end{shortsolution}
\end{question}

\begin{question}
Another question.
\begin{shortsolution}
Second answer.
\end{shortsolution}
\end{question}

\begin{question}
Here's  a question.
\begin{shortsolution}
Third answer.
\end{shortsolution}
\end{question}

\begin{question}
Another question.
\begin{shortsolution}
Fourth answer.
\end{shortsolution}
\end{question}


% close the solutions files
\Closesolutionfile{shortsolutions}

% set up switches to show odd or even problems
%   1: show
%   0: don't show (actually, anything other than 1 makes it not show)
\newcounter{showodd}
\newcounter{showeven}
\setcounter{showodd}{1}
\setcounter{showeven}{1}

% solution to problem (show only odd, even, all)
% Note: this renewenvironment needs to go here
%       so that the answers package can still
%       display correctly to the page if needed
\newcounter{oddproblemnumber}
\renewenvironment{shortSoln}[1]{%
    % before the environment starts
    \fullexpandarg % need this line so that '.' are counted
        % numbers such as 1.3.1, 1.2.4, 7.5.6
        \StrBehind{#1}{.}[\newbit]%
        \StrBehind{\newbit}{.}[\problemnumber]%
     % determine if the problem number is odd or even
     % and depending on our choices above, display or not
     \ifthenelse{\isodd{\problemnumber}}%
     {%
         % set a counter that says the problem number is odd (used later)
         \setcounter{oddproblemnumber}{1}%
         % display or not 
         \ifthenelse{\theshowodd=1}
         {%
            % if we want to show the odd problems
            {\par\bfseries #1.}%
         }%
         {%
            % otherwise don't show them!
            \expandafter\comment%          
         }%
     }%
     {%
         % even numbered problem, set the counter (used later)
         \setcounter{oddproblemnumber}{0}%
         \ifthenelse{\theshoweven=1}
         {%
            % if we want to show the even problems
            {\par\itshape #1.}%
         }%
         {%
            % otherwise don't show them!
            \expandafter\comment%          
         }%
     }%
}%
{%
    % after the environment finishes
    \ifthenelse{\theoddproblemnumber=1}%
    {%
        % odd numbered problems 
         \ifthenelse{\theshowodd=1}
         {%
            % if we want to show the odd problems
            % then the environment is finished
         }%
         {%
            % otherwise we need to finish the comment
            \expandafter\endcomment%
         }%
    }%
    {%
        % even numbered problems
         \ifthenelse{\theshoweven=1}
         {%
            % if we want to show the even problems
            % then the environment is finished
         }%
         {%
            % otherwise we need to finish the comment
            \expandafter\endcomment%
         }%
    }%
}


\newpage
\section{Answers}
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}


\end{document}
share|improve this answer
    
Thanks. My issue with the answers package is the amount of file infrastructure that it needs. It seems really bulky to use, although I imagine that if I just used it I'd get used to it. It simply seems to more than I need, and thus is harder to use than I'd like. –  rogerl Aug 27 '11 at 23:11
    
I used to think that too, but am now pretty much in love with the answers package. It allows me to move questions around without worrying about the order- if I used the enumerate solution then I don't think the same would be true as I would have to adjust the ordering of the answers manually. –  cmhughes Sep 1 '11 at 12:10
add comment

I'dont know if this helps you, but my approach for conditional document creating is using the versions package. With this package, you can include selected regions this way:

\begin{VERSIONfinal}
This text will only appear if VERSIONfinal is included
\end{VERSIONfinal}

Somewhere after \begin{document} you have either to include or exclude a particular Version with \includeversion{VERSIONfinal} or \excludeversion{VERSIONfinal}.

Note: There is also a version package, so far I did not use it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.