4

I am trying to make a re-useable latex document for setting exam questions together with the answer keys. Because the intended future users will not be familiar with Latex, I have made some templates using \newcommand which can be used by them, and it does all the formatting for them. The new command can be used in this manner.

\questiontext{Enter your question text here}{Enter your answer here}

However, I would just like to make the two arguments in the \newcommand print at different parts of the document but correspond to each other.

The code below shows the MWE of what I have now, and what I would like to have.

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage{tabularx}
\usepackage{xifthen}% provides \isempty test

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

\newcommand{\questiontext}[2]{
\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}}
}

\newcommand{\questiontextwoscore}[1]{
\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}\\\\
\end{tabularx}}
}

\begin{document}
\section{Questions (Wrong)}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{500}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{123}

% what i want
\section{Questions (Right)}
\questiontextwoscore{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
\questiontextwoscore{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
\questiontextwoscore{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}

\section{Answers}
\questiontextwoscore{5}
\questiontextwoscore{500}
\questiontextwoscore{123}

\end{document}

enter image description here

My current output is in Section 1, what I am looking for ideally, is in Sections 2 and 3.

To summarize, is there a way to make a latex commands which takes in two arguments, prints all of argument 1 sequentially, then prints all of argument 2 sequentially.

Thanks!

  • For a similar question but with different syntax and possibly requirements see tex.stackexchange.com/questions/399536/… – Andrew Mar 20 '18 at 7:09
  • Thank looks interesting :) Made me think about another perspective. That post used another \newcommand. Maybe I can wrap my custom commands in a newenvironment – charpi Mar 20 '18 at 7:36
3

Typically, if you need to print something later then you can keep it in macro body (in \answertext macro in my example). This macro keeps the text:

\printQA{1}{answer1}\printQA{2}{answer2} etc.

The \printQA#1#2 macro prints question or answer. It solves the typographical design for such printing and the design is common for questions and answers.

The questions are printed (using\printQA) at the time when \questiontext is processed. The answers are printed when \answertext is processed.

\documentclass{article}

\newcount\QAnum

\long\def\printQA#1#2{\par
   \hbox{\hbox to .1\hsize{\hss\bf#1.\quad}\vtop{\hsize=.9\hsize \noindent#2}}
   \bigskip
}
\def\answertext{}
\let\ea=\expandafter

\long\def\questiontext#1#2{\par
   \advance\QAnum by1
   \printQA{\the\QAnum}{#1}
   \ea\addto\ea\answertext\ea{\ea\printQA\ea{\the\QAnum}{#2}}
}
\long\def\addto#1#2{\ea\def\ea#1\ea{#1#2}}

\begin{document}
\section{Questions}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{500}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{123}

\section{Answers}
\answertext

\end{document}
  • LaTeX recommends using \newcommand for very good reasons. For instance, your \def\addto would overwrite the macro defined by babel, with possible adverse results. – egreg Mar 20 '18 at 10:40
  • 1
    My main preference is to create macros which can be used outside LaTeX too. I never force users to LaTeX dependences. The latex-style lines above and below my example are only a decoration:). – wipet Mar 20 '18 at 11:01
  • Hello, I've tried to modify your solution for the MWE to my actual code. The only change which needs to be made is to include another argument into \questiontext - which is the question part number (i.e., \questiontext#1#2#3), and cascade the #3 down to \printQA. Compiles nicely and more or less works. Just that it isn't receiving the second argument (part number) properly (all set to 0). The third argument (answer) and first argument (question number) are perfectly find. Sorry for the trouble, but any idea what would be the issue? – charpi Mar 20 '18 at 12:32
  • @charpi Hello, I can't guess where is your problem without seeing your code. Maybe you need to apply \edef over the part number macro. Maybe you can open new question about it with full example code. – wipet Mar 20 '18 at 19:06

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.