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 making a questions/answers page where answer follows the question. The questions are enumerated while the answers are not. Questions are using one color while answers using another one.

In order to pause numeration, I've used the solution provided to this question. As a result, my code looks like this in general:

\documentclass[english]{article}
...
\newcounter{savedenum}
\newcommand*{\saveenum}{\setcounter{savedenum}{\theenumi}}
\newcommand*{\resume}{\setcounter{enumi}{\thesavedenum}}
...
\usepackage{parskip}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{enumitem}
...
\begin{document}
...
  \begin{enumerate}
    \item \color{black}{Question text goes here.}
    \saveenum
  \end{enumerate}

  \begin{itemize}
    \item[] \color{NavyBlue}{Answer text goes here.}
  \end{itemize}

  \begin{enumerate}
    \resume
    \item \color{black}{Question text goes here.}
    \saveenum
  \end{enumerate}

  \begin{itemize}
    \item[] \color{NavyBlue}{Answer text goes here.}
  \end{itemize}
...
\end{document}

Since there is a lot of code reuse, I'm seeking for a better solution (maybe a loop of some kind?), if possible, that will substitute the copy-and-paste of the \begin{...} ... \end{...} blocks.

share|improve this question
    
You can use \item[\textbullet] for introducing the answer, staying in the same enumerate environment. –  egreg Dec 29 '12 at 10:05
    
Did you try the exercise package? –  zunbeltz Dec 29 '12 at 10:54
1  
I'd probably look in the direction of the theorem/ntheorem/amsthm packages to define an automatically-numbered question environment. –  Ulrich Schwarz Dec 29 '12 at 11:05

2 Answers 2

up vote 8 down vote accepted

I suggest a different approach:

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\newif\ifsolutions
\solutionstrue
\newenvironment{exercises}
 {\begin{enumerate}}
 {\end{enumerate}}
\newenvironment{question}
 {\item}
 {}
\ifsolutions
  \newenvironment{solution}
   {\par\nopagebreak\begingroup\color{NavyBlue}}
   {\endgroup}
\else
  \usepackage{comment}
  \excludecomment{solution}
\fi

\begin{document}
\begin{exercises}

\begin{question}
Question one text goes here.
\end{question}
\begin{solution}
Answer one text goes here.
\end{solution}

\begin{question}
Question two text goes here.
\end{question}
\begin{solution}
Answer two text goes here.
\end{solution}

\end{exercises}

\end{document}

The markup might seem excessive, but it allows for greater flexibility: you can customize exercises using enumitem features, but also question and solution. I've added a possibility: if you comment the

\solutionstrue

line, the solutions will not be printed at all.

Solutions active

enter image description here

Solutions suppressed

enter image description here

share|improve this answer

Since you're already loading the enumitem package, here's a solution using the resume feature:

\documentclass{article}
\usepackage{enumitem}
\usepackage{parskip}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\newenvironment{question}{\enumerate[resume]\item}{\endenumerate}
\newenvironment{solution}{\itemize\item[]\begingroup\color{NavyBlue}}{\endgroup\enditemize}

\begin{document}

  \begin{question}
    Question text goes here.
  \end{question}
  \begin{solution}
    Answer text goes here.
  \end{solution}
  \begin{question}
    Question text goes here.
  \end{question}
  \begin{solution}
    Answer text goes here.
  \end{solution}
\end{document}

If you'd prefer to have your own list (perhaps you're using the enumerate environment outside of this task) then you could use a newlist

\newlist{myenumerate}{enumerate}{5}
\setlist[myenumerate]{label=\arabic*.,resume}

\newenvironment{question}{\myenumerate\item}{\endmyenumerate}

Finally, if you'd like to suppress the solution environment, or perhaps output it to a separate file, I'd recommend looking at the answers package; you can switch the answers on and off in the main part of the document by using

% solutions written to file (NOT to main part of document)
\usepackage{answers}   
% solutions NOT written to file (written to main part of document)
\usepackage[nosolutionfiles]{answers}

Complete MWE

\documentclass{article}
\usepackage{enumitem}
\usepackage{parskip}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{answers}
%\usepackage[nosolutionfiles]{answers}

\newlist{myenumerate}{enumerate}{5}
\setlist[myenumerate]{label=\arabic*.,resume}

\newenvironment{question}{\myenumerate\item}{\endmyenumerate}

% open the answer file
\Opensolutionfile{shortsolutions}
\Newassociation{solution}{ShortSoln}{shortsolutions}

\begin{document}

\begin{question}
    Question text goes here.
    \begin{solution}
        Answer text goes here.
    \end{solution}
\end{question}
\begin{question}
    Question text goes here.
    \begin{solution}
        Answer text goes here.
    \end{solution}
\end{question}

% close the solutions files
\Closesolutionfile{shortsolutions}

\clearpage

% this just makes the displayed solutions use the itemize
% environment- makes the dispaly better
\renewenvironment{ShortSoln}[1]{%
\itemize\item[{\bfseries(#1)}]%
}%
{\enditemize}

% input the answers file
\section*{Answers}
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}
\end{document}
share|improve this answer

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.