1

I'm trying to display the answers to the different Questions in an Exercise in a single line, since answers to questions are often short. Here a workaround was provided but for subQuestions, not Questions. I've naively tried to replace every 'subQuestion' with 'Question' but (not surprisingly) it didn´t work. But in addition, I'd also like to have all the answers in the same line as the number of the exercise: my desired output would be:

1.1 (a) 1st ans. (b) 2nd ans. (c) 3rd ans.


MWE:

\documentclass[]{book}

\usepackage[lastexercise, answerdelayed]{exercise}

\renewcommand{\ExerciseHeader}{\vspace{-0.3cm} \hrule \vspace{0.2cm} \noindent \bfseries \thesection.\arabic{Exercise} }

\renewcommand{\AnswerHeader}{\vspace{-0.4cm} \hrule \vspace{0.1cm} \noindent \bfseries \thesection.\arabic{Exercise} \ }

\renewcommand{\QuestionNB}{(\bfseries \alph{Question})}

\counterwithout{section}{chapter}
\counterwithin*{Exercise}{section}

\begin{document}

\section{First Section}

\begin{Exercise}
Guideline
\Question First question blabla
\Question Second question blabla
\Question Third question blabla 
\end{Exercise}

\vspace{3pt} $\vdots$ \vspace{10pt}

\begin{Answer}
\Question 1st ans.
\Question 2nd ans.
\Question 3rd ans.
\end{Answer}

\shipoutAnswer

\end{document}

The output for that is four lines: one for 1.1, another for (a), (b), and (c).


Here is my failed attempt at doing it myself:

\makeatletter
\let \@QuestionHeader@original=\@QuestionHeader
\newcommand{\@QuestionHeader@noItem}{%
    \hspace{1cm}%
    \begingroup
        \@getQuestionInfo
        \QuestionHeaderDifficulty
        \QuestionNB
    \endgroup
    \begingroup
        \@getQuestionInfo
        \QuestionHeaderTitle
    \endgroup%
    \ignorespaces
}%

\newcommand{\QuestionOnSameLine}{%
    \let \@QuestionHeader=\@QuestionHeader@noItem
    \subQuestion
}
\newcommand{\QuestionOnNewLine}{%
    \let \@QuestionHeader=\@QuestionHeader@original
    \Question
}
\makeatother

1 Answer 1

1

Sometimes the best solution is the simplest. The exercise package does some fancy footwork behind the scenes to make the \Question commands begin (and later end) implicit list environments (and likewise for \subQuestion and \subsubsQuestion. But since we just want to compile the answer items on a single line, we can define \MyQuestion as:

\newcommand{\MyQuestion}{\refstepcounter{Question}%
   \ifnum\value{Question}=1\else
     \quad\fi{\QuestionNB\unskip}~%
   }

This uses the Question counter defined by exercise package to number our answers as before, and inserts a quad space before the question number on all questions after the first and a non-breakable space after the number. I've also enclosed the question number in a group since the existing \Question command does the same and your definition of \QuestionNB assumes this group (although you might not have realized it. Finally, there's an \unskip after \QuestionNB since the default definition of \QuestionNB has a space there, but since it's a breakable space, we want to get rid of it.

Now if all your answers are going to be in this single-line format, and you don't want to edit your document to change the \Question commands to \MyQuestion, you can add to the document preamble:

\renewcommand{\AtBeginAnswer}{\let\Question\MyQuestion}

which will redefine \Question to use \MyQuestion in the Answer environment.

I did not add support for the optional argument to \Question to my solution, nor did I deal with \subQuestion or \subsubQuestion. Those are left as an exercise for the reader.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .