1

this is a follow up question from Printing arguments in \newcommand at different parts of document

I managed to get the MWE from that question to work from the great help which I have got. I thought that it would be trivial to extend the solution to my actual problem, but it appears that it is not so straightforward.

For each question, there is a part number, which is an additional parameter to be passed into \printQA. It is not reading this parameter #2 properly at the moment.

My current, more complex MWE looks as such,

\documentclass{article}
\usepackage{tabularx}
\usepackage{xifthen}% provides \isempty test
\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{comment}
\usepackage{environ}
\usepackage[margin=3cm]{geometry}
\linespread{1.7}
\setlength\parindent{0pt}

\newcounter{questionnumber}
\newcounter{questionpartnumber}[questionnumber]

\let\ea=\expandafter
\long\def\addto#1#2{\ea\def\ea#1\ea{#1#2}}

\def\answertext{}
\long\def\printQA#1#2#3{\par
   \ifthenelse{\isempty{#3}}{}{\hbox{\hbox to .07\hsize{\hss\bf#1(#2)\quad}\vtop{\hsize=0.9\hsize #3}}}
   \bigskip
}

\newcommand{\questiontext}[3]{
\stepcounter{questionnumber}
\ifthenelse{\isempty{#1}}{\noindent\textbf{\arabic{questionnumber}.}\\}
{\begin{tabularx}{\textwidth}{p{0.5cm} p{12.5cm} @{\extracolsep{\fill}}r}
\textbf{\arabic{questionnumber}.} & \noindent\parbox[t]{\hsize}{#1} & \ifx\empty#2\else[#2]\fi\\\\
\end{tabularx}}
\ea\addto\ea\answertext\ea{\ea\printQA\ea{\the\value{questionnumber}}{\the\value{questionpartnumber}}{#3}}
}

\newcommand{\questionpart}[3]{
\stepcounter{questionpartnumber}
\begin{tabularx}{\textwidth}{p{0.5cm} p{0.5cm} p{11cm} @{\extracolsep{\fill}}r}
& \textbf{\alph{questionpartnumber})} & \noindent\parbox[t]{\hsize}{#1} & \ifx\empty#2\else[#2]\fi\\\\
\end{tabularx}
\ea\addto\ea\answertext\ea{\ea\printQA\ea{\the\value{questionnumber}}{\the\value{questionpartnumber}}{#3}}
}

\newcommand{\citeschool}[3]{
\hspace*{1cm}[#1/#2/#3]\\
}

\NewEnviron{questionTopic}
{
\section*{Questions}
\BODY
\section*{Answers}
\answertext
}


\begin{document}

\begin{questionTopic}

\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}{}{}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer one}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}{answer two}

% question 2
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}{}{}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer three}

% question 3
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}{6}{answer four}

\end{questionTopic}

\end{document}

My output looks like this enter image description here It should be quite clear that when I tried to extend the MWE, I wasn't able to get the question part number in. Ideally, the answers should show 1(a), 1(b), 2(a) and 3. If #2 can be input into \printQA correctly, I think that this can be solved.

Thanks again for the help!

2

Here's my suggestions, in particular using \questionprolog for questions that have parts, so there's no need to do checks for empty arguments.

\documentclass{article}
\usepackage{etoolbox}

\newcounter{questionnumber}
\newcounter{questionpartnumber}[questionnumber]
\renewcommand{\thequestionpartnumber}{\alph{questionpartnumber}}

\newenvironment{questionTopic}
 {\section*{Questions}%
  \setlength{\parindent}{0pt}%
  \setlength{\rightskip}{2em}}
 {\par\setlength{\leftskip}{0pt}\setlength{\rightskip}{0pt}%
  \section*{Answers}\answertext}

\newcommand{\question}[3]{%
  \par\stepcounter{questionnumber}%
  \setlength{\leftskip}{2em}%
  \hspace{-2em}%
  \makebox[0pt][l]{\makebox[\columnwidth][r]{[#2]}}% the points
  \makebox[2em][l]{\bfseries\thequestionnumber.}#1\par
  \eappto{\answertext}{%
    \noexpand\textbf{\thequestionnumber}\unexpanded{ #3\par}%
  }%
}
\newcommand{\questionprolog}[1]{%
  \par\stepcounter{questionnumber}%
  \setlength{\leftskip}{2em}%
  \hspace{-2em}%
  \makebox[2em]{\bfseries\thequestionnumber.\quad}#1\par
}
\newcommand{\questionpart}[3]{%
  \par\stepcounter{questionpartnumber}%
  \setlength{\leftskip}{4em}%
  \hspace{-4em}%
  \makebox[0pt][l]{\makebox[\columnwidth][r]{[#2]}}% the points
  \makebox[4em][l]{\qquad\bfseries\thequestionpartnumber)}#1\par
  \eappto{\answertext}{%
    \noexpand\textbf{\thequestionnumber(\thequestionpartnumber)}\unexpanded{ #3\par}%
  }%
}

\begin{document}

\begin{questionTopic}

\questionprolog{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat.}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer one}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}{answer two}

% question 2
\questionprolog{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat.}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer three}

% question 3
\question{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat.}{6}{answer four}

\end{questionTopic}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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