3

The answers to sub-questions are often short: a) 2, b) -15, c) 21 How can I force a no-line-break for any given sub question? MWE follows

%
%--------------------------------------------------------------------------------------------------------- %
\documentclass{book}
%--------------------------------------------------------------------------------------------------------- %
%
\usepackage[fleqn]{amsmath} %
\usepackage{amsthm,amssymb} %
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,driver=none,lmargin=25mm,vmargin=20mm,showframe,showcrop]{geometry} % verbose,showcrop,showframe=inner frame
\usepackage[a4,frame,center]{crop} % pageframe
\usepackage[lastexercise,answerdelayed]{exercise}
%
\setlength{\marginparsep}{0mm} %
%
\renewcommand{\subQuestionNB}{\alph{subQuestion}) }

\renewcommand{\AnswerHeader}{\medskip{\textbf{\AnswerName\ %
    \ExerciseHeaderNB}\smallskip}}

\renewcommand{\ExerciseHeader}{\textbf{\quad\:\ExerciseName\;\ExerciseHeaderNB\ExerciseHeaderTitle\ExerciseHeaderOrigin} %
  \par\nopagebreak\medskip}

\setlength{\subQuestionIndent}{16.8pt}

\renewcounter{Exercise}[chapter] % Reset counter every chapter
\renewcounter{Answer}[chapter] %
\newcounter{Problem}[chapter] %
%
\def\AnswerName{\par{Answers to questions on chapter}} %
%
\begin{document}

\begin{Exercise}[label={E1},name={Exercises on chapter},counter={Problem}]
  \Question{What is the symbol for iron?}
  \Question{What is the symbol for hydrogen?}
\end{Exercise}

\begin{Exercise}[label={E2},name={Exercises on chapter},counter={Problem}]
  \Question{What is the symbol for iron?}
  \Question{For hydrogen, state the following:}
        \subQuestion{the valency?} \subQuestion{the atomic number?} \subQuestion{the density?}
\end{Exercise}

  \begin{Answer}[ref={E1}]
    \Question{Fe}     
  \Question{H} 
  \end{Answer}

  \begin{Answer}[ref={E2}]
  \Question{Fe}
    \Question{}
    \subQuestion{2}
    \subQuestion{1 - Want this answer to follow on same line as (a)}
    \subQuestion{small - and this answer, too}
  \end{Answer}

\shipoutAnswer

\end{document}

1 Answer 1

1
+100

I am not familiar with the exercise package but I did not find anything in the documentation that it supports to have answers on the same line. Therefore I have defined a new command \subQuestionOnSameLine for this purpose.

The command responsible for placing a subquestion on a new line is the \item in \@subQuestionHeader. I have defined a new command \@subQuestionHeader@noItem which is basically just the output of \show\@subQuestionHeader without the \item but a \hspace instead.

\subQuestionOnSameLine simply replaces \@subQuestionHeader with \@subQuestionHeader@noItem and then executes the normal \subQuestion.

\@subQuestionHeader is reset at the end of the question where the group ends. Until then all \subQuestion will behave like \subQuestionOnSameLine. If you want to place a subquestion on a new line after a \subQuestionOnSameLine you can use \subQuestionOnNewLine.

Please note that the first subquestion of each question should always be a normal \subQuestion, never a \subQuestionOnSameLine.

\documentclass{book}
% ------ packages ------
\usepackage[fleqn]{amsmath}
\usepackage{amsthm,amssymb}
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,driver=none,lmargin=25mm,vmargin=20mm,showframe,showcrop]{geometry}
\usepackage[a4,frame,center]{crop}
\usepackage[lastexercise,answerdelayed]{exercise}

% ------ \subQuestionOnSameLine ------
\makeatletter
\let \@subQuestionHeader@original=\@subQuestionHeader
\newcommand{\@subQuestionHeader@noItem}{%
    \hspace{1cm}%
    \begingroup
        \@getsubQuestionInfo
        \subQuestionHeaderDifficulty
        \subQuestionNB
    \endgroup
    \begingroup
        \@getsubQuestionInfo
        \subQuestionHeaderTitle
    \endgroup%
    \ignorespaces
}%

\newcommand{\subQuestionOnSameLine}{%
    \let \@subQuestionHeader=\@subQuestionHeader@noItem
    \subQuestion
}
\newcommand{\subQuestionOnNewLine}{%
    \let \@subQuestionHeader=\@subQuestionHeader@original
    \subQuestion
}
\makeatother

% ------ layout ------
\setlength{\marginparsep}{0mm}

\renewcommand{\subQuestionNB}{\alph{subQuestion}) }

\renewcommand{\AnswerHeader}{\medskip{\textbf{\AnswerName\ 
    \ExerciseHeaderNB}\smallskip}}

\renewcommand{\ExerciseHeader}{\textbf{\quad\:\ExerciseName\;\ExerciseHeaderNB\ExerciseHeaderTitle\ExerciseHeaderOrigin} 
  \par\nopagebreak\medskip}

\setlength{\subQuestionIndent}{16.8pt}

\renewcounter{Exercise}[chapter] % Reset counter every chapter
\renewcounter{Answer}[chapter]
\newcounter{Problem}[chapter]

\def\AnswerName{\par{Answers to questions on chapter}}


% ------ document ------
\begin{document}

\begin{Exercise}[label={E1},name={Exercises on chapter},counter={Problem}]
    \Question{What is the symbol for iron?}
    \Question{What is the symbol for hydrogen?}
\end{Exercise}

\begin{Exercise}[label={E2},name={Exercises on chapter},counter={Problem}]
    \Question{What is the symbol for iron?}
    \Question{For hydrogen, state the following:}
        \subQuestion{the valency} \subQuestion{the atomic number} \subQuestion{the density}
\end{Exercise}

\begin{Answer}[ref={E1}]
    \Question{Fe}
    \Question{H}
\end{Answer}


\begin{Answer}[ref={E2}]
    \Question{Fe}
    \Question{}
        \subQuestion{answer to~a}
        \subQuestionOnSameLine{answer to~b}
        \subQuestionOnSameLine{answer to~c}
\end{Answer}

\shipoutAnswer

\end{document}

screenshot

3
  • Exactly what I was looking for.
    – keith77777
    Commented Apr 27, 2017 at 13:08
  • @keith77777 I'm glad I could help. If it solved your problem please accept the answer.
    – jakun
    Commented Apr 30, 2017 at 7:28
  • Sorry, I thought I had. I now have ticked the tick - is that adequate?
    – keith77777
    Commented May 1, 2017 at 11:21

You must log in to answer this question.

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